2011-02-04 84 views
2

我有一個奇怪的問題與複選框驗證。它總是無效...我已經閱讀了很多關於這個問題,但我無法找到解決辦法......(我用array_keys的驗證) 所以,這裏是我的代碼:Symfony sfWidgetFormChoice總是無效的多選(複選框)

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

回答

5

當指定的窗口小部件選項「多」,你應該相應的驗證器做同樣的:

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

謝謝!作品! – kungfucsiga 2011-02-14 07:56:53