简体   繁体   English

如何自定义错误消息以在Yii中添加ID?

[英]How to customise error message to add ID in Yii?

Currently I am using $form->error($user_model,'password') this function to show error message which gives me output as <div class="errorMessage">Please enter current password</div> but I want to add id in same div. 目前,我正在使用$form->error($user_model,'password')此函数显示错误消息,该错误消息为<div class="errorMessage">Please enter current password</div>但我想添加id在同一个div。 What changes I have to do for that? 为此,我需要做哪些更改?

To customize error message you have to go to user model and change/add your message to function rules() array. 要自定义错误消息,您必须转到用户模型,然后将消息更改/添加到function rules()数组。

function rules() {
   return [
      ['password', 'required', 'message' => 'Your custom message'],
   ];
}

Check CRequiredValidator class for more information. 检查CRequiredValidator类以获取更多信息。


To apply some custom ID: 要应用一些自定义ID:

$form->error($error_model, 'password', ['id' => 'My-super-custom-id']);

just it will break your error messages output when using yii-js methods. 只是使用yii-js方法时,它会破坏您的错误消息输出。

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

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