douchen4915 2011-02-04 09:33
浏览 20
已采纳

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

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

Action:

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

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

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

1条回答 默认 最新

  • douzhenchun6782 2011-02-04 14:44
    关注

    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!'))
    ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答