简体   繁体   English

如何为同一字段自定义不同的错误消息?

[英]How to customize different error messages for the same field?

I have the following field that uses it's own regular expression to validate: 我有以下使用它自己的正则表达式进行验证的字段:

<input type="text" name="first-name" class="form-control" id="first-name" placeholder="First Name" 
       data-parsley-trigger="change" required data-parsley-alpha data-parsley-pattern="^[A-Za-z]*$"/>

The field has multiple validation failure states, and I want each of those states to have a different error message. 该字段具有多个验证失败状态,我希望每个状态都具有不同的错误消息。

For example, if the user enters a space, I want the error message to say "No spaces allowed", but if it has a numeric character, I want the error message to say "No numbers allowed". 例如,如果用户输入空格,我希望错误消息说“不允许空格”,但是如果它有数字字符,我希望错误消息说“不允许数字”。

I've tried to figure out how to do this by reading the docs, but I'm still confused on how to achieve this. 我试图通过阅读文档来弄清楚如何做到这一点,但是我仍然对如何实现这一目标感到困惑。

I'm using Parsley 2.0.0-rc4 我正在使用Parsley 2.0.0-rc4

Yeah, this is not something easily doable with Parsley. 是的,这对于欧芹而言并非易事。 Each validator have an unique error message. 每个验证器都有一个唯一的错误消息。

If you want to do so, for UI/UX purposes, you may have two possibilities: 如果要这样做,出于UI / UX的目的,您可能有两种可能:

1) You'll need to define some custom validators of yours, and their related messages. 1)您需要定义自己的一些自定义验证器及其相关消息。

In your example: 在您的示例中:

  • create a validator nospaces and its message with 66 priority 创建一个验证器nospaces及其优先级为66的消息
  • create a validator nonumbers and its message with a 65 priority 创建一个验证器nonumbers及其优先级为65的消息
  • still use your pattern validator (64 priority) and eventually change its message by something like 'only alphanum allowed' 仍然使用您的pattern验证器(优先级为64),并最终通过“仅允许字母数字”之类的方式更改其消息

Add then these 3 validators to your input, and depending on their respective priority they would be fired in the right order to display the right error message you want as described in your question. 然后将这3个验证器添加到您的输入中,并根据它们各自的优先级将它们按正确的顺序触发,以显示您想要的正确错误消息,如问题中所述。

Pros: easily reusable 优点:易于重用
Cons: some work is needed 缺点:需要做一些工作

2) Keep your pattern validator, and bind a custom function to the parsley:field:error event for this input, and do your check to display the right error message you want for this field, and not the default one 2)保留您的模式验证器,并将自定义函数绑定到此输入的parsley:field:error事件,并进行检查以显示所需的正确错误消息,而不是默认字段

Pros: less work maybe, in a single function 优点:单个功能可能工作更少
Cons: not much reusable 缺点:可重用性不高

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

相关问题 将相同字段的错误消息放在不同的位置jquery中 - Place Error Messages for the same field in different Locations jquery jQuery验证同一字段的条件错误消息 - jQuery Validate conditional error messages for same field 使用JavaScript自定义Stripe错误消息 - Customize Stripe error messages with JavaScript 如何全局自定义错误消息 vee-validate - How to globally customize error messages vee-validate 如何使用不同的错误消息验证这两个字段? - How to validate these 2 fields with different error messages? 如何在表单验证中显示不同的错误消息 - How to show different error messages in form validation 如何在字段类型验证中自定义默认浏览器错误消息? - How to customize default browser error message on field type validation? 如何在angular2中针对不同的http错误显示不同的错误消息? - How to show different error messages to different http errors in the angular2? 如何使用VeeValidate(Vue.Js)上的词典为ValidationProvider自定义“必需”错误消息 - How to customize “required” error messages for ValidationProvider using a dictionary on VeeValidate (Vue.Js) 如何使用 VeeValidate (Vue.Js) 上的字典自定义“必需”错误消息 - How to customize "required" error messages using a dictionary on VeeValidate (Vue.Js)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM