YII2将whenClient添加到动态窗体
我用过。表单字段如下图所示。如您所见,有一个名为“取消”的复选框。我想要的是,如果单击YII2将whenClient添加到动态窗体,yii2,dynamicform,Yii2,Dynamicform,我用过。表单字段如下图所示。如您所见,有一个名为“取消”的复选框。我想要的是,如果单击取消复选框,它将只需要支票号码,并允许剩余的为空。如果不使用动态表单,我可以使用when和when-client验证器轻松实现这一点,因为我可以准确地获得复选框的名称 这里的问题是,动态表单为复选框生成这种name系列 TblDvBub[0][is_cancelled][] TblDvBub[1][is_cancelled][] TblDvBub[2][is_cancelled][] 我认为您可以使用'whe
取消
复选框,它将只需要支票号码,并允许剩余的为空。如果不使用动态表单,我可以使用when
和when-client
验证器轻松实现这一点,因为我可以准确地获得复选框的名称
这里的问题是,动态表单为复选框生成这种name
系列
TblDvBub[0][is_cancelled][]
TblDvBub[1][is_cancelled][]
TblDvBub[2][is_cancelled][]
我认为您可以使用'whenClient'=>'函数(属性,值){}参数中的'attribute.name'提取被取消复选框的名称。console.log该“属性”-必须有一个具有“name”属性的对象-在那里,您可以获得当前TblDvBub的编号(使用regex)。
顺便问一下,为什么要使用多个is_cancelled[]字段-它不是已经属于特定的TblDvBub子数组吗?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',
]);
}
}
?>
是的,你是对的。我使用属性提取名称。name
。谢谢。回答得好。谢谢
<?php
'whenClient' => "function(attribute, value, form) {
$("form# " + form + " > attribute")
}"
?>