Symfony sfWidgetFormChoice 总是无效的多项选择(复选框) [英] Symfony sfWidgetFormChoice always invalid with multiple choices (checkboxes)

查看:31
本文介绍了Symfony sfWidgetFormChoice 总是无效的多项选择(复选框)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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' => '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'];
    }
}

推荐答案

在小部件选项中指定 'multiple' 时,您应该对相应的验证器执行相同的操作:

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!'))
));

这篇关于Symfony sfWidgetFormChoice 总是无效的多项选择(复选框)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆