#VRaptor-form
#Installing
Add to your pom:
<dependency>
<groupId>br.com.caelum.vraptor</groupId>
<artifactId>vraptor-form</artifactId>
<version>4.0.0-SNAPSHOT</version>
</dependency>
#Basic validation and mass assignment protection
@Controller
public class UsersController {
@Inject
private Form<User> userForm;
public void add(User user) {
userForm.bind(user);
userForm.navigate().onErrorUsePageOf(HomeController.class).index();
//rest of the code here
}
}
If you need to protect against Mass assignment, you can use hasBlackListedFields or hasOnlyAllowedFields.
public void add(User user) {
userForm.bind(user);
if (userForm.hasBlackListedFields("user.admin")) {
userForm.reject("invalid.params", "Invalid paramters came from your form");
}
userForm.navigate().onErrorUsePageOf(HomeController.class).index();
//rest of the code here
}
#Errors in your view
<c:if test="${form.hasErrors()}">
<div class="alert alert-danger">
<button type="button" class="close" data-dismiss="alert">×</button>
<c:forEach items="${form.allErrors}" var="error">
<b><fmt:message key="${error.category}" /></b> - ${error.message}
</c:forEach>
</div>
</c:if>
If you need, you can access specific field errors:
${userForm.get("login").getErrors()}