Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring validation, how to have PropertyEditor generate specific error message
    text
    copied!<p>I'm using Spring for form input and validation. The form controller's command contains the model that's being edited. Some of the model's attributes are a custom type. For example, Person's social security number is a custom SSN type.</p> <pre><code>public class Person { public String getName() {...} public void setName(String name) {...} public SSN getSocialSecurtyNumber() {...} public void setSocialSecurtyNumber(SSN ssn) {...} } </code></pre> <p>and wrapping Person in a Spring form edit command:</p> <pre><code>public class EditPersonCommand { public Person getPerson() {...} public void setPerson(Person person) {...} } </code></pre> <p>Since Spring doesn't know how to convert text to a SSN, I register a customer editor with the form controller's binder:</p> <pre><code>public class EditPersonController extends SimpleFormController { protected void initBinder(HttpServletRequest req, ServletRequestDataBinder binder) { super.initBinder(req, binder); binder.registerCustomEditor(SSN.class, "person.ssn", new SsnEditor()); } } </code></pre> <p>and SsnEditor is just a custom <code>java.beans.PropertyEditor</code> that can convert text to a SSN object:</p> <pre><code>public class SsnEditor extends PropertyEditorSupport { public String getAsText() {...} // converts SSN to text public void setAsText(String str) { // converts text to SSN // throws IllegalArgumentException for invalid text } } </code></pre> <p>If <code>setAsText</code> encounters text that is invalid and can't be converted to a SSN, then it throws <code>IllegalArgumentException</code> (per <code>PropertyEditor</code> <code>setAsText</code>'s specification). The issue I'm having is that the text to object conversion (via <code>PropertyEditor.setAsText()</code>) takes place <strong>before</strong> my Spring validator is called. When <code>setAsText</code> throws <code>IllegalArgumentException</code>, Spring simply displays the generic error message defined in <code>errors.properties</code>. What I want is a specific error message that depends on the exact reason why the entered SSN is invalid. <code>PropertyEditor.setAsText()</code> would determine the reason. I've tried embedded the error reason text in <code>IllegalArgumentException</code>'s text, but Spring just treats it as a generic error.</p> <p>Is there a solution to this? To repeat, what I want is the specific error message generated by the <code>PropertyEditor</code> to surface to the error message on the Spring form. The only alternative I can think of is to store the SSN as text in the command and perform validation in the validator. The text to SSN object conversion would take place in the form's <code>onSubmit</code>. This is less desirable as my form (and model) has many properties and I don't want to have to create and maintain a command that has each and every model attribute as a text field.</p> <p>The above is just an example, my actual code isn't Person/SSN, so there's no need to reply with "why not store SSN as text..."</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload