Reputation: 9
my UserDto class is here:
package com.carpoint.dto;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.Email;
import org.hibernate.validator.constraints.NotEmpty;
import com.carpoint.validation.annotation.PasswordMatches;
@PasswordMatches
public class UserDto{
@NotEmpty
private String username;
@NotEmpty @Size(min=6, max=30)
private String password;
@NotEmpty
private String confirmPassword;
@NotEmpty
private String firstname;
@NotEmpty
private String lastname;
@NotEmpty @Email
private String email;
private UserAddressDto userAddress;
//getters & setters
}
and UserAddressDto class here:
package com.carpoint.dto;
import org.hibernate.validator.constraints.NotEmpty;
public class UserAddressDto {
@NotEmpty
private String address;
@NotEmpty
private String country;
@NotEmpty
private String city;
@NotEmpty
private Integer pincode;
//getters & setters
}
and here is my UserController code snippet:
@RequestMapping(value = "/add", method = RequestMethod.POST)
public String addUsers(@Valid @ModelAttribute("userDto")UserDto userDto,
ModelMap model, SessionStatus status, RedirectAttributes attributes)
throws IOException {
....
....
}
Upvotes: 0
Views: 2159
Reputation: 407
Did you try putting @Valid
upon private UserAddressDto userAddress;
in class UserDto
? I guess that should be enough as spring handles it on its own.
Upvotes: 4