如何从子表单 symfony2 的字段中删除 required

How to remove required from the field of the child form symfony2

我有2个表格。对于子表单,我需要删除必填字段。

CourseType 形式:

class CourseType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('contactName', TextType::class, [
            'attr' => [
                'placeholder' => 'Enter Contact name'
            ]])
            ->add('save', SubmitType::class, array('label' => 'Submit'));
    }
}

批量课程更新表格:

class BulkCourseType extends CourseType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);
        $builder->add('ids', HiddenType::class, array(
            'data' => $options['ids'],
            'mapped' => false
        ));

        //need to remove required for contactName field
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'ids' => null
        ]);
    }
}

在上面的示例中删除此属性的最简单方法是什么?

最简单的方法:

$builder->get('contactName')->setRequired(false);

感谢大家