开发者

Symfony sfWidgetFormChoice always invalid with multiple choices (checkboxes)

I have a weird problem with checkbox validation. It's always invalid... I have read a lot about this problem, but I couldn't find the solution... (I use array_keys in validation) So, here is my code:

class NetworkDevicesAndInterfacesForm extends sfForm {

    public function configure() {

        $optionsArr = array('one' => 'One','two' => 'Two');

        $this->setWidgets(array(

            'devices' => new sfWidgetFormChoice(array(
                         'expanded' => true,
                         'multiple' => true, 
                         'choices' => $optionsArr),
             array('class' => 'checkbox'))
        ));

        $this->setValidators(array(

            'devices' => new sfValidatorChoice(array(
                         'choices' => array_keys($optionsArr)),
            array('required' => 'Pl开发者_开发知识库ease choose something!'))
        ));

        $this->widgetSchema->setLabels(array(

            'devices' => ' '
        ));

        $this->widgetSchema->setNameFormat('devices[%s]');
    }
}

Action:

if ($request->isMethod('post')) {

    $this->form->bind($request->getParameter('devices'));
    if ($this->form->isValid()) {

         $formValues = $this->form->getValues();
         $deviceId = $formValues['devices'];
    }
}


When specifying 'multiple' in the widget options, you should do the same for the corresponding validator:

$this->setValidators(array(
    'devices' => new sfValidatorChoice(array(
        'choices' => array_keys($optionsArr),
        'multiple' => true
    ),
    array('required' => 'Please choose something!'))
));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜