Skip to content

[Form] Adding constraints to a non-entity related form fails #896

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
jcrombez opened this issue Dec 1, 2011 · 4 comments
Closed

[Form] Adding constraints to a non-entity related form fails #896

jcrombez opened this issue Dec 1, 2011 · 4 comments

Comments

@jcrombez
Copy link
Contributor

jcrombez commented Dec 1, 2011

Hello,

I think i found a problem in Symfony Form (2.0.6).

First : http://symfony.com/doc/2.0/book/forms.html#adding-validation

In the example, there is no return $options, it was my first problem in trying to add constraints to my form. I'm not sure of the whole correction so i didn't pull-requested anything in the symfony-doc repo yet.

Second, a more serious problem (i guess it's not really a documentation issue, but still), when FormFactory process the options, it failed to array_replace the Constraints Collection :

The code

public function getDefaultOptions(array $options)
{
    $collectionConstraint = new Collection(array(
        'time' => new Choice(array(
                'choices' => $this->times
        ))
    ));

    return $options['validation_constraint'] = $collectionConstraint;
}

The error

Warning: array_replace(): Argument #1 is not an array in /path/to/my/project/vendor/symfony/src/Symfony/Component/Form/FormFactory.php line 236 

Argument #1 being the Constraints Collection :

at array_replace (object(Collection), array('data' => ...

Am i doing something wrong or is the documentation out of date ?

@stof
Copy link
Member

stof commented Dec 1, 2011

you issue is the way you return: you don't return the whole array but only the item you are setting.

And btw, you only need to return the default options, not to do the merge with the options (as it will be done afterwards anyway) so it could simply be

<?php
public function getDefaultOptions(array $options)
{
    $collectionConstraint = new Collection(array(
        'time' => new Choice(array(
                'choices' => $this->times
        ))
    ));

    return array('validation_constraint' => $collectionConstraint);
}

@jcrombez
Copy link
Contributor Author

jcrombez commented Dec 1, 2011

I understand why i'm supposed to return a new array but i don't understand why this avoid array_replace to merge the Constraints Collection. Wasn't i previously already returning my collection at the same place of the array ? Why returning a new array fix this problem ?

@stof
Copy link
Member

stof commented Dec 1, 2011

@jcrombez the issue in your case is that you are returning $options['validation_constraint'] instead of returning the array $options, and so the code merging the default options with the options will then fail as your default options are not an array.

Returning an array with just the real default options instead of returning the options passed by the user as default options is a cleaner way to do this, as it avoids breaking the validation of options: the FormFactory will validate that the options passed are allowed. As you are returning the options passed as default (adding one option), every option will now be able to pass the validation.

@jcrombez
Copy link
Contributor Author

jcrombez commented Dec 1, 2011

Ok i see, i didn't understand the difference regarding the array_replace error because i was sure to return $options but i wasn't. It's clear now. Thank you ! ;)

@jcrombez jcrombez closed this as completed Dec 1, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants