Take the 2-minute tour ×

I am using CraueFormFlowBundle to have a multiple page form, and am trying to do some validation on some of the fields but can't figure out how to do this. The object that needs to be validated isn't an Entity, which is causing me trouble.

I tried adding a collectionConstraint in the getDefaultOption function of my form type class, but this doesn't work as I get the "Expected argument of type array or Traversable and ArrayAccess" error. I tried with annotations in my object class, but they don't seem to be taken into account. Are annotations taken into account if the class isn't an entity? (i set enable_annotations to true)

Anyway, what is the proper way to do this? Basically, I just want to validate that "age" is an integer...

class PoemDataCollectorFormType extends AbstractType {
    public function buildForm(FormBuilder $builder, array $options) {
        switch ($options['flowStep']) {
            case 6:
            $builder->add('msgCategory', 'hidden', array(
            $builder->add('msgFIB','text', array(
                'required' => false,
            $builder->add('age', 'integer', array(
                'required' => false,
    public function getDefaultOptions(array $options)
        $options = parent::getDefaultOptions($options);
        $options['flowStep'] = 1;
        $options['data_class'] = 'YOP\YourOwnPoetBundle\PoemBuilder\PoemDataCollector';
        $options['intention'] = 'my_secret_key';

        return $options;

EDIT: add code, handle validation with annotations

As Cyprian, I was pretty sure that using annotations should work, however it doesn't... Here is how I try:

In my Controller:

public function collectPoemDataAction() {
    $collector = $this->get('yop.poem.datacollector');
    $flow = $this->get('yop.form.flow.poemDataCollector');
    $form = $flow->createForm($collector);
    if ($flow->isValid($form)) {

In my PoemDataCollector class, which is my data class (service yop.poem.datacollector):

class PoemDataCollector
     * @Assert\Type(type="integer", message="Age should be a number")

    private $age;


Here is the services implementation: The data class (PoemDataCollector) seems to be linked to the flow class and not to the form.. Is that why there is no validation?

    <service id="yop.poem.datacollector" class="YOP\YourOwnPoetBundle\PoemBuilder\PoemDataCollector">
    <service id="yop.form.poemDataCollector"
        <tag name="form.type" alias="poemDataCollector" />
    <service id="yop.form.flow.poemDataCollector"
        <call method="setFormType">
            <argument type="service" id="yop.form.poemDataCollector" />

How can I do the validation while respecting the craueFormFlowBundle guidelines?

The guidelines state:

Validation groups
To validate the form data class a step-based validation group is passed to the form type. By default, if getName() of the form type returns registerUser, such a group is named flow_registerUser_step1 for the first step.

Where should I state my constraint to use those validation groups..?

I tried:

          - MinLength: { limit: 5, message: "Your name must have at least {{ limit }} characters.", groups: [flow_poemDataCollector_step1] }
          - Type:
               type: integer
               message: Please input a number
               groups: [flow_poemDataCollector_step6]

But it is not taken into acount.


OK, much better, validation on name seems to be working with:

 * @Assert\MinLength(limit=5, groups={"flow_poemDataCollector_step1"}, message="Your name must have at least {{ limit }} characters.")
private $name;

Although the message doesn't appear... I have put {{ form_errors(form.name) }} and {{ form_errors(form) }} in my template... Why doesn't it show?


 * @Assert\Type(type="integer", groups={"flow_poemDataCollector_step6"}, message="Age should be a number")

private $age;

isn't working at all...


Allright, I managed to have my validation working now.. But I can't find a way to display the error messages. Aren't they supposed to show when using:

{{ form_errors(form.name) }}


{{ form_errors(form) }}

in my template? In my case, when validation fails, i return to the same step in my form but no error message is shown. Besides, $form->getErrors() returns an empty array! How to handle errors?

share|improve this question

3 Answers 3

up vote 0 down vote accepted

So here is what was happening: i wasn't rendering the same instance of the form as the one being validated...

I was rendering my page with 'form' => $flow->createForm($collector)->createView(), changing it to 'form' => $form->createView() solved my issue: error messages are now rendered.

share|improve this answer

You should define constrains in class related to the form. This class doesn't have to be an entity.

An example which should work:

// your data class:
use Symfony\Component\Validator\Constraints as Assert;

class DataClass
     * @Assert\Type(type="integer", message="Age should be an integer")
    public $age;



Moreover you need pass your data class object to form. Eg.

$dataClassObject = new DataClass();
$form = $this->createForm(new SomeType(), $dataClassObject); // here is place where you put your data class object into form

// now validation should work
if ($form->isValid()) { 
share|improve this answer
Yes I tried this and was pretty sure that it should work! However it doesn't... I edited my question with some more code to show how i did this –  Marronsuisse Sep 19 '12 at 1:55
as far as I can't see your services implementation, I've posted some more abstract example. All in all i guess the problem is that because you didn't pass data class object to your form. –  Cyprian Sep 19 '12 at 7:15
I will edit my post with the service implementation. I am using this CraueFormFlowBundle so I have to follow their guidelines in the Controller..(link in my post) They are talking about step based validation but I can't understand how it works and can't find examples... –  Marronsuisse Sep 20 '12 at 2:44
1) class PoemDataCollector shouldn't be a service, because it's a resource (this is your data class). 2) Are you sure that you specified right group name: "flow_poemDataCollector_step6" ? Pay attention on "six" at the end of name. In my understanding - this constraint should work in sixth step only then, right? Is it your intention? –  Cyprian Sep 20 '12 at 7:08
1)I replaced the $collector = $this->get('yop.poem.datacollector'); with $collector = new PoemDataCollector();. I have to keep the service in services.xml though as other services use this one as argument. 2) Yes this constraint should apply in step 6. I'm still wondering why the MaxLength, MinLength constraints are working but not the Type one. Besides, still can't have the messages to display. –  Marronsuisse Oct 22 '12 at 3:18

I haven't tested this so I'm not sure if it'll work, but you may be able to do something like the following inside your buildForm method.

    $builder->addValidator(new \Symfony\Component\Form\CallbackValidator(function(FormInterface $form) {
        $data = $form->getData();
        if (!is_int($data['age'])) {
            $form->addError(new \Symfony\Component\Form\FormError('Age must be an integer'));
share|improve this answer

Your Answer


