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);
}
}
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()]);
}
}
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);
}
}
}
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);
}
}
}
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);
}
}
Aggregations