com.pedra.storefront.forms.validation.PasswordValidator.java Source code

Java tutorial

Introduction

Here is the source code for com.pedra.storefront.forms.validation.PasswordValidator.java

Source

/*
 * [y] hybris Platform
 *
 * Copyright (c) 2000-2013 hybris AG
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of hybris
 * ("Confidential Information"). You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the
 * license agreement you entered into with hybris.
 * 
 *  
 */
package com.pedra.storefront.forms.validation;

import com.pedra.storefront.forms.UpdatePasswordForm;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

/**
 * Validator for password forms.
 */
@Component("passwordValidator")
public class PasswordValidator implements Validator {

    @Override
    public boolean supports(final Class<?> aClass) {
        return UpdatePasswordForm.class.equals(aClass);
    }

    @Override
    public void validate(final Object object, final Errors errors) {
        final UpdatePasswordForm passwordForm = (UpdatePasswordForm) object;
        final String currPasswd = passwordForm.getCurrentPassword();
        final String newPasswd = passwordForm.getNewPassword();
        final String checkPasswd = passwordForm.getCheckNewPassword();

        if (StringUtils.isEmpty(currPasswd)) {
            errors.rejectValue("currentPassword", "profile.currentPassword.invalid");
        }

        if (StringUtils.isEmpty(newPasswd)) {
            errors.rejectValue("newPassword", "updatePwd.pwd.invalid");
        } else if (StringUtils.length(newPasswd) < 6 || StringUtils.length(newPasswd) > 255) {
            errors.rejectValue("newPassword", "updatePwd.pwd.invalid");
        }

        if (StringUtils.isEmpty(checkPasswd)) {
            errors.rejectValue("checkNewPassword", "updatePwd.pwd.invalid");
        } else if (StringUtils.length(checkPasswd) < 6 || StringUtils.length(checkPasswd) > 255) {
            errors.rejectValue("checkNewPassword", "updatePwd.pwd.invalid");
        }
    }

}