2

As I know I could create form using form type: $form = $this->createForm(new RegistrationType(), $user);

And here is form type:

class RegistrationType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name', 'text');
        $builder->add('email', 'email');

        $builder->add('terms', 'checkbox', array(
            'mapped' => false
        ));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'App\UsersBundle\Entity\User'
        ));
    }

    public function getName()
    {
        return 'user_registration';
    }

}

So I could add field term and don't map it to entity. But what is the way to validate this field? Sure I can do something like if ($form->get('terms')->getData()) in my controller but I want to use one function $form->isValid() to validate all fields (mapped and don't mapped)? May be any validate hooks or events exists?

1 Answer 1

4

It's covered in the Adding Validation section of the Forms chapter.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.