Reputation: 43
I have a very simple form without class. I made some items with constraint options but the form validation does not work.
I've read several places (e.g here )
I can add a validation_constraint
parameter which is a \Symfony\Component\Validator\Constraints\Collection
instance.
When I try I always get an error message:
The option "validation_constraint" does not exist. Known options are: ... blabla
My form:
$collectionConstraint = new \Symfony\Component\Validator\Constraints\Collection(
array(
'customer' => new \Symfony\Component\Validator\Constraints\NotBlank(),
'customer_address' => new \Symfony\Component\Validator\Constraints\NotBlank(),
'customer_address_postal' => new \Symfony\Component\Validator\Constraints\NotBlank(),
'paymentDeadline' => new \Symfony\Component\Validator\Constraints\Date(),
'fulfillmentDate' => new \Symfony\Component\Validator\Constraints\Date(),
'currency' => new \Symfony\Component\Validator\Constraints\Choice(array(
'choices' => $currency_entities
)),
'paymode' => new \Symfony\Component\Validator\Constraints\Choice(array(
'choices' => $paymode_entities
))
)
);
$form = $this->createFormBuilder(null,array(
'validation_constraint' => $collectionConstraint
))
->add('customer','choice',array(
'choice_list'=> $customer_choices,
'multiple' => false,
'required' => true,
'empty_value' => '',
'attr' => array(
'class' => 'chosen large',
)
))
->add('customer_address','choice',array(
'multiple' => false,
'required' => true,
'empty_value' => '',
'attr' => array(
'class' => 'chosen large'
)
))
->add('customer_address_postal','choice',array(
'multiple' => false,
'required' => true,
'empty_value' => '',
'attr' => array(
'class' => 'chosen large'
)
))
->add('paymentDeadline','date',array(
'input' => 'datetime',
'widget' => 'single_text',
'required' => true,
'attr' => array(
'class' => 'date-picker m-ctrl-medium',
'addon' => 'icon-calendar',
)
))
->add('fulfillmentDate','date',array(
'input' => 'datetime',
'widget' => 'single_text',
'required' => true,
'attr' => array(
'class' => 'date-picker m-ctrl-medium',
'addon' => 'icon-calendar',
)
))
->add('currency','choice',array(
'required' => true,
'choice_list' => $curreny_choices
))
->add('paymode','choice',array(
'required' => true,
'choice_list' => $paymode_choices
))
->add('subject','text',array(
'required' => false,
'attr' => array(
'class' => 'span8'
)
))
->add('comment','textarea',array(
'required' => false,
'attr' => array(
'class' => 'span8',
'rows' => 5
)
))
;
Symfony version is 2.3.3.
What could be the problem?
Upvotes: 4
Views: 6140
Reputation: 11762
The validation has to be applied to each field using the constraints
option, not to the form builder.
So your code should be as follow:
use \Symfony\Component\Validator\Constraints\NotBlank;
use \Symfony\Component\Validator\Constraints\Date;
use \Symfony\Component\Validator\Constraints\Choice;
$form = $this->createFormBuilder(null)
->add('customer','choice',array(
'choice_list'=> $customer_choices,
'multiple' => false,
'required' => true,
'empty_value' => '',
'attr' => array(
'class' => 'chosen large',
),
'constraints' => new NotBlank()
))
->add('customer_address','choice',array(
'multiple' => false,
'required' => true,
'empty_value' => '',
'attr' => array(
'class' => 'chosen large'
),
'constraints' => new NotBlank()
))
->add('customer_address_postal','choice',array(
'multiple' => false,
'required' => true,
'empty_value' => '',
'attr' => array(
'class' => 'chosen large'
),
'constraints' => new NotBlank()
))
->add('paymentDeadline','date',array(
'input' => 'datetime',
'widget' => 'single_text',
'required' => true,
'attr' => array(
'class' => 'date-picker m-ctrl-medium',
'addon' => 'icon-calendar',
),
'constraints' => new Date()
))
->add('fulfillmentDate','date',array(
'input' => 'datetime',
'widget' => 'single_text',
'required' => true,
'attr' => array(
'class' => 'date-picker m-ctrl-medium',
'addon' => 'icon-calendar',
),
'constraints' => new Date()
))
->add('currency','choice',array(
'required' => true,
'choice_list' => $curreny_choices,
'constraints' => new Choice(array(
'choices' => $currency_entities
)),
))
->add('paymode','choice',array(
'required' => true,
'choice_list' => $paymode_choices,
'constraints' => new Choice(array(
'choices' => $paymode_entities
))
))
->add('subject','text',array(
'required' => false,
'attr' => array(
'class' => 'span8'
)
))
->add('comment','textarea',array(
'required' => false,
'attr' => array(
'class' => 'span8',
'rows' => 5
)
))
;
Note: I have added some use
statements at the top to keep the code clear.
http://symfony.com/doc/current/book/forms.html#adding-validation
Upvotes: 2