Symfony sfWidgetFormChoice始终无效,有多个选项(复选框)

时间:2011-02-04 09:33:11

标签: php symfony1 validation

我的复选框验证有一个奇怪的问题。它总是无效的...我已经阅读了很多关于这个问题,但我找不到解决方案......(我在验证中使用array_keys) 所以,这是我的代码:

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' => 'Please choose something!'))
        ));

        $this->widgetSchema->setLabels(array(

            'devices' => ' '
        ));

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

动作:

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

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

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

1 个答案:

答案 0 :(得分:6)

在窗口小部件选项中指定“倍数”时,您应该对相应的验证器执行相同操作:

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