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 is my select element:
<select name="status" id="tbSelect"> <option value="ACTIVE">ACTIVE</option> <option value="LISTEN">LISTEN</option> <option value="DOWN">DOWN</option> </select> how I can for example validate the value of the options(DOWN,LISTEN,ACTIVE) inside the select element by using the annotation validators which I mention above?
my form is like this :
<form:form action="../agents/add" method="POST" commandName="myAgent"> <form:select id="tbSelect" 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> 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 String state;