class FormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('a')
->add('b')
->add('c')
->add('d');
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' =>'entity'
));
}
}
html表单字段:
<form>
<input name="a" value="a">
<input name="b" value="b">
</form
当我提交表单时,c,d设置为null。 当表单没有这个字段时,我不想更新c和d:)
答案 0 :(得分:0)
这是
的内容public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('a')
->add('b')
->add('c')
->add('d');
}
使用此
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('a')
->add('b')
->add('c', TextType::class , array('mapped' => false))
->add('d', TextType::class , array('mapped' => false));
}
答案 1 :(得分:0)
像这样更新你的buildForm函数。在Symfony FormType中,默认情况下所有字段都是必需的。
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('a')
->add('b')
->add('c',TextType::class, ['required' => false,'empty_data' => null])
->add('d',TextType::class, ['required' => false,'empty_data' => null] );
}
如果要在数据库c中保留实体,并且d也必须可以为空