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.