Question

sfValidatorChoice is not working on multiple select element, my code

$this->form=new MyTestForm();
$options_array=array("php","python","java");
$widgetSchema["my_select"] =new sfWidgetFormChoice(array('choices'  =>  $options_array,'multiple' => true,'expanded' => true ));
                $validatorSchema["my_select"] = new sfValidatorChoice(array("choices" =>array_keys($options_array)));

Note: i have also tried using array_keys and by directly passing the array to sfValidatorChoice.

when i submit, it gives me Invalid error(when checked) and Required(when unchecked). is there any error in parameters or is bug?

Was it helpful?

Solution

Firstly, you need to enable "multiple" in the validator as well as the widget:

"multiple" => true

To make having any selection optional, you need to set required to false:

"required" => false

Finally, I can't exactly remember how to use sfValidatorChoice (it's been a while), but I think it's best to make the values readable, so I'd do:

$options_array=array('php'=>'php','python'=>'python','java'=>'java');

I'm not certain this will fix the problem, but it may well do.

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