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

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

问题描述

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

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.

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

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

我在模型中使用了这种语法,但是它没有像我期望的那样进行验证.我还添加了whenClient属性,因此可以在此处使用客户端验证.

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.

我的相关代码如下:

// Page model
namespace common\modules\page\models;

use Yii;

class Page extends \yii\db\ActiveRecord
{
    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(); ?>

所需的验证器已正确应用,但它似乎忽略了when属性,即使我的复选框未选中,它也需要.我的语法不正确,还是我缺少要求?任何帮助表示赞赏,在此先感谢!

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!

推荐答案

我想这就是您想要的. 首先,在输入字段的whenClient中使用属性"checked",以获取是否选中该复选框.

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.

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

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.

如果使用此规则,则表单中不需要任何其他js代码.

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

但是您可能还会看到,由于全名"page-something",实际上仅固定于"page"模型,我想这也取决于表单名称.因此,您应该在form.php的两个字段中都使用其他固定的"id",因此如果将页面模型扩展到另一个类,这也将起作用.

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天全站免登陆