Hello, I'm playing with symfony2 and I have a question about the validation of a ChoiceField with a callback, as explained in http://docs.symfony-reloaded.org/guides/validator/constraints/Choice.html.
I have a domain model class with a getter returning an array of values to display in the form. The array looks like this: array { 'a' => 'The value A', 'b' => 'Some B value', '100' => 'Value 100'} When creating the ChoiceField in my form,i use this: $this->add('myfield', array('choices' => MyDomain::getValues())); Now, I don't know what is the best way to validate this field... I'd like to use the getValues method with something like this: /** * @validation:Choice(callback = "getValues") * @validation:NotBlank */ public $myfield = 'The value A'; 1) The problem is that in the array returned by getValues(), the validator doesn't look at the keys. What is the best way to make this work? Should I create a new getValuesKeys() method that would do a array_keys() of getValues() result ? Or something else? 2) In this case, when using array_keys(), the key '100' for 'Value 100' is automatically converted to a php int. But the choice validator expects the values to be of the same type (data from the post request is a php string), so I would have to convert all int keys to string in the getValuesKeys() method to make it work. Is there a better way? 3) Another point: the default value I put in $myfield is the displayed value ('The value A'). Is there a way to use the array keys instead ('a')? That would be useful when having a ChoiceField with lines with different keys but similar displayed values (if displaying a tree for example). In this situation for example: Category1 [optgroup] --The value A [key=1-a] Category2 [optgroup] --The value A [key=2-a] I'd be very happy if someone could help me! Thanks Anthony -- If you want to report a vulnerability issue on symfony, please send it to security at symfony-project.com You received this message because you are subscribed to the Google Groups "symfony users" group. To post to this group, send email to symfony-users@googlegroups.com To unsubscribe from this group, send email to symfony-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/symfony-users?hl=en