Refine search
public void validate(Object obj, Errors errors) { CheckoutForm checkoutForm = (CheckoutForm) obj; ValidationUtils.rejectIfEmptyOrWhitespace(errors, "billingAddress.addressLine1", "addressLine1.required"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "billingAddress.phonePrimary", "phone.required"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "billingAddress.city", "city.required"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "billingAddress.postalCode", "postalCode.required"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "billingAddress.firstName", "firstName.required"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "billingAddress.lastName", "lastName.required"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "shippingAddress.addressLine1", "addressLine1.required"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "shippingAddress.city", "city.required"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "shippingAddress.postalCode", "postalCode.required"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "shippingAddress.firstName", "firstName.required"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "shippingAddress.lastName", "lastName.required"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "emailAddress", "emailAddress.required"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "creditCardNumber", "creditCardNumber.required"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "creditCardCvvCode", "creditCardCvvCode.required"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "creditCardExpMonth", "creditCardExpMonth.required"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "creditCardExpYear", "creditCardExpYear.required"); if (!errors.hasErrors()) { if (!GenericValidator.isEmail(checkoutForm.getEmailAddress())) { errors.rejectValue("emailAddress", "emailAddress.invalid", null, null); } } } }
/** * Invoke the given {@link Validator} for the supplied object and * {@link Errors} instance. * @param validator the {@code Validator} to be invoked * @param target the object to bind the parameters to * @param errors the {@link Errors} instance that should store the errors * @throws IllegalArgumentException if either of the {@code Validator} or {@code Errors} * arguments is {@code null}, or if the supplied {@code Validator} does not * {@link Validator#supports(Class) support} the validation of the supplied object's type */ public static void invokeValidator(Validator validator, Object target, Errors errors) { invokeValidator(validator, target, errors, (Object[]) null); }
/** * Reject the given field with the given error code if the value is empty. * <p>An 'empty' value in this context means either {@code null} or * the empty string "". * <p>The object whose field is being validated does not need to be passed * in because the {@link Errors} instance can resolve field values by itself * (it will usually hold an internal reference to the target object). * @param errors the {@code Errors} instance to register errors on * @param field the field name to check * @param errorCode the error code, interpretable as message key */ public static void rejectIfEmpty(Errors errors, String field, String errorCode) { rejectIfEmpty(errors, field, errorCode, null, null); }
public void validate(BroadleafFormType formType, Address address, Errors errors) { if (isCustomValidationEnabled()) { validatorExtensionManager.getProxy().validate(formType, address, errors); return; } ValidationUtils.rejectIfEmptyOrWhitespace(errors, "address.addressLine1", "addressLine1.required"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "address.city", "city.required"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "address.stateProvinceRegion", "state.required"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "address.postalCode", "postalCode.required"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "address.phonePrimary.phoneNumber", "phonePrimary.required"); if (isValidateFullNameOnly()) { ValidationUtils.rejectIfEmptyOrWhitespace(errors, "address.fullName", "fullName.required"); } else { ValidationUtils.rejectIfEmptyOrWhitespace(errors, "address.firstName", "firstName.required"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "address.lastName", "lastName.required"); } if (address.getIsoCountryAlpha2() == null && address.getCountry() == null) { errors.rejectValue("address.isoCountryAlpha2", "country.required", null, null); } }
public void validate(Customer customer, String password, String passwordConfirm, Errors errors) { ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "password.required"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "passwordConfirm", "passwordConfirm.required"); errors.pushNestedPath("customer"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstName", "firstName.required"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "lastName", "lastName.required"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "emailAddress", "emailAddress.required"); errors.popNestedPath(); if (errors.hasErrors()){ if (!passwordConfirm.equals(password)) { errors.rejectValue("passwordConfirm", "invalid"); } if (!customer.getFirstName().matches(getValidNameRegex())) { errors.rejectValue("firstName", "firstName.invalid", null, null); } if (!customer.getLastName().matches(getValidNameRegex())) { errors.rejectValue("lastName", "lastName.invalid", null, null); } if (!customer.getPassword().matches(getValidPasswordRegex())) { errors.rejectValue("password", "password.invalid", null, null); } if (!password.equals(passwordConfirm)) { errors.rejectValue("password", "passwordConfirm.invalid", null, null); } if (!GenericValidator.isEmail(customer.getEmailAddress())) { errors.rejectValue("emailAddress", "emailAddress.invalid", null, null); } } }
errors.reject("Concept.name.atLeastOneRequired"); return; ValidationUtils.rejectIfEmpty(errors, "datatype", "Concept.datatype.empty"); ValidationUtils.rejectIfEmpty(errors, "conceptClass", "Concept.conceptClass.empty"); log.debug("Name in locale '" + conceptNameLocale.toString() + "' cannot be an empty string or white space"); errors.reject("Concept.name.empty"); log.warn("Preferred name in locale '" + conceptNameLocale.toString() + "' shouldn't be an index term"); errors.reject("Concept.error.preferredName.is.indexTerm"); } else if (nameInLocale.isShort()) { log.warn("Preferred name in locale '" + conceptNameLocale.toString() errors.pushNestedPath("conceptMappings[" + index + "].conceptReferenceTerm"); ValidationUtils.invokeValidator(new ConceptReferenceTermValidator(), map.getConceptReferenceTerm(), errors);
/** * Checks the form object for any inconsistencies/errors * * @see org.springframework.validation.Validator#validate(java.lang.Object, * org.springframework.validation.Errors) * @should fail validation if personMergeLogData is null * @should fail validation if winner is null * @should fail validation if loser is null * @should pass validation if all fields are correct * @should pass validation if field lengths are correct * @should fail validation if field lengths are not correct */ @Override public void validate(Object obj, Errors errors) { PersonMergeLog personMergeLog = (PersonMergeLog) obj; if (personMergeLog == null) { errors.rejectValue("persnMergeLog", "error.general"); } else { ValidationUtils.rejectIfEmpty(errors, "personMergeLogData", "error.null"); ValidationUtils.rejectIfEmpty(errors, "winner", "error.null"); ValidationUtils.rejectIfEmpty(errors, "loser", "error.null"); ValidateUtil.validateFieldLengths(errors, obj.getClass(), "voidReason"); } }
private Errors validate(String event, Object entity) { if (entity == null) { return null; } Errors errors = new ValidationErrors(entity, persistentEntitiesFactory.getObject()); for (Validator validator : getValidatorsForEvent(event)) { if (validator.supports(entity.getClass())) { LOGGER.debug("{}: {} with {}", event, entity, validator); ValidationUtils.invokeValidator(validator, entity, errors); } } if (errors.hasErrors()) { throw new RepositoryConstraintViolationException(errors); } return errors; }
errors.rejectValue("drugReferenceMaps[" + index + "].drug", "Drug.drugReferenceMap.mappedDrug"); errors.rejectValue("drugReferenceMaps[" + index + "].conceptReferenceTerm", "Drug.drugReferenceMap.conceptReferenceTerm"); } else if (referenceTerm.getConceptReferenceTermId() == null) { try { errors.pushNestedPath("drugReferenceMaps[" + index + "].conceptReferenceTerm"); ValidationUtils.invokeValidator(new ConceptReferenceTermValidator(), referenceTerm, errors); try { errors.pushNestedPath("drugReferenceMaps[" + index + "].conceptMapType"); ValidationUtils.invokeValidator(new ConceptMapTypeValidator(), mapType, errors);
OrderFrequency orderFrequency = (OrderFrequency) obj; if (orderFrequency == null) { errors.reject("error.general"); } else { ValidationUtils.rejectIfEmpty(errors, "concept", "Concept.noConceptSelected"); errors.rejectValue("concept", "OrderFrequency.concept.shouldBeClassFrequency"); errors.rejectValue("concept", "OrderFrequency.concept.shouldNotBeShared");
ValidationUtils.rejectIfEmpty(errors, "patient", "error.required", new Object[] { mss.getMessage("general.patient") }); ValidationUtils.rejectIfEmpty(errors, "program", "error.required", new Object[] { mss.getMessage("Program.program") }); if (errors.hasErrors()) { return; ValidationUtils.rejectIfEmpty(errors, "dateEnrolled", "error.patientProgram.enrolledDateEmpty"); errors.rejectValue("dateEnrolled", "error.patientProgram.enrolledDateDateCannotBeInFuture"); errors.rejectValue("dateCompleted", "error.patientProgram.completionDateCannotBeInFuture");
/** * @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors) * @should fail validation if aIsToB(or A is To B) is null or empty or whitespace * @should fail validation if bIsToA(or B is To A) is null or empty or whitespace * @should fail validation if description is null or empty or whitespace * @should pass validation if all required fields are set * @should fail validation if relationshipTypeName already exist * @should pass validation if field lengths are correct * @should fail validation if field lengths are not correct */ @Override public void validate(Object obj, Errors errors) { RelationshipType relationshipType = (RelationshipType) obj; if (relationshipType == null) { errors.rejectValue("relationshipType", "error.general"); } else { ValidationUtils.rejectIfEmptyOrWhitespace(errors, "aIsToB", "RelationshipType.aIsToB.required"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "bIsToA", "RelationshipType.bIsToA.required"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "description", "RelationshipType.description.required"); RelationshipType exist = Context.getPersonService().getRelationshipTypeByName( relationshipType.getaIsToB() + "/" + relationshipType.getbIsToA()); if (exist != null && !exist.getRetired() && !OpenmrsUtil.nullSafeEquals(relationshipType.getUuid(), exist.getUuid())) { errors.reject("duplicate.relationshipType"); } ValidateUtil.validateFieldLengths(errors, obj.getClass(), "aIsToB", "bIsToA", "description", "retireReason"); } } }
boolean atLeastOneNonVoidPersonNameLeft = false; for (PersonName personName : person.getNames()) { errors.pushNestedPath("names[" + index + "]"); personNameValidator.validate(personName, errors); if (!personName.getVoided()) { atLeastOneNonVoidPersonNameLeft = true; errors.popNestedPath(); index++; errors.rejectValue("names", "Person.shouldHaveAtleastOneNonVoidedName"); try { errors.pushNestedPath("addresses[" + index + "]"); ValidationUtils.invokeValidator(personAddressValidator, address, errors); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "voidReason", "error.null");
/** * Reject the given field with the given error code if the value is empty * or just contains whitespace. * <p>An 'empty' value in this context means either {@code null}, * the empty string "", or consisting wholly of whitespace. * <p>The object whose field is being validated does not need to be passed * in because the {@link Errors} instance can resolve field values by itself * (it will usually hold an internal reference to the target object). * @param errors the {@code Errors} instance to register errors on * @param field the field name to check * @param errorCode the error code, interpretable as message key */ public static void rejectIfEmptyOrWhitespace(Errors errors, String field, String errorCode) { rejectIfEmptyOrWhitespace(errors, field, errorCode, null, null); }
ValidationUtils.rejectIfEmpty(errors, "encounterType", "Encounter.error.encounterType.required", "Encounter type is Required"); ValidationUtils.rejectIfEmpty(errors, "patient", "Encounter.error.patient.required", "Patient is required"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "encounterDatetime", "Encounter.datetime.required"); if (encounter.getVisit() != null && !ObjectUtils.equals(encounter.getVisit().getPatient(), encounter.getPatient())) { errors.rejectValue("visit", "Encounter.visit.patients.dontMatch", "The patient for the encounter and visit should be the same"); errors.rejectValue("encounterDatetime", "Encounter.datetimeShouldBeBeforeCurrent", "The encounter datetime should be before the current date."); if (visit != null && encounterDateTime != null) { if (visit.getStartDatetime() != null && encounterDateTime.before(visit.getStartDatetime())) { errors.rejectValue("encounterDatetime", "Encounter.datetimeShouldBeInVisitDatesRange", "The encounter datetime should be between the visit start and stop dates.");
@Test public void testValidationUtilsEmptyOrWhitespaceVariants() { TestBean tb = new TestBean(); tb.setName(" "); Errors errors = new BeanPropertyBindingResult(tb, "tb"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "EMPTY_OR_WHITESPACE", new Object[] {"arg"}); assertTrue(errors.hasFieldErrors("name")); assertEquals("EMPTY_OR_WHITESPACE", errors.getFieldError("name").getCode()); assertEquals("arg", errors.getFieldError("name").getArguments()[0]); errors = new BeanPropertyBindingResult(tb, "tb"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "EMPTY_OR_WHITESPACE", new Object[] {"arg"}, "msg"); assertTrue(errors.hasFieldErrors("name")); assertEquals("EMPTY_OR_WHITESPACE", errors.getFieldError("name").getCode()); assertEquals("arg", errors.getFieldError("name").getArguments()[0]); assertEquals("msg", errors.getFieldError("name").getDefaultMessage()); }
Order order = (Order) obj; if (order == null) { errors.reject("error.general"); } else { ValidationUtils.rejectIfEmpty(errors, "voided", "error.null"); ValidationUtils.rejectIfEmpty(errors, "concept", "Concept.noConceptSelected"); ValidationUtils.rejectIfEmpty(errors, "patient", "error.null"); ValidationUtils.rejectIfEmpty(errors, "encounter", "error.null"); ValidationUtils.rejectIfEmpty(errors, "orderer", "error.null"); ValidationUtils.rejectIfEmpty(errors, "urgency", "error.null"); ValidationUtils.rejectIfEmpty(errors, "action", "error.null");
@Test public void testInvokeValidatorSunnyDay() throws Exception { TestBean tb = new TestBean(); Errors errors = new BeanPropertyBindingResult(tb, "tb"); ValidationUtils.invokeValidator(new EmptyValidator(), tb, errors); assertTrue(errors.hasFieldErrors("name")); assertEquals("EMPTY", errors.getFieldError("name").getCode()); }
@Test public void testValidationUtilsEmptyVariants() { TestBean tb = new TestBean(); Errors errors = new BeanPropertyBindingResult(tb, "tb"); ValidationUtils.rejectIfEmpty(errors, "name", "EMPTY_OR_WHITESPACE", new Object[] {"arg"}); assertTrue(errors.hasFieldErrors("name")); assertEquals("EMPTY_OR_WHITESPACE", errors.getFieldError("name").getCode()); assertEquals("arg", errors.getFieldError("name").getArguments()[0]); errors = new BeanPropertyBindingResult(tb, "tb"); ValidationUtils.rejectIfEmpty(errors, "name", "EMPTY_OR_WHITESPACE", new Object[] {"arg"}, "msg"); assertTrue(errors.hasFieldErrors("name")); assertEquals("EMPTY_OR_WHITESPACE", errors.getFieldError("name").getCode()); assertEquals("arg", errors.getFieldError("name").getArguments()[0]); assertEquals("msg", errors.getFieldError("name").getDefaultMessage()); }
@Override public void validate(Object target, Errors errors) { ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "container.platform.error.required"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "description", "container.platform.error.required"); ValidationUtils.rejectIfEmptyOrWhitespace(errors, "descriptionEn", "container.platform.error.required"); ValidationUtils.rejectIfEmpty(errors, "infonaSupported", "container.platform.error.required"); }