Search in sources :

Example 1 with Validator

use of jakarta.faces.validator.Validator in project myfaces by apache.

the class ApplicationImpl method createValidator.

@Override
public final Validator createValidator(final String validatorId) throws FacesException {
    Assert.notEmpty(validatorId, "validatorId");
    Class<? extends Validator> validatorClass = getObjectFromClassMap(validatorId, _validatorClassMap);
    if (validatorClass == null) {
        String message = "Unknown validator id '" + validatorId + "'.";
        log.severe(message);
        throw new FacesException(message);
    }
    try {
        if (!_cdiManagedValidatorMap.containsKey(validatorClass)) {
            FacesValidator annotation = validatorClass.getAnnotation(FacesValidator.class);
            if (annotation != null && annotation.managed()) {
                _cdiManagedValidatorMap.put(validatorClass, true);
            } else {
                _cdiManagedValidatorMap.put(validatorClass, false);
            }
        }
        boolean managed = _cdiManagedValidatorMap.get(validatorClass);
        Validator validator = null;
        if (managed) {
            validator = new FacesValidatorCDIWrapper(validatorClass, validatorId);
            _handleAttachedResourceDependencyAnnotations(getFacesContext(), ((FacesWrapper<Validator>) validator).getWrapped());
        } else {
            validator = createValidatorInstance(validatorClass);
            _handleAttachedResourceDependencyAnnotations(getFacesContext(), validator);
        }
        return validator;
    } catch (Exception e) {
        log.log(Level.SEVERE, "Could not instantiate validator " + validatorClass, e);
        throw new FacesException("Could not instantiate validator: " + validatorClass, e);
    }
}
Also used : FacesValidatorCDIWrapper(org.apache.myfaces.cdi.wrapper.FacesValidatorCDIWrapper) FacesException(jakarta.faces.FacesException) FacesValidator(jakarta.faces.validator.FacesValidator) FacesValidator(jakarta.faces.validator.FacesValidator) Validator(jakarta.faces.validator.Validator) MissingResourceException(java.util.MissingResourceException) NamingException(javax.naming.NamingException) ELException(jakarta.el.ELException) FacesException(jakarta.faces.FacesException)

Example 2 with Validator

use of jakarta.faces.validator.Validator in project myfaces by apache.

the class UIInput method getValidators.

/**
 * See getValidator.
 */
@Override
public Validator[] getValidators() {
    if (ExternalSpecifications.isBeanValidationAvailable() && Boolean.TRUE.equals(this.getAttributes().containsKey(BEAN_BEFORE_JSF_PROPERTY))) {
        int bvIndex = -1;
        for (int i = 0; i < _validatorList.size(); i++) {
            Validator v = _validatorList.get(i);
            if (BeanValidationUtils.isBeanValidator(v)) {
                bvIndex = i;
                break;
            }
        }
        if (bvIndex != -1) {
            Validator[] array = new Validator[_validatorList.size()];
            for (int i = 0; i < _validatorList.size(); i++) {
                if (i == bvIndex) {
                    array[0] = _validatorList.get(i);
                    bvIndex = -1;
                } else {
                    array[i + 1] = _validatorList.get(i);
                }
            }
            return array;
        } else {
            return _validatorList == null ? EMPTY_VALIDATOR_ARRAY : _validatorList.toArray(new Validator[_validatorList.size()]);
        }
    } else {
        return _validatorList == null ? EMPTY_VALIDATOR_ARRAY : _validatorList.toArray(new Validator[_validatorList.size()]);
    }
}
Also used : Validator(jakarta.faces.validator.Validator)

Example 3 with Validator

use of jakarta.faces.validator.Validator in project myfaces by apache.

the class ComponentTagHandlerDelegate method addEnclosingValidator.

private void addEnclosingValidator(FacesContext context, EditableValueHolder component, String validatorId, EditableValueHolderAttachedObjectHandler attachedObjectHandler) {
    if (shouldAddEnclosingValidator(component, validatorId)) {
        if (attachedObjectHandler != null) {
            attachedObjectHandler.applyAttachedObject(context, (UIComponent) component);
        } else {
            Validator validator = null;
            // create it
            validator = context.getApplication().createValidator(validatorId);
            // special things to configure for a BeanValidator
            if (validator instanceof BeanValidator) {
                BeanValidator beanValidator = (BeanValidator) validator;
                // check the validationGroups
                String validationGroups = beanValidator.getValidationGroups();
                if (validationGroups == null || validationGroups.matches(BeanValidator.EMPTY_VALIDATION_GROUPS_PATTERN)) {
                    // no validationGroups available
                    // --> get the validationGroups from the stack
                    // String stackGroup = mctx.getFirstValidationGroupFromStack();
                    // if (stackGroup != null)
                    // {
                    // validationGroups = stackGroup;
                    // }
                    // else
                    // {
                    // no validationGroups on the stack
                    // --> set the default validationGroup
                    validationGroups = jakarta.validation.groups.Default.class.getName();
                    // }
                    beanValidator.setValidationGroups(validationGroups);
                }
            }
            // add the validator to the component
            component.addValidator(validator);
        }
    }
}
Also used : BeanValidator(jakarta.faces.validator.BeanValidator) BeanValidator(jakarta.faces.validator.BeanValidator) Validator(jakarta.faces.validator.Validator)

Example 4 with Validator

use of jakarta.faces.validator.Validator in project myfaces by apache.

the class ComponentTagHandlerDelegate method addDefaultValidator.

private void addDefaultValidator(FaceletContext ctx, FaceletCompositionContext mctx, FacesContext context, EditableValueHolder component, String validatorId, String validatorClassName) {
    Validator enclosingValidator = null;
    if (validatorClassName == null) {
        // we have no class name for validators of enclosing <f:validateBean> tags
        // --> we have to create it to get the class name
        // note that normally we can use this instance later anyway!
        enclosingValidator = context.getApplication().createValidator(validatorId);
        validatorClassName = enclosingValidator.getClass().getName();
    }
    // check if the validator is already registered for the given component
    // this happens if <f:validateBean /> is nested inside the component on the view
    Validator validator = null;
    for (Validator v : component.getValidators()) {
        if (v.getClass().getName().equals(validatorClassName)) {
            // found
            validator = v;
            break;
        }
    }
    if (validator == null) {
        if (shouldAddDefaultValidator(ctx, mctx, component, validatorId)) {
            if (enclosingValidator != null) {
                // we can use the instance from before
                validator = enclosingValidator;
            } else {
                // create it
                validator = context.getApplication().createValidator(validatorId);
            }
            // add the validator to the component
            component.addValidator(validator);
        } else {
            // we should not add the validator
            return;
        }
    }
    // special things to configure for a BeanValidator
    if (validator instanceof BeanValidator) {
        BeanValidator beanValidator = (BeanValidator) validator;
        // check the validationGroups
        String validationGroups = beanValidator.getValidationGroups();
        if (validationGroups == null || validationGroups.matches(BeanValidator.EMPTY_VALIDATION_GROUPS_PATTERN)) {
            // no validationGroups available
            // --> get the validationGroups from the stack
            // String stackGroup = mctx.getFirstValidationGroupFromStack();
            // if (stackGroup != null)
            // {
            // validationGroups = stackGroup;
            // }
            // else
            // {
            // no validationGroups on the stack
            // --> set the default validationGroup
            validationGroups = jakarta.validation.groups.Default.class.getName();
            // }
            beanValidator.setValidationGroups(validationGroups);
        }
    }
}
Also used : BeanValidator(jakarta.faces.validator.BeanValidator) BeanValidator(jakarta.faces.validator.BeanValidator) Validator(jakarta.faces.validator.Validator)

Example 5 with Validator

use of jakarta.faces.validator.Validator in project myfaces by apache.

the class ValidatorTagHandlerDelegate method applyAttachedObject.

@SuppressWarnings("unchecked")
@Override
public void applyAttachedObject(FacesContext context, UIComponent parent) {
    // Retrieve the current FaceletContext from FacesContext object
    FaceletContext faceletContext = (FaceletContext) context.getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY);
    // id from beeing registered on the component.
    if (_delegate.isDisabled(faceletContext)) {
        // tag is disabled --> add its validatorId to the parent's exclusion list
        String validatorId = _delegate.getValidatorConfig().getValidatorId();
        if (validatorId != null && !validatorId.isEmpty()) {
            List<String> exclusionList = (List<String>) parent.getAttributes().get(VALIDATOR_ID_EXCLUSION_LIST_KEY);
            if (exclusionList == null) {
                exclusionList = new ArrayList<String>();
                parent.getAttributes().put(VALIDATOR_ID_EXCLUSION_LIST_KEY, exclusionList);
            }
            exclusionList.add(validatorId);
        }
    } else {
        // tag is enabled --> create the validator and attach it
        // cast to a ValueHolder
        EditableValueHolder evh = (EditableValueHolder) parent;
        ValueExpression ve = null;
        Validator v = null;
        if (_delegate.getBinding() != null) {
            ve = _delegate.getBinding().getValueExpression(faceletContext, Validator.class);
            v = (Validator) ve.getValue(faceletContext);
        }
        if (v == null) {
            v = this.createValidator(faceletContext);
            if (ve != null) {
                ve.setValue(faceletContext, v);
            }
        }
        if (v == null) {
            throw new TagException(_delegate.getTag(), "No Validator was created");
        }
        _delegate.setAttributes(faceletContext, v);
        if (shouldBeanBeforeJsfValidationEnabled(context)) {
            parent.getAttributes().put(BEAN_BEFORE_JSF_PROPERTY, Boolean.TRUE);
        }
        evh.addValidator(v);
    }
}
Also used : TagException(jakarta.faces.view.facelets.TagException) ValueExpression(jakarta.el.ValueExpression) FaceletContext(jakarta.faces.view.facelets.FaceletContext) ArrayList(java.util.ArrayList) List(java.util.List) Validator(jakarta.faces.validator.Validator) EditableValueHolder(jakarta.faces.component.EditableValueHolder)

Aggregations

Validator (jakarta.faces.validator.Validator)29 LengthValidator (jakarta.faces.validator.LengthValidator)12 UIInput (jakarta.faces.component.UIInput)7 BeanValidator (jakarta.faces.validator.BeanValidator)7 Test (org.junit.Test)7 LongRangeValidator (jakarta.faces.validator.LongRangeValidator)5 EditableValueHolder (jakarta.faces.component.EditableValueHolder)4 UIComponent (jakarta.faces.component.UIComponent)4 UIViewRoot (jakarta.faces.component.UIViewRoot)4 DoubleRangeValidator (jakarta.faces.validator.DoubleRangeValidator)4 ELException (jakarta.el.ELException)3 ValueExpression (jakarta.el.ValueExpression)3 FacesException (jakarta.faces.FacesException)3 FacesContext (jakarta.faces.context.FacesContext)3 ValidatorException (jakarta.faces.validator.ValidatorException)3 IOException (java.io.IOException)3 PrintWriter (java.io.PrintWriter)3 Application (jakarta.faces.application.Application)2 FacesValidator (jakarta.faces.validator.FacesValidator)2 RegexValidator (jakarta.faces.validator.RegexValidator)2