Example usage for org.springframework.validation ValidationUtils rejectIfEmptyOrWhitespace

List of usage examples for org.springframework.validation ValidationUtils rejectIfEmptyOrWhitespace

Introduction

In this page you can find the example usage for org.springframework.validation ValidationUtils rejectIfEmptyOrWhitespace.

Prototype

public static void rejectIfEmptyOrWhitespace(Errors errors, String field, String errorCode) 

Source Link

Document

Reject the given field with the given error code if the value is empty or just contains whitespace.

Usage

From source file:org.jasig.portlet.ClassifiedsPortlet.domain.SubmitAdFormValidator.java

public void validate(Object target, Errors errors) {

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "category", "required.category");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "title", "required.title");
    ValidationUtils.rejectIfEmpty(errors, "startDate", "required.startDate");
    ValidationUtils.rejectIfEmpty(errors, "endDate", "required.endDate");
    ValidationUtils.rejectIfEmpty(errors, "description", "required.description");

    Ad form = (Ad) target;/*from w ww. j ava2s  . c  o m*/

    if (form.getPolicyAccepted() == false) {

        errors.rejectValue("policyAccepted", "required.policyAccepted");
    }

    if (form.getStartDate() != null && form.getEndDate() != null
            && !form.getStartDate().before(form.getEndDate())) {
        errors.rejectValue("startDate", "invalid.startDateAfterEndDate");
    }

    Calendar cal = Calendar.getInstance();
    Date today = new Date();

    cal.setTime(today);
    cal.add(Calendar.DATE, 60);
    Date maxStartDate = cal.getTime();

    if (form.getStartDate() != null && form.getStartDate().after(maxStartDate)) {
        errors.rejectValue("startDate", "invalid.startDatePastMaximumDays");
    }

    cal.setTime(today);
    cal.add(Calendar.DATE, 120);
    Date maxEndDate = cal.getTime();

    if (form.getEndDate() != null && form.getEndDate().after(maxEndDate)) {
        errors.rejectValue("startDate", "invalid.endDatePastMaximumDays");
    }

    if (form.getDescription().length() > 2056) {
        errors.rejectValue("description", "tolong.description");
    }
    ;

    if (form.getCategory() == null) {
        errors.rejectValue("category", "required.category");
    }
    ;

}

From source file:it.f2informatica.core.validator.ConsultantExperienceValidator.java

@Override
public void validate(Object target, Errors errors) {
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "companyName", FIELD_MANDATORY);
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "position", FIELD_MANDATORY);
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "locality", FIELD_MANDATORY);
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "monthFrom", FIELD_MANDATORY);
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "yearFrom", FIELD_MANDATORY);
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "description", FIELD_MANDATORY);
    doFurtherValidations(target, errors);
}

From source file:org.openmrs.module.facilitydata.validator.FacilityDataQuestionValidator.java

/**
* @see Validator#validate(Object, Errors)
*///from w w  w . ja  va2s  .  co  m
public void validate(Object o, Errors errors) {
    super.validate(o, errors);
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", "error.null");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "questionType", "error.null");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "periodApplicability", "error.null");
}

From source file:csns.web.validator.EditUserValidator.java

@Override
public void validate(Object target, Errors errors) {
    super.validate(target, errors);

    User user = (User) target;//from   www.j  a  v  a 2 s  .c  o  m
    Long id = user.getId();

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "username", "error.field.required");

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "primaryEmail", "error.field.required");

    String username = user.getUsername();
    if (StringUtils.hasText(username)) {
        User u = userDao.getUserByUsername(username);
        if (u != null && !u.getId().equals(id))
            errors.rejectValue("username", "error.user.username.taken");
    }

    String password1 = user.getPassword1();
    if (StringUtils.hasText(password1)) {
        if (password1.length() < 6)
            errors.rejectValue("password1", "error.user.password.short");
        if (!password1.equals(user.getPassword2()))
            errors.rejectValue("password2", "error.user.password.notmatch");
    }
}

From source file:org.openmrs.module.sdmxhdintegration.SDMXHDMessageValidator.java

/**
 * @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors)
 * @should accept if valid/*from  w w w .  ja  v a  2s . co  m*/
 * @should reject if name is empty
 * @should reject if zip file name is empty
 */
@Override
public void validate(Object obj, Errors e) {
    ValidationUtils.rejectIfEmptyOrWhitespace(e, "name", Constants.MODULE_ID + ".error.requiredField");
    ValidationUtils.rejectIfEmptyOrWhitespace(e, "zipFilename", Constants.MODULE_ID + ".error.requiredFile");
}

From source file:validation.EventLogValidator.java

/**
 * Validates various fields for an EventLog object
 * @param target//from   w  w  w.ja v a2s  . c o m
 * @param errors
 */
@Override
public void validate(Object target, Errors errors) {
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "clientid", "event.clientid.required");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userid", "event.userid.required");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "clientFirstName", "event.firstName.required");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "clientLastName", "event.lastName.required");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "username", "event.username.required");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "interaction", "event.interaction.required");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "date", "event.date.required");

    EventLog eventlog = (EventLog) target;
    if (eventlog.getClientFirstName() != null && eventlog.getClientFirstName().length() > 60) {
        errors.rejectValue("clientFirstName", "event.firstName.length");
    }
    if (eventlog.getClientFirstName() != null
            && !eventlog.getClientFirstName().matches("^[A-Z]'?[- a-zA-Z]([a-zA-Z])*$")) {
        errors.rejectValue("clientFirstName", "event.firstName.pattern");
    }
    if (eventlog.getClientLastName() != null && eventlog.getClientLastName().length() > 60) {
        errors.rejectValue("clientLastName", "event.lastName.length");
    }
    if (eventlog.getClientLastName() != null
            && !eventlog.getClientLastName().matches("^[A-Z]'?[- a-zA-Z]([a-zA-Z])*$")) {
        errors.rejectValue("clientLastName", "event.lastName.pattern");
    }
    if (eventlog.getUsername() != null && eventlog.getUsername().length() > 60) {
        errors.rejectValue("username", "event.username.length");
    }
    if (eventlog.getUsername() != null
            && !eventlog.getUsername().matches("^[a-zA-Z0-9]+([_ -]?[a-zA-Z0-9])*$")) {
        errors.rejectValue("username", "event.username.pattern");
    }
    if (eventlog.getDate() != null && !eventlog.getDate().matches("[0-9]{1,2}/[0-9]{1,2}/[0-9]{2,4}")) {
        errors.rejectValue("date", "event.date.pattern");
    }
}

From source file:org.openmrs.module.ejemplomedellin.web.controller.UserDetailsValidator.java

/**
 * @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors)
 *//*from  w w  w.  j  a v  a2  s  .c  o  m*/
@Override
public void validate(Object target, Errors errors) {
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "username", "error.null");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstName", "error.null");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "lastName", "error.null");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email", "error.null");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "confirmEmail", "error.null");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "citizenship", "error.null");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "error.null");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "confirmPassword", "error.null");
    ValidationUtils.rejectIfEmpty(errors, "birthdate", "error.null");

    UserDetails details = (UserDetails) target;
    if (details.getEmail() != null && details.getConfirmEmail() != null) {
        if (!details.getEmail().equals(details.getConfirmEmail()))
            errors.rejectValue("confirmEmail", "ejemplomedellin.error.not.same");
    }
    if (details.getUsername() != null && details.getPassword() != null) {
        try {
            OpenmrsUtil.validatePassword(details.getUsername(), details.getPassword(), null);
        } catch (PasswordException ex) {
            errors.rejectValue("password", ex.getMessage());
        }
    }
    if (details.getPassword() != null && details.getConfirmPassword() != null) {
        if (!details.getPassword().equals(details.getConfirmPassword()))
            errors.rejectValue("confirmPassword", "ejemplomedellin.error.not.same");
    }
}

From source file:org.openmrs.module.kenyaemr.ValidatingCommandObject.java

public void require(Errors errors, String field) {
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, field, "kenyaemr.error.required");
}

From source file:com.zergiu.tvman.controllers.validators.ConfigurationPageValidator.java

@Override
public void validate(Object target, Errors errors) {
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "location", "field.required");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "tvShowProvider", "field.required");

}

From source file:csns.web.validator.QuestionValidator.java

@Override
public void validate(Object target, Errors errors) {
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "description", "error.field.required");

    if (target instanceof ChoiceQuestion) {
        ChoiceQuestion question = (ChoiceQuestion) target;

        int minSelections = question.getMinSelections();
        int maxSelections = question.getMaxSelections();
        int numOfChoices = question.getNumOfChoices();

        if (maxSelections <= 0)
            maxSelections = 1;/*  w  ww .j  av a  2s.  c o  m*/
        if (maxSelections > numOfChoices)
            maxSelections = numOfChoices;
        if (minSelections < 0)
            minSelections = 0;
        if (minSelections > maxSelections)
            minSelections = maxSelections;

        question.setMinSelections(minSelections);
        question.setMaxSelections(maxSelections);
    }

    if (target instanceof RatingQuestion) {
        RatingQuestion question = (RatingQuestion) target;
        if (question.getMinRating() > question.getMaxRating())
            question.setMinRating(question.getMaxRating());
    }

    if (target instanceof TextQuestion) {
        TextQuestion question = (TextQuestion) target;
        if (question.getTextLength() < 1)
            question.setTextLength(1);
    }
}