我使用过dynamic form widget。表单域显示在下图中。如您所见,有一个名为cancel的复选框。我想要的是如果单击cancel
复选框,它只需要支票号,并允许其余为空。如果不使用动态表单,我可以使用when
和whenClient
验证程序轻松实现此功能,因为我可以获得复选框的name
。
这里的问题是动态表单为复选框生成了这种name
系列...
TblDvBub[0][is_cancelled][]
TblDvBub[1][is_cancelled][]
TblDvBub[2][is_cancelled][]
答案 0 :(得分:2)
我认为你可以使用'whenClient'=>中的'attribute.name'来提取is_cancelled复选框的名称。 'function(attribute,value){}'参数。 console.log这个'属性' - 必须有一个带'name'属性的对象 - 你可以得到当前TblDvBub的数字(使用正则表达式)。 顺便说一下为什么你使用多个is_cancelled []字段 - 它不属于特定的TblDvBub子阵列吗?
答案 1 :(得分:-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")
}"
?>