Reputation: 430
I am facing a problem with Spring form validation. It works for @NotNull annotation but some how not working for @Size. I am attaching some code below. Thanks in advance.
package com.doctor;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
public class Doctor
{
@NotNull(message="Cannot be Null")
private String uname;
@Size(min=1,max=8,message="Min 1 and Max 8")
private String password;
private String doctor_fname,doctor_lname,address,dept_id,experience,email,phone,resume,image;
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
....................
...................
DoctorController
package com.doctor;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.Model;
import javax.validation.Valid;
@Controller
public class DoctorController {
@RequestMapping(value="/registerDoctor", method=RequestMethod.GET)
public String showRegisterForm(Model model)
{
System.out.println("test");
model.addAttribute(new Doctor());
//return "register1";
return "doctor/edit";
}
@RequestMapping(value="/registerDoctor", method=RequestMethod.POST)
public String addDoctorformForm(@Valid Doctor doctor,BindingResult bindingresult)
{
if(bindingresult.hasErrors())
{
return "doctor/edit";
}
else
{
return "doctor/added";
}
}
}
Display page
<%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form"%>
<h2>Create a Doctor account</h2>
<sf:form method="POST" modelAttribute="doctor"
enctype="multipart/form-data">
<fieldset>
<table>
<tr>
<td><sf:label path="uname">User Name:</sf:label></td>
<td><sf:input path="uname" /><br /> <sf:errors
path="uname" /> </td>
</tr>
<tr>
<td><sf:label path="password">Password:</sf:label></td>
<td><sf:password path="password" showPassword="true" />
<br /> <sf:errors
path="password" /></td>
</tr>
<tr>
<td><sf:label path="doctor_fname">First Name:</sf:label></td>
<td><sf:input path="doctor_fname" size="15" /><br /> </td>
</tr>
<tr>
<td><sf:label path="doctor_lname">Last Name:</sf:label></td>
<td><sf:input path="doctor_lname" size="15" /><br /></td>
</tr>
<tr>
<td><sf:label path="address">Address:</sf:label></td>
<td><sf:input path="address" size="15" /><br /></td>
</tr>
<tr>
<td><sf:label path="dept_id">Department:</sf:label></td>
<td><sf:input path="dept_id" size="15" /><br /></td>
</tr>
<tr>
<td><sf:label path="experience">Experience:</sf:label></td>
<td><sf:input path="experience" size="15" /><br /></td>
</tr>
<tr>
<td><sf:label path="email">Email Address:</sf:label></td>
<td><sf:input path="email" size="30" /> <small>In case
you forget something</small><br /> </td>
</tr>
<tr>
<td><sf:label path="phone">Phone:</sf:label></td>
<td><sf:input path="phone" size="30" /> <small>In case
you forget something</small><br /> </td>
</tr>
<tr>
<td><label for="resume">Resume:</label></td>
<td><input name="resume" type="file" />
</tr>
<tr>
<td><label for="image">Profile image:</label></td>
<td><input name="image" type="file" />
</tr>
<tr>
<th></th>
<td><input name="commit" type="submit">
</tr>
</table>
</fieldset>
</sf:form>
I don't understand why @Size for password is not working.
Help will be highly appreciated.
Upvotes: 0
Views: 7356
Reputation: 58
The issue it seems is you are using multipart form but I dont think I see a mention about multipartResolver. If needed here is how you can add it
<bean id="multipartResolver class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
Upvotes: 1
Reputation: 120771
It is because null
is a valid value from @Size
s point of view.
You need both annotations:
@Size(min=1,max=8,message="Min 1 and Max 8")
@NotNull
private String password;
BTW: Your form is a simple form with no file upload, so there is no need to have `enctype="multipart/form-data" - I would remove it
Upvotes: 2