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);
}
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);
}
}
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."));
}
}
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;
}
}
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;
}
}
Aggregations