2015-06-23 76 views
2

我已经使用了dynamic form widget。表单域显示在下图中。如您所见,有一个名为cancel的复选框。我想要的是如果cancel复选框被点击,它只会需要检查号码,并允许其余为空。如果不使用动态表单,我可以使用whenwhenClient验证程序轻松实现此功能,因为我可以完全确定复选框的nameYII2将whenClient添加到动态表单

的这里的问题是,动态表单生成这种name系列的复选框...

TblDvBub[0][is_cancelled][] 
TblDvBub[1][is_cancelled][] 
TblDvBub[2][is_cancelled][] 

enter image description here

回答

2

我想你可以使用“属性提取is_cancelled复选框的名称。 name'from'whenClient'=>'function(attribute,value){}'参数。 console.log'属性' - 必须有一个'name'属性的对象 - 你可以得到当前TblDvBub的数字(使用正则表达式)。 顺便说一句,为什么你使用多个is_cancelled []字段 - 它不是已经属于特定的TblDvBub子阵列?

+0

是你对。我使用'attribute.name'提取名称。谢谢。 – beginner

+0

很好的答案..谢谢 –

-1

1)在形式,必须重写fieldClass

<?php $form = ActiveForm::begin([ 
    'fieldClass' => 'backend\widgets\ActiveField' 
]); ?> 

2)覆盖的方法

<?php 
class ActiveField extends \yii\widgets\ActiveField 
{ 
    protected function getClientOptions() 
    { 
     $attribute = Html::getAttributeName($this->attribute); 
     if (!in_array($attribute, $this->model->activeAttributes(), true)) { 
      return []; 
     } 
     $enableClientValidation = $this->enableClientValidation || $this->enableClientValidation === null && $this->form->enableClientValidation; 
     $enableAjaxValidation = $this->enableAjaxValidation || $this->enableAjaxValidation === null && $this->form->enableAjaxValidation; 
     if ($enableClientValidation) { 
      $validators = []; 
      foreach ($this->model->getActiveValidators($attribute) as $validator) { 
       /* @var $validator \yii\validators\Validator */ 
       $js = $validator->clientValidateAttribute($this->model, $attribute, $this->form->getView()); 
       if ($validator->enableClientValidation && $js != '') { 
        if ($validator->whenClient !== null) { 
         $js = "if (({$validator->whenClient})(attribute, value, '{$this->form->id}')) { $js }"; 
        } 
        $validators[] = $js; 
       } 
      } 
     } 
     if (!$enableAjaxValidation && (!$enableClientValidation || empty($validators))) { 
      return []; 
     } 
     $options = []; 
     $inputID = $this->getInputId(); 
     $options['id'] = $inputID; 
     $options['name'] = $this->attribute; 
     $options['container'] = isset($this->selectors['container']) ? $this->selectors['container'] : ".field-$inputID"; 
     $options['input'] = isset($this->selectors['input']) ? $this->selectors['input'] : "#$inputID"; 
     if (isset($this->selectors['error'])) { 
      $options['error'] = $this->selectors['error']; 
     } elseif (isset($this->errorOptions['class'])) { 
      $options['error'] = '.' . implode('.', preg_split('/\s+/', $this->errorOptions['class'], -1, PREG_SPLIT_NO_EMPTY)); 
     } else { 
      $options['error'] = isset($this->errorOptions['tag']) ? $this->errorOptions['tag'] : 'span'; 
     } 
     $options['encodeError'] = !isset($this->errorOptions['encode']) || $this->errorOptions['encode']; 
     if ($enableAjaxValidation) { 
      $options['enableAjaxValidation'] = true; 
     } 
     foreach (['validateOnChange', 'validateOnBlur', 'validateOnType', 'validationDelay'] as $name) { 
      $options[$name] = $this->$name === null ? $this->form->$name : $this->$name; 
     } 
     if (!empty($validators)) { 
      $options['validate'] = new JsExpression("function (attribute, value, messages, deferred, \$form) {" . implode('', $validators) . '}'); 
     } 
     // only get the options that are different from the default ones (set in yii.activeForm.js) 
     return array_diff_assoc($options, [ 
      'validateOnChange' => true, 
      'validateOnBlur' => true, 
      'validateOnType' => false, 
      'validationDelay' => 500, 
      'encodeError' => true, 
      'error' => '.help-block', 
     ]); 
    } 
} 
?> 

3)验证可用于

<?php 
'whenClient' => "function(attribute, value, form) { 
        $("form# " + form + " > attribute") 
       }" 
?>