开发者

Struts2 - disable conversion error

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_REGE开发者_开发百科X,person.getSsn().trim())){
   this.addFieldError("person.ssn","SSN is required");
}


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;

if(yourCondition){
   // Check whether this field has existing errors and remove them.
   List<String> existingErrors = getFieldErrors().get("person.ssn");
   if(existingErrors != null){
       existingErrors.clear();
   }       
   // Add your own error.
   addFieldError("person.ssn","SSN is required");
}

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜