0

My form type looks like

class ApplicationFormType extends AbstractType
{
        public function buildForm(FormBuilderInterface $builder, array $options)
    {
            $nurse = new Type\NurseType();
            $builder
                ->add('nurse', $nurse)
                ->add('nurse_type', 'entity', array(
                    'class' => 'Acme\MainBundle\Entity\NurseType',
                    'expanded' => true,
                    'multiple' => false,
                ))
                ->add('nursing_support', 'checkbox', array(
                    'required' => false,
                ))
                ->add('nursing', new Type\NursingType());
            $builder->addEventSubscriber(new ApplicationDynamicNursingSubscriber());
        }
}

Depending NursingType looks like

class NursingType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name')
            ->add('street')
            ->add('zipcode')
            ->add('city')
            ->add('email')
            ->add('phone');
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array('data_class' => 'Acme\MainBundle\Entity\Nursing'));
    }

The event looks like (and what I want to do)

class ApplicationDynamicNursingSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents() {
        return array(
            FormEvents::POST_BIND => 'onPostBind',
        );
    }

    public function onPostBind(FormEvent $event) {
        $data = $event->getData();
        $form = $event->getForm();
        if (!$data->getNursingSupport()) {
            $data->setNursing(NULL);
            $form->get('nursing')->setRequired(false);
        }
    }
}

There is no method setRequired for fields, so my event script fails on form submit. How can I get Symfony2 to make the entity nursing not required, when checkbox nursing_support is not checked?

rabudde
  • 7,498
  • 6
  • 53
  • 91

1 Answers1

1

Instead of hooking into the form event to validate, why don't you try using the build in validation on the entity using the Callback method to determine if a certain field is required or not.

http://symfony.com/doc/current/reference/constraints/Callback.html

Ghassan Idriss
  • 2,120
  • 20
  • 16
  • You will probably want to make a plain object class for your ApplicationFormType and set it as the data_class. Then put the callback constraint on there since that is where the checkbox is. You can leave the constraints on your current entities because they wont be validated unless they are included and required. – Ghassan Idriss Mar 28 '13 at 16:58
  • Yes, I've already such a class, that is set as `data_class`. But I could not believe that is such simple. It's working. When no field of entity `Nursing` is filled, then the callback contraint works (because entity is `NULL`). And when at least one field of `Nursing` is filled (so `$application->getNursing()` is not `NULL`), property constraints of entity `Nursing` validate the data set. – rabudde Mar 28 '13 at 20:00
  • Symfony2 is pretty awesome :) – Ghassan Idriss Mar 28 '13 at 20:03