Commit de UserValidator
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package org.yacos.web.system.validation;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.springframework.validation.Errors;
|
||||
import org.springframework.validation.ValidationUtils;
|
||||
import org.springframework.validation.Validator;
|
||||
import org.yacos.web.system.form.FormUser;
|
||||
|
||||
/**
|
||||
* @author christiancorsano
|
||||
*
|
||||
*/
|
||||
public class UserValidator implements Validator {
|
||||
|
||||
/**
|
||||
* @see org.springframework.validation.Validator#supports(java.lang.Class)
|
||||
*/
|
||||
public boolean supports(Class commandClass) {
|
||||
return FormUser.class.equals(commandClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors)
|
||||
*/
|
||||
public void validate(Object commandObject, Errors errors) {
|
||||
FormUser userCommand = (FormUser) commandObject;
|
||||
|
||||
// User login
|
||||
Pattern loginPattern = Pattern.compile("^[a-zA-Z][a-zA-Z0-9\\._-]+$");
|
||||
Matcher loginMatcher = loginPattern.matcher(userCommand.getLogin());
|
||||
if(loginMatcher.matches()){
|
||||
errors.rejectValue("login", "form.register.error.loginInvalid");
|
||||
}
|
||||
if(userCommand.getLogin().length()<3){
|
||||
errors.rejectValue("login", "form.register.error.loginTooShort");
|
||||
}
|
||||
|
||||
ValidationUtils.rejectIfEmptyOrWhitespace(errors, userCommand.getPassword(), "form.register.error.passwordEmpty");
|
||||
if(userCommand.passwordWasModified() && (! userCommand.passwordsMatches())){
|
||||
errors.rejectValue("password", "form.register.error.passwordsDontMatches");
|
||||
}
|
||||
|
||||
ValidationUtils.rejectIfEmptyOrWhitespace(errors, userCommand.getFirstName(), "form.register.error.firstNameEmpty");
|
||||
ValidationUtils.rejectIfEmptyOrWhitespace(errors, userCommand.getLastName(), "form.register.error.lastNameEmpty");
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user