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:com.nagarro.core.validator.PaymentDetailsDTOValidator.java

@Override
public void validate(final Object target, final Errors errors) {
    final PaymentDetailsWsDTO paymentDetails = (PaymentDetailsWsDTO) target;

    if (StringUtils.isNotBlank(paymentDetails.getStartMonth())
            && StringUtils.isNotBlank(paymentDetails.getStartYear())
            && StringUtils.isNotBlank(paymentDetails.getExpiryMonth())
            && StringUtils.isNotBlank(paymentDetails.getExpiryYear())) {
        final Calendar start = Calendar.getInstance();
        start.set(Calendar.DAY_OF_MONTH, 0);
        start.set(Calendar.MONTH, Integer.parseInt(paymentDetails.getStartMonth()) - 1);
        start.set(Calendar.YEAR, Integer.parseInt(paymentDetails.getStartYear()) - 1);

        final Calendar expiration = Calendar.getInstance();
        expiration.set(Calendar.DAY_OF_MONTH, 0);
        expiration.set(Calendar.MONTH, Integer.parseInt(paymentDetails.getExpiryMonth()) - 1);
        expiration.set(Calendar.YEAR, Integer.parseInt(paymentDetails.getExpiryYear()) - 1);

        if (start.after(expiration)) {
            errors.rejectValue("startMonth", "payment.startDate.invalid");
        }/* w w w  .j  a v a2  s.c  o m*/
    }

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "accountHolderName", "field.required");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "cardType.code", "field.required");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "cardNumber", "field.required");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "expiryMonth", "field.required");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "expiryYear", "field.required");

    paymentAddressValidator.validate(paymentDetails, errors);
}

From source file:com.ssbusy.register.validator.MyRegisterCustomerValidator.java

public void validate(Object obj, Errors errors, boolean useEmailForUsername) {
    RegisterCustomerForm form = (RegisterCustomerForm) obj;

    Customer customerFromDb = customerService.readCustomerByUsername(form.getCustomer().getUsername());

    if (customerFromDb != null) {
        if (useEmailForUsername) {
            errors.rejectValue("customer.emailAddress", "emailAddress.used", null, null);
        } else {/*w  w  w  .  j av a 2s. c om*/
            errors.rejectValue("customer.username", "username.used", null, null);
        }
    }

    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 (form.getPassword().length() < 4 || form.getPassword().length() > 15) {
            errors.rejectValue("password", "password.invalid", null, null);
        }

        if (!form.getPassword().equals(form.getPasswordConfirm())) {
            errors.rejectValue("password", "passwordConfirm.invalid", null, null);
        }

        if (!GenericValidator.isEmail(form.getCustomer().getEmailAddress())) {
            errors.rejectValue("customer.emailAddress", "emailAddress.invalid", null, null);
        }
    }
}

From source file:com.tasktop.c2c.server.tasks.domain.validation.TaskValidator.java

public void validate(Object target, Errors errors) {

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "shortDescription", "field.required");

    Task task = (Task) target;//  w w w  . j av a  2s  . co m
    if (task.getSeverity() == null || isEmpty(task.getSeverity().getValue())) {
        errors.rejectValue("severity", "field.required");
    }
    if (task.getTaskType() == null || isEmpty(task.getTaskType())) {
        errors.rejectValue("taskType", "field.required");
    }
    if (task.getIteration() == null || isEmpty(task.getIteration().getValue())) {
        errors.rejectValue("iteration", "field.required");
    }
    if (task.getStatus() == null || isEmpty(task.getStatus().getValue())) {
        errors.rejectValue("status", "field.required");
    } else {
        // when resolved, a resolution is required
        if ("RESOLVED".equalsIgnoreCase(task.getStatus().getValue())) {
            if (task.getResolution() == null || isEmpty(task.getResolution().getValue())) {
                errors.rejectValue("resolution", "field.required");
            }
        } else {
            if (task.getStatus().isOpen() && task.getResolution() != null
                    && !isEmpty(task.getResolution().getValue())) {
                errors.rejectValue("resolution", "field.prohibited");
            }
        }
    }
    if (task.getPriority() == null || isEmpty(task.getPriority().getValue())) {
        errors.rejectValue("priority", "field.required");
    }
    if (task.getMilestone() == null || isEmpty(task.getMilestone().getValue())) {
        errors.rejectValue("milestone", "field.required");
    }
    if (task.getComponent() == null || task.getComponent().getId() == null) {
        errors.rejectValue("component", "field.required");
    }
    if (task.getProduct() == null || task.getProduct().getId() == null) {
        errors.rejectValue("product", "field.required");
    }

    // allow null assignee as we set this to the component default
    if (task.getAssignee() != null && task.getAssignee().getLoginName() == null) {
        errors.rejectValue("assignee", "field.empty");
    }

    if (task.getEstimatedTime() != null && task.getEstimatedTime().signum() < 0) {
        errors.rejectValue("estimatedTime", "nonNegative");
    }
    if (task.getRemainingTime() != null && task.getRemainingTime().signum() < 0) {
        errors.rejectValue("remainingTime", "nonNegative");
    }

    // Validate the sub objects.

    // Validate Comments
    Comment curComment = null;
    for (int i = 0; i < (task.getComments() == null ? 0 : task.getComments().size()); i++) {
        curComment = task.getComments().get(i);
        if (curComment.getId() != null) {
            continue; // Only validate comments that we save
        }
        errors.pushNestedPath("comments[" + i + "]");
        commentValidator.validate(curComment, errors);
        errors.popNestedPath();
    }

    // Validate WorkLogs
    WorkLog workLog = null;
    for (int i = 0; i < (task.getWorkLogs() == null ? 0 : task.getWorkLogs().size()); i++) {
        workLog = task.getWorkLogs().get(i);
        if (workLog.getId() != null) {
            continue; // Only validate worklogs that we save
        }
        errors.pushNestedPath("workLogs[" + i + "]");
        workLogValidator.validate(workLog, errors);
        errors.popNestedPath();
    }

    validateTaskReference(task, task.getDuplicateOf(), "duplicateOf", errors);
    validateTaskReference(task, task.getParentTask(), "parentTask", errors);

    if (task.getSubTasks() != null) {
        for (int i = 0; i < task.getSubTasks().size(); i++) {
            Task subTask = task.getSubTasks().get(i);
            validateTaskReference(task, subTask, "subTasks[" + i + "]", errors);
            if (task.getParentTask() != null && task.getParentTask().equals(subTask)) {
                errors.reject("task.depAndBlock");
            } else if (task.getBlocksTasks() != null && task.getBlocksTasks().contains(subTask)) {
                errors.reject("task.depAndBlock");
            }
        }
    }

    if (task.getBlocksTasks() != null) {
        for (int i = 0; i < task.getBlocksTasks().size(); i++) {
            validateTaskReference(task, task.getBlocksTasks().get(i), "blocksTasks[" + i + "]", errors);
        }
    }

    if (task.getWatchers() != null) {
        for (int i = 0; i < task.getWatchers().size(); i++) {
            TaskUserProfile profile = task.getWatchers().get(i);
            if (profile.getLoginName() == null) {
                errors.rejectValue("watchers[" + i + "]", "field.empty");
            }
        }
    }

    if (task.getExternalTaskRelations() != null) {
        for (int i = 0; i < task.getExternalTaskRelations().size(); i++) {
            ExternalTaskRelation relation = task.getExternalTaskRelations().get(i);
            if (!externalAssociationUrlValidator.isValid(relation.getUri())) {
                errors.rejectValue("externalTaskRelations[" + i + "].uri", "invalid");
            }
        }
    }
}

From source file:cz.zcu.kiv.eegdatabase.logic.controller.article.AddArticleCommentController.java

public void validate(Object command, Errors errors) {
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "text", "required.field");
}

From source file:cz.zcu.kiv.eegdatabase.logic.controller.experiment.AddExperimentOptParamValValidator.java

public void validate(Object command, Errors errors) {
    AddExperimentOptParamValCommand data = (AddExperimentOptParamValCommand) command;

    if ((!auth.userIsOwnerOrCoexperimenter(data.getMeasurationFormId())) && (!auth.isAdmin())) {
        // First check whether the user has permission to add data
        errors.reject("error.mustBeOwnerOfExperimentOrCoexperimenter");
    } else {//from ww w  .ja  v a 2 s .  c  o m

        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "paramValue", "required.field");

        if (data.getParamId() < 0) {
            errors.rejectValue("paramId", "required.field");
        }

        ExperimentOptParamVal val = experimentOptParamValDao
                .read(new ExperimentOptParamValId(data.getMeasurationFormId(), data.getParamId()));
        if (val != null) { // field already exists
            errors.rejectValue("paramId", "invalid.paramIdAlreadyInserted");
        }

    }

}

From source file:cz.zcu.kiv.eegdatabase.logic.controller.experiment.AddExperimentValidator.java

public void validate(Object command, Errors errors) {
    AddExperimentCommand data = (AddExperimentCommand) command;

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "startDate", "required.date");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "startTime", "required.time");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "endDate", "required.date");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "endTime", "required.time");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "temperature", "required.field");

    if (data.getMeasurationId() > 0) {
        // Edit of existing experiment
        // No special actions yet
    } else {//w  w  w .j  a v  a  2 s.co  m
        // Creating new experiment
        if (data.getResearchGroup() == -1) {
            // research group not chosen
            errors.rejectValue("researchGroup", "required.researchGroup");
        } else if (!auth.personAbleToWriteIntoGroup(data.getResearchGroup())) {
            errors.rejectValue("researchGroup", "invalid.notAbleToAddExperimentInGroup");
        }
    }

    if (data.getSubjectPerson() == -1) { // measured person not chosen
        errors.rejectValue("subjectPerson", "required.subjectPerson");
    }

    if (data.getScenario() == -1) { // scenario not selected
        errors.rejectValue("scenario", "required.scenario");
    }

    if (data.getHardware().length == 0) { // no hardware selected
        errors.rejectValue("hardware", "required.hardware");
    }

    if (data.getWeather() == -1) { // weather not selected
        errors.rejectValue("weather", "required.weather");
    }

    try {
        ControllerUtils.getDateFormat().parse(data.getStartDate());
    } catch (ParseException ex) {
        errors.rejectValue("startDate", "invalid.date");
    }

    try {
        ControllerUtils.getDateFormat().parse(data.getEndDate());
    } catch (ParseException ex) {
        errors.rejectValue("endDate", "invalid.date");
    }

    try {
        ControllerUtils.getTimeFormat().parse(data.getStartTime());
    } catch (ParseException ex) {
        errors.rejectValue("startTime", "invalid.time");
    }

    try {
        ControllerUtils.getTimeFormat().parse(data.getEndTime());
    } catch (ParseException ex) {
        errors.rejectValue("endTime", "invalid.time");
    }

    try {
        int temp = Integer.parseInt(data.getTemperature());
        if (temp < -273) {
            errors.rejectValue("temperature", "invalid.minTemp");
        }
    } catch (NumberFormatException e) {
        errors.rejectValue("temperature", "invalid.temperature");
    }

}

From source file:cz.zcu.kiv.eegdatabase.logic.controller.experiment.AddExperimentWizardController.java

@Override
protected void validatePage(Object command, Errors errors, int page) {
    AddExperimentWizardCommand data = (AddExperimentWizardCommand) command;

    switch (page) {
    case 0: //if page 1 , go validate with validatePage1Form
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "startDate", "required.date");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "startTime", "required.time");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "endDate", "required.date");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "endTime", "required.time");
        if (data.getResearchGroup() == -1) {
            // research group not chosen
            errors.rejectValue("researchGroup", "required.researchGroup");
        } else if (!auth.personAbleToWriteIntoGroup(data.getResearchGroup())) {
            errors.rejectValue("researchGroup", "invalid.notAbleToAddExperimentInGroup");
        }//from  w  ww . ja  v a 2  s  . com
        if (data.getSubjectPerson() == -1) { // measured person not chosen
            errors.rejectValue("subjectPerson", "required.subjectPerson");
        }
        try {
            ControllerUtils.getDateFormat().parse(data.getStartDate());
        } catch (ParseException ex) {
            errors.rejectValue("startDate", "invalid.date");
        }

        try {
            ControllerUtils.getDateFormat().parse(data.getEndDate());
        } catch (ParseException ex) {
            errors.rejectValue("endDate", "invalid.date");
        }

        try {
            ControllerUtils.getTimeFormat().parse(data.getStartTime());
        } catch (ParseException ex) {
            errors.rejectValue("startTime", "invalid.time");
        }

        try {
            ControllerUtils.getTimeFormat().parse(data.getEndTime());
        } catch (ParseException ex) {
            errors.rejectValue("endTime", "invalid.time");
        }

        break;
    case 1: //if page 2 , go validate with validatePage2Form
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "temperature", "required.field");
        if (data.getScenario() == -1) { // scenario not selected
            errors.rejectValue("scenario", "required.scenario");
        }

        if (data.getHardware().length == 0) { // no hardware selected
            errors.rejectValue("hardware", "required.hardware");
        }

        if (data.getWeather() == -1) { // weather not selected
            errors.rejectValue("weather", "required.weather");
        }
        try {
            int temp = Integer.parseInt(data.getTemperature());
            if (temp < -273) {
                errors.rejectValue("temperature", "invalid.minTemp");
            }
        } catch (NumberFormatException e) {
            errors.rejectValue("temperature", "invalid.temperature");
        }
        break;
    case 2: //if page 3 , go validate with validatePage3Form
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "samplingRate", "required.samplingRate");
        try {
            double rate = Double.parseDouble(data.getSamplingRate());
            if (rate <= 0) {
                errors.rejectValue("samplingRate", "invalid.positiveRate");
            }
        } catch (NumberFormatException e) {
            errors.rejectValue("samplingRate", "invalid.invalidRate");
        }
        MultipartFile file = data.getDataFile();
        if (file.getOriginalFilename().length() == 0) {
            errors.rejectValue("dataFile", "required.dataFile");
        }

        break;
    }

}

From source file:cz.zcu.kiv.eegdatabase.logic.controller.experiment.AddFileMetadataValidator.java

public void validate(Object command, Errors errors) {
    AddFileMetadataCommand addFileMetadataCommand = (AddFileMetadataCommand) command;
    log.debug("Validating form for adding file metadata.");

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "paramValue", "required.paramValue");

    if (addFileMetadataCommand.getParamId() <= 0) {
        errors.rejectValue("paramId", "required.paramId");
    }//www . j a  v  a 2s  .c o  m

    FileMetadataParamVal f = fileMetadataDao.read(new FileMetadataParamValId(
            addFileMetadataCommand.getParamId(), addFileMetadataCommand.getDataId()));
    if (f != null) {
        errors.rejectValue("paramId", "invalid.paramIdAlreadyInserted");
    }
}

From source file:cz.zcu.kiv.eegdatabase.logic.controller.experiment.DataFileDetailController.java

public void validate(Object command, Errors errors) {
    AddFileMetadataCommand data = (AddFileMetadataCommand) command;
    log.debug("Validating form for adding file metadata.");

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "paramValue", "required.paramValue");

    if (data.getParamId() <= 0) {
        errors.rejectValue("paramId", "required.paramId");
    }//from w ww. j  a va2  s.com

    FileMetadataParamVal f = fileMetadataParamValDao
            .read(new FileMetadataParamValId(data.getParamId(), data.getDataId()));
    if (f != null) {
        errors.rejectValue("paramId", "invalid.paramIdAlreadyInserted");
    }
}

From source file:cz.zcu.kiv.eegdatabase.logic.controller.list.artifact.AddArtifactValidator.java

@Override
public void validate(Object o, Errors errors) {
    AddArtifactCommand data = (AddArtifactCommand) o;

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "compensation", "required.field");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "rejectCondition", "required.field");

}