-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
[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
Comments
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);
} |
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 ? |
@jcrombez the issue in your case is that you are returning 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. |
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 ! ;) |
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
The error
Argument #1 being the Constraints Collection :
Am i doing something wrong or is the documentation out of date ?
The text was updated successfully, but these errors were encountered: