Struts2 - 禁用转换错误
我有一个 ssn 字段(在 Action 类中表示为 String),用户在其中输入以下格式的内容 123-23-2233。 Struts2 抛出错误。我不知道它在哪里配置为将其作为错误抛出。我该如何阻止这个?
我在 validate() 方法中有自己的验证,如下所示
if(StringUtility.isBlank(person.getSsn()) || !validateRegex(SSN_REGEX,person.getSsn().trim())){
this.addFieldError("person.ssn","SSN is required");
}
I have a ssn field (represented as a String in Action class) in which the user enters something in the following format 123-23-2233. Struts2 throws an error. I dont know where it is configured for it to throw this as an error. How do I stop this?
I have my own validation in the validate() method, something like this
if(StringUtility.isBlank(person.getSsn()) || !validateRegex(SSN_REGEX,person.getSsn().trim())){
this.addFieldError("person.ssn","SSN is required");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在验证方法运行之前,转换错误将被添加到字段错误映射中。因此,一旦您进入验证方法,就有一种非常简单的方法可以删除它们。只需从地图中删除错误,然后再添加自己的错误即可。
下面的示例代码;
同样,如果您想清除所有字段上的默认错误消息,您可以清除整个字段错误映射。
我希望这有帮助。
The conversion errors will be added to the field errors map before your validate method even runs. As such there is a very simple way of removing them once you get to your validate method. Simply remove the error from the map before adding your own.
Example code below;
Similarly you could clear the entire field errors map if you wanted to clear the default error messages on all fields.
I hope this helps.