简体   繁体   English

当yii2中的验证器无法正常工作时

[英]When validator in yii2 not working properly

On one of my view file in project i am using when validator for defining some validation for a model but its not working properly may be because i dont know how to use that validator. 在我的一个view在项目文件中,我使用when验证定义了一些验证model ,但它不能正常工作可能是因为我不知道如何使用验证器。 Here is the model code of that table 这是该表的模型代码

       return [
        [['event_id', 'user_id'], 'required'],
        [['event_id', 'user_id'], 'integer'],
        [['is_mandatory'], 'boolean'],
        [['answer'], 'string', 'max' => 250],
        [['answer'], 'required', 'when' => function($model){
            return $model->is_mandatory == 1; }]
    ];

Here is my view code and yes i am creating this field on the view file with the instance because to create those fields depends on other inputs. 这是我的view代码,是的,我正在使用实例在视图文件上创建此字段,因为要创建这些字段取决于其他输入。

        $modelAnswers = new Answers();
        $modelAnswers->is_mandatory = 0;

        echo $form->field($modelAnswers, "answer",[
            'template' => ' <div class="row"><div class="col-lg-6 col-md-6 col-sm-6 col-xs-6"><label>{label}</label></div><div class="col-lg-4 col-md-4 col-sm-6 col-xs-8">{input}{error}{hint}</div></div>'
        ])->textInput(['maxlength' => true]);

But it always prints the field as required. 但是它总是根据需要打印该字段。 I can see in the debugger that $model is assigned the value correctly. 我可以在调试器中看到$model被正确分配了值。 Whats wrong with the validator here?? 这里的验证器怎么了?

The when() validation is only there for server-side validation, and will only evaluate when the form is submitted. when()验证仅用于服务器端验证,并且仅在提交表单时评估。 If you want to display the field as required or not depending on the value of is_mandatory then you have two options. 如果要根据is_mandatory的值显示是否需要显示该字段,则有两个选项。

Firstly, you can use the whenClient() method to add client-side validation. 首先,可以使用whenClient()方法添加客户端验证。 This is what I use on my own forms, and involves writing a javascript function that will determine if the field is required or not, and it will then apply the relvant classes. 这是我在自己的表单上使用的内容,涉及编写一个javascript函数,该函数将确定是否需要该字段,然后将应用相关的类。 To do this, you will need to include the is_mandatory field somewhere on your form, probably as a hidden field, and you wil need to enable clientValidation on the form. 为此,您将需要在表单上的某处包括is_mandatory字段,可能将其作为隐藏字段,并且需要在表单上启用clientValidation。

Secondly, you could simply adapt the template for the field so that the error messages are not included unless is_mandatory is set to true. 其次,您可以简单地为该字段调整模板,以便除非is_mandatory设置为true,否则不包括错误消息。 This will involve rewriting the field label. 这将涉及重写字段标签。

echo $form->field($modelAnswers, "answer",[
            'template' => ' <div class="row"><div class="col-lg-6 col-md-6 col-sm-6 col-xs-6"><label class="{$modelAnswers->is_mandatory ? 'required' : ''}">Name of label here</label></div><div class="col-lg-4 col-md-4 col-sm-6 col-xs-8">{input}{error}{hint}</div></div>'
        ])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM