0

在用户输入验证期间,我想将属性与值进行比较。

我有这个代码:

['ao_id', 'compare', 'when' => function($model) {
            return $model->lqp_id == 24 || $model->lqp_id == 26 || $model->lqp_id == 46;
        }, 'compareValue' => 50],

它有效(但仅在 时'enableClientValidation' => false),但是否有可能以某种方式显示外部属性的名称?因为如果用户收到外表面 (ao_id) 必须为 50 的错误消息,这并没有多大帮助。没有人知道这是什么意思,因为在下拉列表中您只看到名称而不是 ID。非常感谢!

4

2 回答 2

1

First of all, if you want your conditional validation to work on the client-side too (when enableClientValidation=>true), then add the whenClient property which contains the javascript code that will do the validation.

Second, you can use the message property to specify a custom validation error.

[
    'ao_id', 
    'compare', 
    'when' => function ($model) {
        return $model->lqp_id == 24 || $model->lqp_id == 26 || $model->lqp_id == 46;
    }, 
    'whenClient' => "function (attribute, value) {
        return $('#lqp_id').val() == '24' || $('#lqp_id').val() == '26' || $('#lqp_id').val() == '46';
    }", 
    'compareValue' => 50, 
    'message'=>'ao_id must be 50 when lqp_id is 24, 26 or 46'
]

Attention: be sure to check and change the id of the input field $('#lqp_id') as this is most likely different to my example.

于 2017-03-13T09:36:16.583 回答
0

message在您定义将显示的自己的消息而不是默认消息的位置添加键。

于 2017-03-13T07:24:42.773 回答