Search in sources :

Example 1 with ValidatorException

use of jakarta.faces.validator.ValidatorException in project rubia-forums by flashboss.

the class PollValidator method throwValidationException.

private void throwValidationException(String exceptionMsg) throws ValidatorException {
    FacesMessage message = new FacesMessage();
    message.setDetail(getBundleMessage(BUNDLE_NAME, exceptionMsg));
    message.setSummary(getBundleMessage(BUNDLE_NAME, exceptionMsg));
    message.setSeverity(FacesMessage.SEVERITY_ERROR);
    throw new ValidatorException(message);
}
Also used : ValidatorException(jakarta.faces.validator.ValidatorException) FacesMessage(jakarta.faces.application.FacesMessage)

Example 2 with ValidatorException

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

the class UIInputTest method testValidateWithEmptyStringWithEmptyStringAsNullEnabled.

public void testValidateWithEmptyStringWithEmptyStringAsNullEnabled() {
    try {
        InitParameterMockExternalContext mockExtCtx = new InitParameterMockExternalContext(servletContext, request, response);
        mockExtCtx.getInitParameterMap().put("jakarta.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL", "true");
        mockExtCtx.getInitParameterMap().put(UIInput.VALIDATE_EMPTY_FIELDS_PARAM_NAME, "true");
        facesContext.setExternalContext(mockExtCtx);
        input.addValidator(new Validator() {

            public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
                // the value must be null
                Assert.assertNull(value);
            }
        });
        input.setSubmittedValue("");
        input.validate(facesContext);
        Assert.assertEquals(null, input.getSubmittedValue());
    } finally {
        facesContext.setExternalContext(externalContext);
    }
}
Also used : FacesContext(jakarta.faces.context.FacesContext) ValidatorException(jakarta.faces.validator.ValidatorException) UIComponent(jakarta.faces.component.UIComponent) LengthValidator(jakarta.faces.validator.LengthValidator) Validator(jakarta.faces.validator.Validator)

Example 3 with ValidatorException

use of jakarta.faces.validator.ValidatorException in project myfaces-tobago by apache.

the class ValidationController method passwordValidator.

public void passwordValidator(final FacesContext facesContext, final UIComponent component, final Object value) throws ValidatorException {
    final String password = value.toString();
    final UIInput confirmationField = (UIInput) component.getAttributes().get("confirmationField");
    final String confirmationFieldValue = confirmationField.getSubmittedValue().toString();
    if (password.isEmpty() || confirmationFieldValue.isEmpty()) {
        return;
    }
    if (!password.equals(confirmationFieldValue)) {
        confirmationField.setValid(false);
        throw new ValidatorException(new FacesMessage("Passwords must match."));
    }
}
Also used : ValidatorException(jakarta.faces.validator.ValidatorException) UIInput(jakarta.faces.component.UIInput) FacesMessage(jakarta.faces.application.FacesMessage)

Example 4 with ValidatorException

use of jakarta.faces.validator.ValidatorException in project mojarra by eclipse-ee4j.

the class WholeBeanValidator method validate.

public void validate(FacesContext context, UIValidateWholeBean component, Object value) throws ValidatorException {
    // Get parent and check if the parent of this f:validateWholeBean is a form
    UIForm form = getParentForm(component);
    ValueExpression wholeBeanVE = getValueExpressionNullSafe(component, "value");
    // The "whole" bean that we're going to validate at the class level
    Object wholeBean = wholeBeanVE.getValue(context.getELContext());
    // A shortened or fully qualified class name, or EL expression pointing
    // to a type that copies the target bean for validation
    String copierType = (String) component.getAttributes().get("copierType");
    // Inspect the status of field level validation
    if (hasAnyBeanPropertyFailedValidation(context, wholeBean)) {
        return;
    }
    AddRemainingCandidateFieldsCallback addRemainingCandidateFieldsCallback = new AddRemainingCandidateFieldsCallback(context, wholeBean);
    form.visitTree(createVisitContext(context), addRemainingCandidateFieldsCallback);
    Map<String, Map<String, Object>> validationCandidate = addRemainingCandidateFieldsCallback.getCandidate();
    if (validationCandidate.isEmpty()) {
        return;
    }
    // Perform the actual bean validation on a copy of the whole bean
    Set<ConstraintViolation<?>> violations = doBeanValidation(getBeanValidator(context), copyBeanAndPopulateWithCandidateValues(context, wholeBeanVE, wholeBean, copierType, validationCandidate), component.getValidationGroupsArray(), wholeBeanVE);
    // If there are any violations, transform them into a Faces validator exception
    if (violations != null && !violations.isEmpty()) {
        ValidatorException toThrow;
        if (violations.size() == 1) {
            ConstraintViolation<?> violation = violations.iterator().next();
            toThrow = new ValidatorException(getMessage(context, MESSAGE_ID, violation.getMessage(), getLabel(context, component)));
        } else {
            Set<FacesMessage> messages = new LinkedHashSet<>(violations.size());
            for (ConstraintViolation<?> violation : violations) {
                messages.add(getMessage(context, MESSAGE_ID, violation.getMessage(), getLabel(context, component)));
            }
            toThrow = new ValidatorException(messages);
        }
        // values during updateModelValues
        for (Entry<String, Map<String, Object>> validationCandidateEntry : validationCandidate.entrySet()) {
            invalidateComponent(validationCandidateEntry);
        }
        throw toThrow;
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) UIForm(jakarta.faces.component.UIForm) ValidatorException(jakarta.faces.validator.ValidatorException) ValueExpression(jakarta.el.ValueExpression) ConstraintViolation(jakarta.validation.ConstraintViolation) HashMap(java.util.HashMap) Map(java.util.Map) Collections.emptyMap(java.util.Collections.emptyMap) FacesMessage(jakarta.faces.application.FacesMessage)

Example 5 with ValidatorException

use of jakarta.faces.validator.ValidatorException in project mojarra by eclipse-ee4j.

the class ContextualCompositeMethodExpression method invoke.

@Override
public Object invoke(ELContext elContext, Object[] objects) {
    FacesContext ctx = (FacesContext) elContext.getContext(FacesContext.class);
    try {
        boolean pushed = pushCompositeComponent(ctx);
        try {
            return delegate.invoke(elContext, objects);
        } finally {
            if (pushed) {
                popCompositeComponent(ctx);
            }
        }
    } catch (ELException ele) {
        /*
             * If we got a validator exception it is actually correct to immediately bubble it up.
             */
        if (ele.getCause() != null && ele.getCause() instanceof ValidatorException) {
            throw (ValidatorException) ele.getCause();
        }
        if (source != null && ele instanceof MethodNotFoundException) {
            // nesting level. Is there a cleaner way to detect this case?
            try {
                Object fallback = source.getValue(elContext);
                if (fallback != null && fallback instanceof MethodExpression) {
                    return ((MethodExpression) fallback).invoke(elContext, objects);
                }
            } catch (ELException ex) {
                /*
                     * If we got a validator exception it is actually correct to immediately bubble it up.
                     */
                if (ex.getCause() != null && ex.getCause() instanceof ValidatorException) {
                    throw (ValidatorException) ex.getCause();
                }
                if (LOGGER.isLoggable(Level.WARNING)) {
                    LOGGER.log(Level.WARNING, ele.toString());
                    LOGGER.log(Level.WARNING, "faces.facelets.el.method.expression.invoke.error: {0} {1}", new Object[] { ex.toString(), source.getExpressionString() });
                }
                if (!(ex instanceof MethodNotFoundException)) {
                    throw ex;
                }
            }
        }
        throw ele;
    }
}
Also used : FacesContext(jakarta.faces.context.FacesContext) ValidatorException(jakarta.faces.validator.ValidatorException) ELException(jakarta.el.ELException) MethodNotFoundException(jakarta.el.MethodNotFoundException) MethodExpression(jakarta.el.MethodExpression)

Aggregations

ValidatorException (jakarta.faces.validator.ValidatorException)25 UIInput (jakarta.faces.component.UIInput)16 FacesContext (jakarta.faces.context.FacesContext)13 UIViewRoot (jakarta.faces.component.UIViewRoot)9 FacesMessage (jakarta.faces.application.FacesMessage)8 ServletException (jakarta.servlet.ServletException)8 IOException (java.io.IOException)8 PrintWriter (java.io.PrintWriter)8 LengthValidator (jakarta.faces.validator.LengthValidator)5 UIComponent (jakarta.faces.component.UIComponent)4 DoubleRangeValidator (jakarta.faces.validator.DoubleRangeValidator)4 LongRangeValidator (jakarta.faces.validator.LongRangeValidator)4 Validator (jakarta.faces.validator.Validator)3 ValueExpression (jakarta.el.ValueExpression)2 RegexValidator (jakarta.faces.validator.RegexValidator)2 ConstraintViolation (jakarta.validation.ConstraintViolation)2 LinkedHashSet (java.util.LinkedHashSet)2 Map (java.util.Map)2 Test (org.junit.Test)2 ELException (jakarta.el.ELException)1