Advertisement
arthurgregorio

Validator Sample

Jan 24th, 2013
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.69 KB | None | 0 0
  1. package se.conecte.view.validators;
  2.  
  3. import javax.faces.application.FacesMessage;
  4. import javax.faces.component.UIComponent;
  5. import javax.faces.context.FacesContext;
  6. import javax.faces.validator.Validator;
  7. import javax.faces.validator.ValidatorException;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.context.annotation.Scope;
  10. import org.springframework.stereotype.Component;
  11. import se.conecte.services.IUserService;
  12.  
  13. /**
  14.  *
  15.  * @author Arthur Gregorio
  16.  *
  17.  * @category
  18.  *
  19.  * @since 1.0
  20.  * @version 1.0, 05/11/2012
  21.  */
  22. @Scope("request")
  23. @Component("userLoginValidator")
  24. public class UserLoginValidator implements Validator {
  25.  
  26.     @Autowired
  27.     private IUserService userService;
  28.  
  29.     @Override
  30.     public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
  31.        
  32.         final boolean isSaved = (Boolean) component.getAttributes().get("isSaved");
  33.        
  34.         if (!isSaved) {
  35.             final String username = value.toString();
  36.  
  37.             if (userService.findByLogin(username) != null) {
  38.                 FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
  39.                         "Erro!", "Nome de usuário duplicado!");
  40.                 throw new ValidatorException(message);
  41.             }
  42.         }
  43.     }
  44. }
  45.  
  46. <p:inputText id="inLogin"
  47.              required="true"
  48.              value="#{userController.user.login}"
  49.              validatorMessage="Login informado é inválido">
  50.     <f:validateRegex pattern="[a-zA-Z0-9_]{3,16}" />
  51.     <f:validator binding="#{userLoginValidator}"/>
  52.     <f:attribute name="isSaved" value="#{userController.user.saved}"/>
  53. </p:inputText>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement