Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
License Agreement. Rich Faces - Natural Ajax for Java Server Faces (JSF) Copyright (C) 2007 Exadel, Inc. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
 package org.richfaces.validator;
 
 
 import  javax.faces.FacesException;
 import  javax.faces.application.FacesMessage;
 import  javax.faces.component.EditableValueHolder;
 import  javax.faces.component.UIComponent;
 import  javax.faces.context.FacesContext;
 import  javax.faces.validator.Validator;
 import  javax.faces.validator.ValidatorException;
 
 import  org.richfaces.application.ServiceTracker;
 
 import  com.google.common.base.Strings;

Implementation of the JSF validator to use with Bean Validation / Hibernate validator TODO - implement partial state saving.

Author(s):
asmirnov
 
 public class FacesBeanValidator implements Serializable, Validator, GraphValidator {
     public static final String BEAN_VALIDATOR_TYPE = "org.richfaces.BeanValidator";
    
 
     private static final long serialVersionUID = -264568176252121853L;
     private ValueExpression summaryExpression = null;
     private String summary = null;
     private ValueExpression groupsExpression = null;
     private Class<?>[] groups = null;
     private boolean validateFields = true;
 
     public FacesBeanValidator() {
     }

    

Returns:
the summary
 
     public String getSummary() {
         String summaryString = null;
         if (null != ) {
             summaryString = (String.getValue(FacesContext.getCurrentInstance().getELContext());
         } else {
             summaryString = this.;
         }
         return summaryString;
     }

    

Parameters:
summary the summary to set
 
     public void setSummary(ValueExpression summary) {
         this. = summary;
     }

    

Parameters:
summary the summary to set
 
     public void setSummary(String summary) {
         this. = summary;
     }
 
     /*
      * (non-Javadoc)
      *
      * @see javax.faces.validator.Validator#validate(javax.faces.context.FacesContext, javax.faces.component.UIComponent,
      * java.lang.Object)
      */
     public void validate(FacesContext context, UIComponent componentObject convertedValuethrows ValidatorException {
         if (component instanceof EditableValueHolder) {
            // Validate input component
            EditableValueHolder input = (EditableValueHolder) component;
            try {
                ValueExpression valueExpression = component.getValueExpression("value");
                if (null != valueExpression) {
                    BeanValidatorService validatorService = ServiceTracker.getService(BeanValidatorService.class);
                    Collection<Stringmessages = validatorService.validateExpression(contextvalueExpressionconvertedValue,
                        getGroups());
                    if (isValidateFields() && !messages.isEmpty()) {
                        input.setValid(false);
                        Object label = getLabel(contextcomponent);
                        Locale locale = context.getViewRoot().getLocale();
                        // send all validation messages.
                        for (String msg : messages) {
                            // https://jira.jboss.org/jira/browse/RF-7636 -
                            // format values.
                            String formattedMessage = formatMessage(msglocalelabelconvertedValue); // create Summary
                                                                                                         // message ?
                            String summary = getSummary();
                            String formattedSummary = Strings.isNullOrEmpty(summary) ? formattedMessage : formatMessage(
                                summarylocalelabelconvertedValue);
                            context.addMessage(component.getClientId(context), new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                formattedSummaryformattedMessage));
                        }
                    }
                }
            } catch (ELException e) {
                throw new FacesException(e);
            }
        }
    }
    static String formatMessage(String msgLocale localeObject... messageParams) {
        if (msg.contains("{")) {
            try {
                MessageFormat messageFormat = new MessageFormat(msglocale);
                msg = messageFormat.format(messageParams);
            } catch (IllegalArgumentException e) {
                // Do nothing, use original string unchanged.
            }
        }
        return msg;
    }
    static Object getLabel(FacesContext context, UIComponent component) {
        Object label = component.getAttributes().get("label");
        if (null == label || 0 == label.toString().length()) {
            label = component.getClientId(context);
        }
        return label;
    }
    public Collection<StringvalidateGraph(FacesContext context, UIComponent componentObject valueClass<?>[] groups)
        throws ValidatorException {
        BeanValidatorService validatorService = ServiceTracker.getService(BeanValidatorService.class);
        Collection<Stringmessages = validatorService.validateObject(contextvaluegroups);
        return messages;
    }

    

Returns:
the profiles
    public Class<?>[] getGroups() {
        Class<?>[] profiles;
        if (null != ) {
            profiles = (Class<?>[]) .getValue(FacesContext.getCurrentInstance().getELContext());
        } else {
            profiles = this.;
        }
        return profiles;
    }

    

Parameters:
profiles the profiles to set
    public void setGroups(Class<?>... profiles) {
        this. = profiles;
    }
    public void setGroups(ValueExpression profilesExpression) {
        this. = profilesExpression;
    }

    

Returns:
the ignoreErrors
    public boolean isValidateFields() {
        return this.;
    }

    

Parameters:
ignoreErrors the ignoreErrors to set
    public void setValidateFields(boolean ignoreErrors) {
        this. = ignoreErrors;
    }
New to GrepCode? Check out our FAQ X