Skip to content

'validation_constraint' - Problem with Collections #3107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
DerStoffel opened this issue Jan 13, 2012 · 3 comments
Closed

'validation_constraint' - Problem with Collections #3107

DerStoffel opened this issue Jan 13, 2012 · 3 comments
Labels

Comments

@DerStoffel
Copy link

I created the following form where I remove a field for certain cases, but when I add it, i want it to be validated, but only then.
If I would add the assert to the entity it actually would validate it even if it is not added by the validator, since the entity would
not match its "notBlank" assert.

public function buildForm(FormBuilder $builder, array $options) {
        $builder->add('start', 'datetime', array('date_widget' => 'single_text', 'time_widget' => 'single_text'));
        $builder->add('end', 'datetime', array('date_widget' => 'single_text', 'time_widget' => 'single_text'));
        $builder->add('journeytime');
        $builder->add('content');
        if (count($this->secondaryCategories) > 0) {
            $builder->add('secondaryCategory', 'entity', array('class' => 'ACMEContactBundle:SecondaryCategory', 'empty_value' => 'Bitte auswählen...'));
        }
        $builder->add('initiator', 'entity', array('class' => ACME\\MasterdataBundle\\Entity\\PartyInstance',
            'query_builder' => $this->qb
        ));
    }

public function getDefaultOptions(array $options) {
        $collectionConstraint = new Collection(array(
                    'secondaryCategory' => new Assert\NotBlank()
                ));

        if (count($this->secondaryCategories) > 0) {
            return array(
                'data_class' => 'ACME\ContactBundle\Entity\Visit',
                'validation_constraint' => $collectionConstraint,
            );
        }

        return array(
            'data_class' => 'ACME\ContactBundle\Entity\Visit',
        );
    }

In general the form works, except, when I get to the validation, I get the following error:
Even though I felt, I followed the symfony2 docs (http://symfony.com/doc/current/book/forms.html#adding-validation).

Expected argument of type array or Traversable and ArrayAccess, object given 

Bug or Stupidity? :)
Need help there.

@sebhoerl
Copy link
Contributor

As said in the forum you can only validate array-like values with a CollectionConstraint (and not objects).
I added #3114 to provide support for dynamic constraints, which might be the solution for that problem in the future.
But there is another solution for your problem: You could define a CallbackConstraint and validate the field manually. I'll post an example in the forum

@webmozart
Copy link
Contributor

As said, the Collection constraint can not be used to validate normal objects. You should use validation groups to control which constraints are applied on your object.

@DerStoffel
Copy link
Author

Already solved it by actually doing exactly what you proposed, I created a custom validation group. Thx for your input.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants