3

my problem is that I have a form which has html select element with some choosing option value & I want to validate those value using :

org.hibernate.validator.constraints
or
javax.validation.constraints

annotations. here you may see my form & my select element:

<form:form action="../agents/add" method="POST" commandName="myAgent">
     <form:select path="state">
            <form:option value="ACTIVE" path="state">ACTIVE</form:option>
            <form:option value="LISTEN" path="state">LISTEN</form:option>
            <form:option value="DOWN" path="state">DOWN</form:option>
    </form:select>
</form:form>

I have defined my controller method like this:

    @RequestMapping(value = "agents/add", method = RequestMethod.POST)
    public String addAgentSubmit(@ModelAttribute("myAgent") @Valid final AgentValidator agent, BindingResult result, RedirectAttributes redirect) {
      if (result.hasErrors()) {
        return "admin/agent/add";
      } 
       ...
    }

and I also define a ModelAttribute like this:

@ModelAttribute("myAgent")
 public AgentValidator getLoginForm() {
    return new AgentValidator();
 }

Here is my AgentValidator class also:

public class AgentValidator {
    @NotEmpty(message = "your state can not be empty !")
    private AgentState state;
        getter & setter ...
}

and here is my AgentState:

public enum AgentState {
    ACTIVE, DOWN, PAUSED
 }

when I input a wrong value to my form (something like this):

<form:form action="../agents/add" method="POST" commandName="myAgent">
     <form:select path="state">
            <form:option value="ACTIVE!NVALID" path="state">ACTIVE</form:option>
            <form:option value="LISTEN" path="state">LISTEN</form:option>
            <form:option value="DOWN" path="state">DOWN</form:option>
    </form:select>
</form:form>

after I submit my form I don't have a custom message showing in my JSP, instead I will see this message:

Failed to convert property value of type java.lang.String to required type tm.sys.validator.AgentState for property state; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String to type @javax.validation.constraints.NotNull tm.sys.validator.AgentState for value ACTIVE!NVALID; nested exception is java.lang.IllegalArgumentException: No enum constant tm.sys.validator.AgentState.ACTIVE!NVALID

I searched a lot for this problem but none of the solution helped me to provide my customized message showing to the user. If you have any solution for this please provide it complete because I'm not so advance programmer yet.

1 Answer 1

4

You need to add one of the following keys: typeMismatch, typeMismatch.state or typeMismatch.agentValidator.state into your Messages.properties (or ValidationMessages.properties - whichever messages properties file you have configured with your validator).

typeMismatch.state=You provided invalid state

Exact key name(s) that you need add to message properties can be found from BindingResult. Take a look at result.getFieldError().getCodes() (this should contain codes for the first field error if there is any).

This will override default message (the one which you are seeing) for situations when form submitted value cannot be converted to required type (enum in your case).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.