3

Here is an issue I have been stuck into. Actually, I implemented all required features in order to get couple of my field validated, however I still get that 400 error. I even compared what I have made with examples from Spring 3 for Professionals (taking their sourcecode); their examples work perfectly, and failed to find difference... :((( - maybe it is due to lack of attention again.

So, here is couple of fields properly annoted to be validated:

@NotEmpty(message="{validation.firstname.NotEmpty.message}") 
@Size(min=3, max=60, message="{validation.firstname.Size.message}")
@Column(name = "FirstName") 
public String getFirstName() { 
    return firstName;
}

@NotEmpty(message="{validation.lastname.NotEmpty.message}") 
@Size(min=1, max=40, message="{validation.lastname.Size.message}")
@Column(name = "LastName") 
public String getLastName() { 
    return lastName;
}

Their messages within messages.properties:

validation.firstname.NotEmpty.message=First name is required
validation.lastname.NotEmpty.message=Last name is required
validation.firstname.Size.message=First name must be between {min} and {max}
validation.lastname.Size.message=Last name must be between {min} and {max}

And yes, messages.properties is reachable, since I successfully get amother message related to success saving.

Here are related parts of methods within controller:

public String update(@Valid Employee employee,...
public String create(@Valid Employee employee,...

i.e. I put required @Valid annotation there.

Then part of my servlet-context.xml:

<annotation-driven validator="validator"/>

<beans:bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"> 
    <beans:property name="validationMessageSource" ref="messageSource"/> 
</beans:bean>

Here is related part of my edit.jspx

<form:label path="firstName"> ${labelEmployeeFirstName}* </form:label> 

<form:input path="firstName" /> 
<div> <form:errors path="firstName" cssClass="error" /> </div> 
<p/> 

<form:label path="lastName">${labelEmployeeLastName}* </form:label> 

<form:input path="lastName" /> 
<div> <form:errors path="lastName" cssClass="error" /> </div> 

I don't know whether something else required to put here to clarify more...

orionix
  • 61
  • 1
  • 3
  • 10

1 Answers1

-2

Remove the @Valid annotation from the controller method argument. Does it still give you 400?

Jukka
  • 4,583
  • 18
  • 14
  • Thank you everybody... the problem has disappeared, unfortunately, I don't know what was the cause, I simply re-wrote some parts of code from the scratch and then everything works smoothly. I cannot reproduce it. – orionix Jun 13 '13 at 21:08
  • 8
    In the Controller method, the parameter Bindingresult should be right after the @Valid parameter. Otherwise, you will get a 400 error. – Adamsan Jun 10 '16 at 08:06
  • 1
    @TigranBabajanyan It's written it the spring documentation somewhere, I don't know anything more specific. – Adamsan Nov 29 '17 at 22:30