Yii2 自定义验证器未按指南建议工作 [英] Yii2 custom validator not working as guide suggests

查看:27
本文介绍了Yii2 自定义验证器未按指南建议工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天在这里搜索了一段时间,但我无法理解为什么我的验证器在我的模型中不起作用.

我在下面的代码中有一个带有模型页面"的模块.

我需要使用 2 个属性来验证模型.它们是 hero_link 和 hero_linked.如果 hero_linked 为真,我想要求 hero_link.

在指南这里中,他们解释这种验证器的正确语法

我在我的模型中使用了这个语法,但它没有像我期望的那样验证.我还添加了 whenClient 属性,所以我可以在这里使用客户端验证.

我的相关代码如下:

//页面模型命名空间 commonmodulespagemodels;使用 Yii;类 Page 扩展 yiidbActiveRecord{公共函数规则(){返回 [[['hero_linked',], 'boolean'],[['hero_linked',], 'required'],[['hero_link',], 'string', 'max' =>255],['英雄链接','必需的','当' =>功能($模型){返回 $model->hero_linked == true;},'whenClient' =>功能(属性,值){返回 $('#page-hero_linked').val() == '1';}",],];}}//_form 视图<?php $form = ActiveForm::begin();?><?= $form->field($model, 'hero_linked')->checkbox() ?><?= $form->field($model, 'hero_link')->textInput(['maxlength' => 255]) ?><?php ActiveForm::end();?>

所需的验证器已正确应用,但它似乎忽略了 when 属性,并且即使我的复选框未选中也需要.我的语法不正确,还是我遗漏了一个要求?任何帮助表示赞赏,提前致谢!

解决方案

我想这就是你想要的.首先使用输入字段的whenClient中的属性checked"来获取checkbox是否被选中.

复选框的隐藏字段不是问题.

hero_linked(复选框)中的 whenClient 用于在复选框更改时验证 hero_link(文本输入)字段.因此,您可以立即查看是否需要 hero_link(文本输入).

如果您使用此规则,您的表单中不需要任何额外的 js 代码.

但是正如您可能还看到的那样,由于完整的名称page-something",这实际上仅适用于page"模型,我猜也取决于表单名称.因此,您应该对 form.php 中的两个字段使用其他一些固定的id",这样如果您将页面模型扩展到另一个类,这也适用.

<预><代码>['hero_linked', '必需','whenClient' =>功能(属性,值){$('form').yiiActiveForm('validateAttribute', 'page-hero_link');返回真;}",],[['hero_link',], 'string', 'max' =>255],['hero_link', 'required', 'when' =>功能($模型){返回 $model->hero_linked == true;},'whenClient' =>功能(属性,值){return $('#page-hero_linked').prop('checked');}",],

I've searched around here for some time today, but I'm unable to understand why my validators aren't working in my model.

I have a Module with a model "Page" in my code below.

I have 2 attributes that I need to use to validate the model. They are hero_link and hero_linked. If hero_linked is true, I want to require hero_link.

In the guide here, they explain the proper syntax for this kind of validator

I have used this syntax in my model, but it doesn't validate as I'd expect. I've added the whenClient property as well, so I can use client side validation here.

my relevant code is below:

// Page model
namespace commonmodulespagemodels;

use Yii;

class Page extends yiidbActiveRecord
{
    public function rules()
    {
        return [
            [['hero_linked',], 'boolean'],
            [['hero_linked',], 'required'],
            [['hero_link',], 'string', 'max' => 255],
            [
                'hero_link', 
                'required', 
                'when' => function($model) {
                    return $model->hero_linked == true;
                },
                'whenClient' => "function (attribute, value) {
                    return $('#page-hero_linked').val() == '1';
                }",
            ],
        ];
    }
}

// _form view 
<?php $form = ActiveForm::begin(); ?>
    <?= $form->field($model, 'hero_linked')->checkbox() ?>
    <?= $form->field($model, 'hero_link')->textInput(['maxlength' => 255]) ?>
<?php ActiveForm::end(); ?>

The required validator is correctly applied, but it seems to be ignoring the when property, and requiring even though my checkbox is unchecked. Is my syntax incorrect, or am I missing a requirement? Any help is appreciated, Thanks in advance!

解决方案

I guess this is what you want. First use the property "checked" in the whenClient of the input field to get if the checkbox is checked or not.

The hidden field of the checkbox is not an issue.

The whenClient in hero_linked (checkbox) is used to validate the hero_link (textinput) field on change of the checkbox. So you instantly see if the hero_link (textinput) is required or not.

You don't need any additional js code in your form if you use this rules.

But as you might also see that due to the complete name "page-something" this is really fixed to only the "page" model and i guess also depending on the form name. So you should use some other fixed "id's" for both fields in the form.php so this also would also work if you extend your page model to another class.

[
    'hero_linked', 'required',
    'whenClient' => "function (attribute, value) {
        $('form').yiiActiveForm('validateAttribute', 'page-hero_link');
        return true;
    }",
],
[['hero_link',], 'string', 'max' => 255],
[
    'hero_link', 'required', 'when' => function ($model) {
        return $model->hero_linked == true;
    },
    'whenClient' => "function (attribute, value) {
        return $('#page-hero_linked').prop('checked');
    }",
],

这篇关于Yii2 自定义验证器未按指南建议工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆