Cakephp v2.4.1,验证一组单独的单选按钮 - 需要强制检查一个


Cakephp v2.4.1, validation of group of separate radio buttons - need to enforce that one is checked

我有四个单独的单选按钮,如下所示,我需要在模型中进行验证,我使用了一些jquery只允许使用一个按钮,这有效,但无法弄清楚如何在模型中进行验证如果没有选中按钮,任何帮助都会真正拯救我的脖子:

$options = array('A1' => 'A1' );
$attributes = array(
 'legend'=>False ,
 'label'=>true,
 'class'=>'g' , 
 //'before'=>'',
 //'after'=>'',
 'separator'=>'' );
echo $this->Form->radio('a1', $options, $attributes); 

$options = array('A2' => 'A2' );
$attributes = array(
 'legend'=>False,
 'label'=>true, 
  'class'=>'g' , 
 //'before'=>'',
 //'after'=>'',
 'separator'=>'' );
echo $this->Form->radio('a2', $options, $attributes); 

$options = array('B1' => 'B1' );
$attributes = array(
 'legend'=>False,
 'label'=>true, 
  'class'=>'g' , 
 //'before'=>'',
 //'after'=>'',
 'separator'=>'' );
echo $this->Form->radio('b1', $options, $attributes); 

$options = array('B2' => 'B2' );
$attributes = array(
 'legend'=>False,
 'label'=>true, 
  'class'=>'g' , 
 //'before'=>'',
 //'after'=>'',
 'separator'=>'' );
echo $this->Form->radio('b2', $options, $attributes); 

我不明白你为什么要这样做,因为单选按钮的全部意义在于一次只点击一个选项。不需要JS或任何东西。我会只传递一个带有 4 个选项的Form::radio,如果它必须是不同的字段,我会在我的控制器中处理输入。

但是,如果您想像计划的那样执行此操作,则需要自定义验证规则。制定一个新规则,该规则将从$this->data中检查所有上述字段,并且仅在仅设置其中一个字段时才返回 true。

相关文章: