mvc3 验证输入“不等于”
我的表单具有带有默认帮助文本的输入,可指导用户输入内容(而不是使用标签)。这使得验证变得棘手,因为输入值永远不会为空。
我如何扩展不显眼的验证来处理这个问题?如果名称输入等于“请输入您的姓名...”,则该表单不应有效
我开始阅读 Brad Wilson 关于验证适配器的博客文章,但我不确定这是否是正确的方法?我需要能够根据字段验证不同的默认值。
谢谢
My forms have inputs with default helper text that guides the user on what to enter (rather than using labels). This makes validation tricky because the input value is never null.
How can I extend unobtrusive validation to handle this? The form shouldn't be valid if the Name input is equal to "Please enter your name..."
I started reading Brad Wilson's blog post on validation adapters, but I'm not sure if this is the right way to go? I need to be able to validate against different default values depending on the field.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
下面是一个示例,说明如何继续实现自定义验证属性:
然后在模型:
控制器:
和视图上:
Here's a sample illustrating how you could proceed to implement a custom validation attribute:
and then on the model:
controller:
and view:
是的,那就是正确的方法。您应该实现自己的属性并实现
IClientValidatable
。您还可以将必需的布尔值最初设置为
false
作为隐藏表单字段。当用户更改文本框时,将其设置为 true。Yes thats the right way to go. You should implement your own atribute and implement
IClientValidatable
.You could also have a required boolean value set initially to
false
as a hidden form field. When the user changes the textbox, set it to true.您可以使 ViewModel 实现 IValidatableObject,并在实现 Validate 方法(来自 IValidatableObject)时添加一些逻辑来检查属性的值,例如
现在,当在控制器中调用 Model.IsValid 时,将运行这段逻辑并将返回验证错误正常。
You could make your ViewModel implement IValidatableObject and when implementing the Validate method (from IValidatableObject) add some logic to check the values of the properties e.g.
Now, when Model.IsValid is called in your controller, this bit of logic will be ran and will return validation errors as normal.
自从提出你的问题以来花了一点时间,但如果你仍然喜欢数据注释,这个问题可以使用 this 轻松解决库:
上面,字段值与一些预定义的文本进行比较。或者,您可以相互比较字段值:
...并且当比较的字符串的大小写不重要时:
It took a little while since your question was asked, but if you still like data annotations, this problem can be easily solved using this library:
Above, the field value is compared with some pre-defined text. Alternatively, you can compare fields values with each other:
...and when the case of the strings being compared does not matter:
为了改进 @Darin Dimitrov 的答案,如果您想使用 ErrorMessageResourceName 和 ErrorMessageResourceType 添加来自资源的消息,只需将其添加到错误消息中
ErrorMessage = ErrorMessage ?? ErrorMessageString
ErrorMessageString 将查找您使用这些参数(ErrorMessageResourceName 和 ErrorMessageResourceType)在模型中设置的错误消息的本地化版本
To improve a little bit of @Darin Dimitrov answer, if you want to add messages from the resources using
ErrorMessageResourceName and ErrorMessageResourceType
, just add this to the to the Error messageErrorMessage = ErrorMessage ?? ErrorMessageString
The ErrorMessageString will look for the localized version of error message that you set in the model using those parameters (ErrorMessageResourceName and ErrorMessageResourceType)
理想的解决方案是自定义属性,您可以在其中指定最小和最大长度以及 MustNotContain="请输入您的姓名..."。
The ideal solutions is a custom Attribute where you specify minimum and maximum lengths as well as MustNotContain="Please enter your name...".