0

I Provide a question in [here] : Spring MVC form validation but unfortunately I was not able to solve the problem completely !
my problem is when I submit a wrong value to form (select element) the default error of Spring just show up:

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 ACTIVE3; nested exception is java.lang.IllegalArgumentException: No enum constant tm.sys.validator.AgentState.ACTIVE3

how I can make my custom handler message because this one is so weird and my client can abuse it.

note that I use some validators as well and I also check BindingResult methods and I could not find a setter to overwrite the message !

Community
  • 1
  • 1
Mehdi
  • 3,795
  • 3
  • 36
  • 65
  • The problem is not the validation. A solution has already been suggested by @n1ckolas. Have you tried it? – a better oliver Mar 17 '13 at 14:53
  • that solution hadn't solved my problem, that's why I provide this question I searched a lot & try different solution using @ InitBinder & @ ExceptionHandler but they didn't worked for me! I don't know how to use them effectively to solve my problem, all I need is just a custom message(not spring default exception message) showing in my jsp when a user pass an illegal format !!!! – Mehdi Mar 18 '13 at 19:12
  • I just found the problem. Is `AgentValidator`only a validator or do you use it for somthing else as well? – a better oliver Mar 18 '13 at 19:30
  • AgentValidator is a bean class & I pass the form values to that class & use Validator libraries to validate the jsp form values. then I use DataBinder obj as a parameter inside my controller method for validation & I check to display the error if any error exist inside jsp using – Mehdi Mar 18 '13 at 20:01
  • The thing is: you cannot use both an enum and the `AgentStateConstraintValidator` suggested by @n1ckolas. In your `AgentValidator` class keep `private String state;` and add `@AgentStateConstraint`. Then the `AgentStateConstraintValidator` will check if it has a valid value. – a better oliver Mar 18 '13 at 20:08
  • I haven't used the second solution just :public class AgentValidator { @NotNull(message = "your state can not be empty !") private AgenState state; Where AgentState is: public enum AgentState { DOWN,LISTEN,ACTIVE } that's all ! – Mehdi Mar 18 '13 at 20:12
  • I just define an enum class & then insiden my bean I give the object of the enum class instead. (private AgenState state;) got it ? – Mehdi Mar 18 '13 at 20:14
  • everything works great but the problem is just the exception message which shows inside my jsp in case of user wrong input(ex String instead of int / different values instead of enum defined values)! got it ? – Mehdi Mar 18 '13 at 20:17
  • But the second solution is what you want ;) If you use enums and get a wrong value, like "ACTIVE3", then your method will never be invoked. There are ways to deal with this, but not with validators, because they wouldn't be called either. – a better oliver Mar 18 '13 at 20:17
  • what method will never be invoked ? – Mehdi Mar 18 '13 at 20:19
  • I don't think so coz If it never be invoked how then the default spring exception error message shows inside my jsp along with all other error messages ? I'm pretty sure that I also checked inside my if(dataBinderObject.hasError()) which is inside my addAgentSubmit to see whether after the error exist, it pass trough it or not & it passed!
    and one more inportant thing is if it doesn't pass through it how the page get redirected back to it's own & show the messages ? It's just because of the return value which I do provide inside my if(dataBinderObject.hasError())
    – Mehdi Mar 18 '13 at 20:27
  • I see, one your parameters is `BindingResult`, so you are right. In this case the method gets invoked and you have that ugly error message. So you can either play with the data binder or use the suggested solution. – a better oliver Mar 18 '13 at 20:44
  • 1
    [this](http://alasdoo.com/2011/07/data-validation-conversion-basics-custom-error-messages-in-spring-mvc/) might help you. It shows how you can easily define your own message. – a better oliver Mar 18 '13 at 22:10
  • using typeMismatch.state solved my problem take a look at this: http://www.raistudies.com/spring/spring-mvc/form-processing-spring-mvc-3-annotation/ – Mehdi Mar 31 '13 at 05:33

1 Answers1

0

By using typeMismatch I solved this problem : here is the sample that I used to understand the cencept of typeMismatch : http://www.raistudies.com/spring/spring-mvc/form-processing-spring-mvc-3-annotation

Mehdi
  • 3,795
  • 3
  • 36
  • 65