views:

49

answers:

1

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?

+1  A: 

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.

lonesomeday
yeah! thank u @lonesomeday it worked!
Harish Kurup
the problem was, i didnt set "multiple" => true in sfValidatorChoice, the "choices" =>$options_array. hence it was not working as desired...
Harish Kurup