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.telscenter.sail.webapp.presentation.validators.student.TeamSignInFormValidator.java

/**
 * @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors)
 *//*from w  ww.j  av  a  2  s .c  o  m*/
public void validate(Object teamSignInFormIn, Errors errors) {
    TeamSignInForm teamSignInForm = (TeamSignInForm) teamSignInFormIn;
    PasswordEncoder encoder = new Md5PasswordEncoder();

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "username1", "error.teamsignin-username-not-specified");

    if (errors.hasErrors()) {
        return;
    }

    User user1 = userService.retrieveUserByUsername(teamSignInForm.getUsername1());
    if (user1 == null) {
        errors.rejectValue("username1", "error.teamsignin-user-does-not-exist");
        return;
    }

    // handle user2 and user3 separately; be able to handle when user3 is filled in
    // but not user2
    if (!StringUtils.isEmpty(teamSignInForm.getUsername2())) {
        if (StringUtils.isEmpty(teamSignInForm.getPassword2())) {
            //the password field is empty
            errors.rejectValue("password2", "error.teamsignin-password-not-specified");
        } else {
            User user2 = userService.retrieveUserByUsername(teamSignInForm.getUsername2());
            if (user2 == null) {
                //the username does not exist
                errors.rejectValue("username2", "error.teamsignin-user-does-not-exist");
            } else {
                //get the user and the hashed password
                MutableUserDetails userDetails2 = user2.getUserDetails();
                String hashedPassword2 = encoder.encodePassword(teamSignInForm.getPassword2(),
                        systemSaltSource.getSystemWideSalt());

                if (userDetails2 instanceof TeacherUserDetails) {
                    //username is for a teacher and that is not allowed
                    errors.rejectValue("username2", "error.teamsignin-teacher-username-specified");
                } else if (!user2.getUserDetails().getPassword().equals(hashedPassword2)) {
                    //password is incorrect
                    errors.rejectValue("password2", "error.teamsignin-incorrect-password");
                }
            }
        }
    }

    //handle user3
    if (!StringUtils.isEmpty(teamSignInForm.getUsername3())) {
        if (StringUtils.isEmpty(teamSignInForm.getPassword3())) {
            //the password field is empty
            errors.rejectValue("password3", "error.teamsignin-password-not-specified");
        } else {
            User user3 = userService.retrieveUserByUsername(teamSignInForm.getUsername3());
            if (user3 == null) {
                //the username does not exist
                errors.rejectValue("username3", "error.teamsignin-user-does-not-exist");
            } else {
                //get the user and the hashed password
                MutableUserDetails userDetails3 = user3.getUserDetails();
                String hashedPassword3 = encoder.encodePassword(teamSignInForm.getPassword3(),
                        systemSaltSource.getSystemWideSalt());

                if (userDetails3 instanceof TeacherUserDetails) {
                    //username is for a teacher and that is not allowed
                    errors.rejectValue("username3", "error.teamsignin-teacher-username-specified");
                } else if (!user3.getUserDetails().getPassword().equals(hashedPassword3)) {
                    //password is incorrect
                    errors.rejectValue("password3", "error.teamsignin-incorrect-password");
                }
            }
        }
    }
}

From source file:org.telscenter.sail.webapp.presentation.validators.TeacherAccountFormValidator.java

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

    if (errors.hasErrors())
        return;/*from  ww w  .j  a v  a2 s . c om*/
    TeacherAccountForm teacherAccountForm = (TeacherAccountForm) userAccountFormIn;
    TeacherUserDetails userDetails = (TeacherUserDetails) teacherAccountForm.getUserDetails();

    if (!teacherAccountForm.isNewAccount()) {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userDetails.displayname",
                "error.displayname-not-specified");
    } else {
        if (!errors.hasErrors() && (userDetails.getPassword() == null || userDetails.getPassword().length() < 1
                || !userDetails.getPassword().equals(teacherAccountForm.getRepeatedPassword()))) {
            errors.reject("error.passwords-mismatch",
                    "Passwords did not match or were not provided. Matching passwords are required.");
        }
    }

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userDetails.emailAddress", "error.email-not-specified");

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userDetails.country", "error.country-not-specified");

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userDetails.schoolname",
            "error.schoolname-not-specified");

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userDetails.curriculumsubjects",
            "error.curriculumsubjects-not-specified");

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userDetails.schoollevel",
            "error.schoollevel-not-specified");

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userDetails.city", "error.city-not-specified");

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userDetails.state", "error.state-not-specified");

    if (!teacherAccountForm.isLegalAcknowledged()) {
        errors.reject("error.legal-not-acknowledged", "You must agree to the terms of use");
    }

    // TODO HT: CHECK FOR ILLEGAL EMAIL ADDRESS FORMAT
    String email = userDetails.getEmailAddress();

    //validate email if it is not null and not empty
    if (email != null && !email.trim().equals("")) {
        validateEmail(email, errors);
    }

    if (errors.hasErrors())
        userDetails.setPassword("");
}

From source file:org.telscenter.sail.webapp.presentation.validators.UserAccountFormValidator.java

/**
 * @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors)
 *//*from   w  w  w.  j  a  va 2  s  . co  m*/
public void validate(Object userAccountFormIn, Errors errors) {
    UserAccountForm userAccountForm = (UserAccountForm) userAccountFormIn;
    MutableUserDetails userDetails = userAccountForm.getUserDetails();

    if (userAccountForm.isNewAccount()) {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userDetails.password",
                "error.password-not-specified");

        if (errors.getFieldErrorCount("userDetails.password") > 0) {
            return;
        }

        if (userDetails.getPassword().length() > MAX_PASSWORD_LENGTH) {
            errors.rejectValue("userDetails.password", "error.password-too-long");
            return;
        }

        if (!StringUtils.isAlphanumeric(userDetails.getPassword())) {
            errors.rejectValue("userDetails.password", "error.password-illegal-characters");
            return;
        }

        if (userDetails.getSignupdate() == null) {
            errors.rejectValue("userDetails.signupdate", "error.signupdate-not-specified");
            return;
        }
    } else {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userDetails.username",
                "error.username-not-specified");

        if (!StringUtils.isAlphanumeric(userDetails.getUsername())) {
            errors.rejectValue("userDetails.username", "error.illegal-characters");
        }
    }

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userDetails.firstname", "error.firstname-not-specified");

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userDetails.lastname", "error.lastname-not-specified");

    if (!StringUtils.isAlphanumeric(userDetails.getFirstname())
            || !StringUtils.isAsciiPrintable(userDetails.getFirstname())) {
        errors.rejectValue("userDetails.firstname", "error.firstname-illegal-characters");
        return;
    }

    if (!StringUtils.isAlphanumeric(userDetails.getLastname())
            || !StringUtils.isAsciiPrintable(userDetails.getLastname())) {
        errors.rejectValue("userDetails.lastname", "error.lastname-illegal-characters");
        return;
    }

    if (errors.hasErrors())
        userDetails.setPassword("");
}

From source file:org.telscenter.sail.webapp.presentation.validators.UserDetailsValidator.java

/**
 * @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors)
 *//* w w  w  .j a va2 s . co  m*/
@Override
public void validate(Object userDetailsIn, Errors errors) {
    MutableUserDetails userDetails = (MutableUserDetails) userDetailsIn;

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "error.password-not-specified");

    if (errors.getFieldErrorCount("password") > 0) {
        return;
    }

    if (userDetails.getPassword().length() > MAX_PASSWORD_LENGTH) {
        errors.rejectValue("password", "error.password-too-long");
        return;
    }

    if (!StringUtils.isAlphanumeric(userDetails.getPassword())) {
        errors.rejectValue("password", "error.password-illegal-characters");
        return;
    }

    if (userDetails.getSignupdate() == null) {
        errors.rejectValue("signupdate", "error.signupdate-not-specified");
        return;
    }

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstname", "error.firstname-not-specified");

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "lastname", "error.lastname-not-specified");

    if (!StringUtils.isAlphanumeric(userDetails.getFirstname())) {
        errors.rejectValue("firstname", "error.firstname-illegal-characters");
        return;
    }

    if (!StringUtils.isAlphanumeric(userDetails.getLastname())) {
        errors.rejectValue("lastname", "error.lastname-illegal-characters");
        return;
    }

    if (errors.hasErrors())
        userDetails.setPassword("");
}

From source file:org.telscenter.sail.webapp.presentation.web.controllers.forgotaccount.student.PasswordReminderWizardController.java

/**
 * This method is called after the onBind and onBindAndValidate method. It
 * acts in the same way as the validator
 * //from ww  w. ja  v a  2s  .c o  m
 * @see org.springframework.web.servlet.mvc.AbstractWizardFormController#validatePage(java.lang.Object,
 *      org.springframework.validation.Errors, int)
 */
@Override
protected void validatePage(Object command, Errors errors, int page) {

    ReminderParameters reminderParameters = (ReminderParameters) command;

    switch (page) {
    case 0:
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "username", "error.no-username");

        if (errors.getErrorCount() == 0) {
            try {
                String username = reminderParameters.getUsername();

                if (username == null) {
                    errors.rejectValue("username", "error.username-not-found");
                    break;
                } else {
                    username = StringUtils.trimToNull(username);
                    User user = userService.retrieveUserByUsername(username);

                    /* check to see if user exists and ensure that user is a student */
                    if (user == null || !(user.getUserDetails() instanceof StudentUserDetails)) {
                        errors.rejectValue("username", "error.username-not-found");
                    }
                }
            } catch (EmptyResultDataAccessException e) {
                //TODO: archana needs to update these
                errors.rejectValue("username", "error.username-not-found");
            }
        }

        break;
    case 1:
        //TODO: archana needs to update these
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "submittedAccountAnswer",
                "error.submitted-account-question-blank");

        String submittedAccountAnswer = reminderParameters.getSubmittedAccountAnswer();

        String accountAnswer = reminderParameters.getAccountAnswer();

        accountAnswer = StringUtils.lowerCase(accountAnswer);

        submittedAccountAnswer = StringUtils.lowerCase(submittedAccountAnswer);

        if (!accountAnswer.equals(submittedAccountAnswer)) {
            //TODO: archana needs to update these
            errors.reject("error.submitted-account-question");
        }

        break;
    case 2:

        //TODO: archana needs to update these
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "verifyPassword", "error.verify-newpassword");

        //TODO: archana needs to update these
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "newPassword", "error.verify-newpassword");

        if (errors.hasErrors()) {
            break;
        }

        String newPassword = reminderParameters.getNewPassword();

        String verifyPassword = reminderParameters.getVerifyPassword();

        verifyPassword = StringUtils.lowerCase(verifyPassword);

        newPassword = StringUtils.lowerCase(newPassword);

        verifyPassword = StringUtils.lowerCase(verifyPassword);

        if (!verifyPassword.equals(newPassword)) {
            //TODO: archana needs to update these
            errors.reject("error.verify-newpassword");
        }
        break;
    default:
        break;
    }
}

From source file:org.telscenter.sail.webapp.presentation.web.controllers.LostPasswordStudentReminderWizardController.java

/**
 * This method is called after the onBind and onBindAndValidate method. It
 * acts in the same way as the validator
 * /*from w w  w .  j  av  a2 s.c o  m*/
 * @see org.springframework.web.servlet.mvc.AbstractWizardFormController#validatePage(java.lang.Object,
 *      org.springframework.validation.Errors, int)
 */
@Override
protected void validatePage(Object command, Errors errors, int page) {

    ReminderParameters reminderParameters = (ReminderParameters) command;

    switch (page) {
    case 0:
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "username", "error.username-not-found");
        try {

            String username = reminderParameters.get(ReminderParameters.USERNAME);
            username = StringUtils.trimToNull(username);
            user = userService.retrieveUserByUsername(username);
        } catch (EmptyResultDataAccessException e) {
            //TODO: archana needs to update these
            errors.reject("username", "error.username-not-found");
        }

        break;
    case 1:
        //TODO: archana needs to update these
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "submittedAccountAnswer",
                "error.submitted-account-question-blank");

        String submittedAccountAnswer = reminderParameters.getSubmittedAccountAnswer();

        String accountAnswer = reminderParameters.getAccountAnswer();

        accountAnswer = StringUtils.lowerCase(accountAnswer);

        submittedAccountAnswer = StringUtils.lowerCase(submittedAccountAnswer);
        ;

        if (!accountAnswer.equals(submittedAccountAnswer)) {
            //TODO: archana needs to update these
            errors.reject("error.submitted-account-question");
        }

        break;
    case 2:

        //TODO: archana needs to update these
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "verifyPassword", "error.verify-newpassword");

        //TODO: archana needs to update these
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "newPassword", "error.verify-newpassword");

        String newPassword = reminderParameters.getNewPassword();

        String verifyPassword = reminderParameters.getVerifyPassword();

        verifyPassword = StringUtils.lowerCase(verifyPassword);

        newPassword = StringUtils.lowerCase(newPassword);

        verifyPassword = StringUtils.lowerCase(verifyPassword);

        if (!verifyPassword.equals(newPassword)) {
            //TODO: archana needs to update these
            errors.reject("error.verify-newpassword");
        }
        break;
    default:
        break;
    }
}

From source file:org.wise.portal.presentation.validators.ChangePasswordParametersValidator.java

public void validatePasswd1(Errors errors, ChangePasswordParameters params) {
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "passwd1",
            "presentation.validators.ChangePasswordParametersValidator.errorNewPasswordMissing");

    if (errors.getErrorCount() != 0)
        return;// w  w w .j  av a2 s.com
    this.validatePasswordAlphaNumeric(errors, params.getPasswd1());
}

From source file:org.wise.portal.presentation.validators.ChangePasswordParametersValidator.java

public void validatePasswd2(Errors errors, ChangePasswordParameters params) {
    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "passwd2",
            "presentation.validators.ChangePasswordParametersValidator.errorNewPasswordMissing");

    if (errors.getErrorCount() != 0)
        return;/*w ww  .j  av a2  s  . co  m*/

    this.validatePasswordAlphaNumeric(errors, params.getPasswd2());
}

From source file:org.wise.portal.presentation.validators.student.TeamSignInFormValidator.java

/**
 * @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors)
 *//*w  ww .jav a2 s .  c  o m*/
public void validate(Object teamSignInFormIn, Errors errors) {
    TeamSignInForm teamSignInForm = (TeamSignInForm) teamSignInFormIn;
    Md5PasswordEncoder encoder = new Md5PasswordEncoder();

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "username1", "error.teamsignin-username-not-specified");

    if (errors.hasErrors()) {
        return;
    }

    // get the signed in user
    User signedInUser = userService.retrieveUserByUsername(teamSignInForm.getUsername1());
    if (signedInUser == null) {
        errors.rejectValue("username1", "error.teamsignin-user-does-not-exist");
        return;
    }

    Run run = null;

    try {
        // get the run
        run = runService.retrieveById(teamSignInForm.getRunId());
    } catch (ObjectNotFoundException e) {
        e.printStackTrace();
    }

    // get the workgroups the signed in user is in for this run
    List<Workgroup> workgroups = workgroupService.getWorkgroupListByOfferingAndUser(run, signedInUser);

    Workgroup workgroup = null;

    // get the members in the workgroup
    Set<User> membersInWorkgroup = new HashSet<User>();

    if (workgroups != null && workgroups.size() > 0) {

        // get the workgroup the signed in user is in
        workgroup = workgroups.get(0);

        // get the members in the workgroup
        membersInWorkgroup = workgroups.get(0).getMembers();
    }

    // loop through all the users that are not signed in (2 through 10)
    for (int x = 1; x < 10; x++) {
        int userIndex = x + 1;
        String usernameField = "username" + userIndex;
        String passwordField = "password" + userIndex;
        String absentField = "absent" + userIndex;

        // get the username, password, and whether the user is absent
        String username = teamSignInForm.getUsernameByString(usernameField);
        String password = teamSignInForm.getPasswordByString(passwordField);
        boolean isAbsent = teamSignInForm.getIsAbsentByString(absentField);

        if (!StringUtils.isEmpty(username)) {
            /*
             * the username field is not empty so we need to check if this user
             * is allowed to join the workgroup
             */

            if (!isAbsent) {
                // the user is not marked as absent

                // get the user
                User user = userService.retrieveUserByUsername(username);

                if (user == null) {
                    //the username does not exist
                    errors.rejectValue(usernameField, "error.teamsignin-user-does-not-exist");
                } else {

                    //get the user details
                    MutableUserDetails userDetails = user.getUserDetails();

                    if (userDetails instanceof TeacherUserDetails) {
                        //username is for a teacher and that is not allowed
                        errors.rejectValue(usernameField, "error.teamsignin-teacher-username-specified");
                    } else {

                        /*
                         * flag to determine if we need to check the password. we do not need to
                         * check the password if the user is not allowed to join the workgroup because
                         * we will display an error message anyway.
                         */
                        boolean needToCheckPassword = true;

                        if (workgroup == null) {
                            // the workgroup has not been created

                            // check if the user is already in a workgroup
                            if (workgroupService.isUserInAnyWorkgroupForRun(user, run)) {
                                // the user is already in another workgroup in the run so we will display an error message
                                errors.rejectValue(passwordField,
                                        "error.teamsignin-user-already-in-another-workgroup");
                                needToCheckPassword = false;
                            }
                        } else {
                            // the workgroup has already been created

                            // check if the user is in the workgroup and not in another workgroup

                            if (workgroupService.isUserInWorkgroupForRun(user, run, workgroup)) {
                                // the user is in the workgroup
                            } else if (workgroupService.isUserInAnotherWorkgroupForRun(user, run, workgroup)) {
                                // the user is already in another workgroup in the run so we will display an error message
                                errors.rejectValue(passwordField,
                                        "error.teamsignin-user-already-in-another-workgroup");
                                needToCheckPassword = false;
                            } else {
                                // the user is not in a workgroup for the run
                            }
                        }

                        if (needToCheckPassword) {
                            // the user is allowed to join the workgroup so we will now check the password

                            // get the hashed password
                            String hashedPassword = encoder.encodePassword(password,
                                    systemSaltSource.getSystemWideSalt());

                            if (StringUtils.isEmpty(password)) {
                                //the password field is empty
                                errors.rejectValue(passwordField, "error.teamsignin-password-not-specified");
                            } else if (!user.getUserDetails().getPassword().equals(hashedPassword)) {
                                //password is incorrect
                                errors.rejectValue(passwordField, "error.teamsignin-incorrect-password");
                            }
                        }
                    }
                }
            }
        }
    }
}

From source file:org.wise.portal.presentation.validators.UserAccountFormValidator.java

/**
 * @see org.springframework.validation.Validator#validate(java.lang.Object, org.springframework.validation.Errors)
 *///  www  .  j  a va  2  s. c o  m
public void validate(Object userAccountFormIn, Errors errors) {
    UserAccountForm userAccountForm = (UserAccountForm) userAccountFormIn;
    MutableUserDetails userDetails = userAccountForm.getUserDetails();

    if (userAccountForm.isNewAccount()) {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userDetails.password",
                "error.password-not-specified");

        if (errors.getFieldErrorCount("userDetails.password") > 0) {
            return;
        }

        if (userDetails.getPassword().length() > MAX_PASSWORD_LENGTH) {
            errors.rejectValue("userDetails.password", "error.password-too-long");
            return;
        }

        if (!StringUtils.isAlphanumeric(userDetails.getPassword())) {
            errors.rejectValue("userDetails.password",
                    "presentation.validators.ChangePasswordParametersValidator.errorPasswordContainsIllegalCharacters");
            return;
        }

        if (userDetails.getSignupdate() == null) {
            errors.rejectValue("userDetails.signupdate", "error.signupdate-not-specified");
            return;
        }
    } else {
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userDetails.username",
                "error.username-not-specified");

        if (!StringUtils.isAlphanumeric(userDetails.getUsername())) {
            errors.rejectValue("userDetails.username", "error.username-illegal-characters");
        }
    }

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userDetails.firstname", "error.firstname-not-specified");

    ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userDetails.lastname", "error.lastname-not-specified");

    if (!StringUtils.isAlphanumeric(userDetails.getFirstname())
            || !StringUtils.isAsciiPrintable(userDetails.getFirstname())) {
        errors.rejectValue("userDetails.firstname", "error.firstname-illegal-characters");
        return;
    }

    if (!StringUtils.isAlphanumeric(userDetails.getLastname())
            || !StringUtils.isAsciiPrintable(userDetails.getLastname())) {
        errors.rejectValue("userDetails.lastname", "error.lastname-illegal-characters");
        return;
    }

    if (errors.hasErrors())
        userDetails.setPassword("");
}