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:cz.zcu.kiv.eegdatabase.logic.controller.list.experimentoptparamdef.AddExperimentOptParamDefValidator.java

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

From source file:cz.zcu.kiv.eegdatabase.logic.controller.list.hardware.AddHardwareValidator.java

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

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "title", "required.field");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "type", "required.field");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "description", "required.field");

    if (data.getResearchGroupId() == DEFAULT_ID) {
        if (!hardwareDao.canSaveDefaultTitle(data.getTitle(), data.getId())) {
            errors.rejectValue("title", "error.valueAlreadyInDatabase");
        }// ww w.  j  a  v a2  s.co m
    } else {
        if (!hardwareDao.canSaveTitle(data.getTitle(), data.getResearchGroupId(), data.getId())) {
            errors.rejectValue("title", "error.valueAlreadyInDatabase");
        }
    }
}

From source file:cz.zcu.kiv.eegdatabase.logic.controller.list.weather.AddWeatherValidator.java

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

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "title", "required.field");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "description", "required.field");

    if (data.getTitle().length() > 30) {
        errors.rejectValue("title", "invalid.fieldLength30");
    }//from  w  w w.  j a  v a2 s  .  co m
    if (data.getDescription().length() > 30) {
        errors.rejectValue("description", "invalid.fieldLength30");
    }

    if (data.getResearchGroupId() == DEFAULT_ID) {
        if (!weatherDao.canSaveDefaultTitle(data.getTitle(), data.getId())) {
            errors.rejectValue("title", "error.valueAlreadyInDatabase");
        }
        if (!weatherDao.canSaveDefaultDescription(data.getDescription(), data.getId())) {
            errors.rejectValue("description", "error.valueAlreadyInDatabase");
        }
    } else {
        if (!weatherDao.canSaveTitle(data.getTitle(), data.getResearchGroupId(), data.getId())) {
            errors.rejectValue("title", "error.valueAlreadyInDatabase");
        }
        if (!weatherDao.canSaveDescription(data.getTitle(), data.getResearchGroupId(), data.getId())) {
            errors.rejectValue("description", "error.valueAlreadyInDatabase");
        }
    }

}

From source file:cz.zcu.kiv.eegdatabase.logic.controller.myaccount.ApplyForWritingPermissionValidator.java

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

From source file:cz.zcu.kiv.eegdatabase.logic.controller.myaccount.ChangePasswordValidator.java

public void validate(Object command, Errors errors) {
    log.debug("Started validation of My account form");
    ChangePasswordCommand changePasswordCommand = (ChangePasswordCommand) command;

    Person user = personDao.getPerson(ControllerUtils.getLoggedUserName());
    log.debug("Matching inserted old password and actual password in database [" + user.getPassword() + "]");
    if (!encoder.matches(changePasswordCommand.getOldPassword(), user.getPassword())) {
        log.debug("Inserted password REJECTED");
        errors.rejectValue("oldPassword", "invalid.oldPassword");
    }// w  w w  .j  a  v  a 2  s  .com

    log.debug("Validation whether the new password differs from the old one");
    if (changePasswordCommand.getNewPassword().equals(changePasswordCommand.getOldPassword())) {
        log.debug("New and old passwords are the same");
        errors.rejectValue("newPassword", "invalid.newAndOldPasswordsAreTheSame");
    }

    log.debug("Validating new password if it is not empty");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "newPassword", "required.field");

    log.debug("Validating twice inserted new password");
    if (!changePasswordCommand.getNewPassword().equals(changePasswordCommand.getNewPassword2())) {
        log.debug("New passwords don't match");
        errors.rejectValue("newPassword2", "invalid.passwordMatch");
    }

    if (changePasswordCommand.getNewPassword().length() < ControllerUtils.MINIMUM_PASSWORD_LENGTH) {
        errors.rejectValue("newPassword", "invalid.minimumPasswordLength6");
    }
}

From source file:cz.zcu.kiv.eegdatabase.logic.controller.person.AddPersonAdditionalParamValueValidator.java

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

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

    if (data.getParamId() < 0) {
        errors.rejectValue("paramId", "required.field");
    }//from w  w  w . j a  v  a  2  s . co  m

    PersonOptParamVal val = personOptParamValDao
            .read(new PersonOptParamValId(data.getPersonFormId(), data.getParamId()));
    if (val != null) { // field already exists
        errors.rejectValue("paramId", "invalid.paramIdAlreadyInserted");
    }
}

From source file:cz.zcu.kiv.eegdatabase.logic.controller.person.AddPersonValidator.java

public void validate(Object command, Errors errors) {
    AddPersonCommand apc = (AddPersonCommand) command;

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "givenname", "required.field");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "surname", "required.field");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dateOfBirth", "required.field");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "gender", "required.field");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email", "required.field");

    try {/*  ww  w. j a  va 2s  .com*/
        Date d = ControllerUtils.getDateFormat().parse(apc.getDateOfBirth());
        if (d.getTime() >= System.currentTimeMillis()) {
            errors.rejectValue("dateOfBirth", "invalid.dateOfBirth");
        }
    } catch (ParseException e) {
        errors.rejectValue("dateOfBirth", "invalid.dateOfBirth");
    }
    if (apc.getNote().length() > 255) {
        errors.rejectValue("note", "invalid.maxLength");
    }
    if (!apc.getPhoneNumber().isEmpty()) {
        try {
            if (apc.getPhoneNumber().charAt(0) == '+') {
                Long.parseLong(apc.getPhoneNumber().substring(1));
            } else {
                Long.parseLong(apc.getPhoneNumber());
            }

        } catch (NumberFormatException ex) {
            errors.rejectValue("phoneNumber", "invalid.phoneNumber");
        }

    }
    if (!Pattern.matches("[a-zA-Z][a-zA-Z\\s]*", apc.getGivenname())) {
        errors.rejectValue("givenname", "invalid.givenname");
    }
    if (!Pattern.matches("[a-zA-Z][a-zA-Z\\s]*", apc.getSurname())) {
        errors.rejectValue("surname", "invalid.surname");
    }

    if (!Pattern.matches("^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$", apc.getEmail())) {
        errors.rejectValue("email", "invalid.email");
    }
    if (!apc.getEmail().equals(apc.getOldEmail())) {
        if (personDao.usernameExists(apc.getEmail())) {
            errors.rejectValue("email", "inUse.email");
        }
    }
    apc.setOldEmail("");
}

From source file:cz.zcu.kiv.eegdatabase.logic.controller.root.RegistrationValidator.java

public void validate(Object command, Errors errors) {
    RegistrationCommand registrationCommand = (RegistrationCommand) command;

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "givenname", "required.field");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "surname", "required.field");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "dateOfBirth", "required.field");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "gender", "required.field");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email", "required.field");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "required.field");

    try {/*from w  ww.  jav a  2s . c  o m*/
        Date d = ControllerUtils.getDateFormat().parse(registrationCommand.getDateOfBirth());
        if (d.getTime() >= System.currentTimeMillis()) {
            errors.rejectValue("dateOfBirth", "invalid.dateOfBirth");
        }
    } catch (ParseException e) {
        errors.rejectValue("dateOfBirth", "invalid.dateOfBirth");
    }

    if (!registrationCommand.getPassword().equals(registrationCommand.getPassword2())) {
        errors.rejectValue("password2", "invalid.passwordMatch");
    }
    if (!Pattern.matches("[a-zA-Z][a-zA-Z\\s]*", registrationCommand.getGivenname())) {
        errors.rejectValue("givenname", "invalid.givenname");
    }
    if (!Pattern.matches("[a-zA-Z][a-zA-Z\\s]*", registrationCommand.getSurname())) {
        errors.rejectValue("surname", "invalid.surname");
    }

    if (!Pattern.matches("^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$", registrationCommand.getEmail())) {
        errors.rejectValue("email", "invalid.email");
    }

    if (personDao.usernameExists(registrationCommand.getEmail())) {
        errors.rejectValue("email", "inUse.email");
    }

    if (registrationCommand.getPassword().length() < ControllerUtils.MINIMUM_PASSWORD_LENGTH) {
        errors.rejectValue("password", "invalid.minimumPasswordLength6");
    }
}

From source file:cz.zcu.kiv.eegdatabase.logic.controller.scenario.AddScenarioSchemaValidator.java

public void validate(Object command, Errors errors) {
    log.debug("Validating scenario schema form");
    AddScenarioSchemaCommand data = (AddScenarioSchemaCommand) command;
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "schemaDescription", "required.field");

    int len = data.getSchemaDescription().length();
    if (len > 2000) {
        errors.rejectValue("schemaDescription", "invalid.maxScenSchemaLen");
    }/*from   w  ww .  j  ava 2 s .  c o  m*/

    if ((!(data.getId() > 0)) && (data.getSchemaFile().isEmpty()) && (data.getSchemaFile().isEmpty())) {
        // Creating new scenario and no file was uploaded
        errors.rejectValue("schemaFile", "required.dataFileXsd");
        log.debug("No data file was inserted!");
    }
}

From source file:cz.zcu.kiv.eegdatabase.logic.controller.scenario.AddScenarioValidator.java

public void validate(Object command, Errors errors) {
    log.debug("Validating scenario form");
    AddScenarioCommand data = (AddScenarioCommand) command;
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "title", "required.field");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "length", "required.field");
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "description", "required.field");

    if (data.getResearchGroup() == -1) {
        // research group not chosen
        errors.rejectValue("researchGroup", "required.researchGroup");
    }//  w ww. j ava2s  .  com
    // else if (!auth.personAbleToWriteIntoGroup(data.getResearchGroup())) {
    //            errors.rejectValue("researchGroup", "invalid.notAbleToAddExperimentInGroup");
    //        }

    try {
        int len = Integer.parseInt(data.getLength());
        if (len <= 0) {
            errors.rejectValue("length", "invalid.lengthValue");
        }
    } catch (NumberFormatException ex) {
        errors.rejectValue("length", "invalid.scenarioLength");
        log.debug("Scenario length is not in parseable format!");
    }
    int len = data.getDescription().length();
    if (len > 255) {
        errors.rejectValue("description", "invalid.maxScenLen");
    }

    if (!scenarioDao.canSaveTitle(data.getTitle(), data.getId())) {
        errors.rejectValue("title", "error.valueAlreadyInDatabase");
    }

    if ((!(data.getId() > 0)) && (data.isDataFileAvailable()) && ((!data.isXmlFileCheckBox()))) {
        if (data.getDataFile().isEmpty()) {
            // Creating new scenario and no file was uploaded
            errors.rejectValue("dataFile", "required.dataFile");
            log.debug("No data file was inserted!");
        }
    }

    if ((!(data.getId() > 0)) && (data.isDataFileAvailable()) && (data.isXmlFileCheckBox())) {
        if (data.getDataFileXml().isEmpty()) {
            // Creating new scenario and no file was uploaded
            errors.rejectValue("dataFileXml", "required.dataFileXml");
            log.debug("No XML data file was inserted!");
        }
    }

    if (data.isXmlFileCheckBox()) {
        if (data.getScenarioSchema() == 0 && (data.getScenarioOption().equals("fromList"))) {
            //scenario schema name is not chosen
            errors.rejectValue("scenarioSchema", "required.scenarioSchema");
            log.debug("No scenario schema was selected!");
        }
    }
}