Question

I have a form in symfony 2 like:

$form = $this->createFormBuilder();

        $form
            ->add('subscription', 'entity', array(
                'class' => 'AcmeDemoBundle:Subscription',

                'property'              => 'name',
                'label'                 => 'Subscription',
                'cascade_validation'    => false,
                'constraints'           => array(
                    new NotBlank(),
                )
            ))

This is failing validation with the error:

subscription: ERROR: This value should be of type integer. ERROR: This value should be of type integer.

The problem is I don't want to cascade the validation to the Subscription entity. I just want to be able to select the entity from a dropdown.

Any idea?

Was it helpful?

Solution

The reason you are getting those error messages is because you're failing type validation on one or more properties of the child entity. Review those constraints wherever you've defined them. In my case, this error was triggered when I assigned a "Type()" constraint on properties that were allowed to be NULL. Removing the type constraints eliminated the errors.

Regarding validation of child objects, this should only happen when you assign a "Valid" constraint on a property within the parent class, based on my reading of the documentation. However, it also appears to be controlled by the cascade_validation field defined in the setDefaultOptions() method of the related AbstractType form type class, which you can also override by passing it in via the $options array at the time you instantiate the form object:

$form = $this->createForm(
    $formType,
    $formModel,
    array('cascade_validation' => false)
);

In your case, the cascade_validation setting you've defined only applies to the properties of the Subscription child of your form object, where I think you're trying to apply the validation setting to the class itself (the class that has a Subscription object as one of its properties). So change you form builder instance to this:

$form = $this->createFormBuilder(null, array('cascade_validation' => false));

Alternatively, you can explicitly define the fields that you want validated within the controller as illustrated in the symfony2 documentation as follows:

use Symfony\Component\Validator\Constraints\Email;

public function addEmailAction($email)
{
    $emailConstraint = new Email();
    // all constraint "options" can be set this way
    $emailConstraint->message = 'Invalid email address';

    // use the validator to validate the value
    $errorList = $this->get('validator')->validateValue(
        $email,
        $emailConstraint
    );

    if (count($errorList) == 0) {
        // this IS a valid email address, do something
    } else {
        // this is *not* a valid email address
        $errorMessage = $errorList[0]->getMessage();

        // ... do something with the error
    }

    // ...
}

Reference documentation on symfony2 validation

OTHER TIPS

Just hit the "This value should be of type integer." error myself in an entity field. Turned out to be a bad validator. Look at the validation you've got inside of your entity's class, specifically any integer type validators and ensure what they're validating should indeed be of type integer.

In my case bad documentation on a property led me to mistakenly apply an integer type validation constraint on the property when in fact it was set up to hold an object. I think this entity class validation kicks in on entity fields because you're technically submitting the whole entity - not just a reference to it. So, if the data stored in the chosen entity doesn't match up to what the entity class's validators require, you get the error.

Fix your entity class's validators and you should be good to go.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top