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

Java tutorial

Introduction

Here is the source code for com.pedra.storefront.forms.validation.RegistrationValidator.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.RegisterForm;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

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

/**
 * Validates registration forms.
 */
@Component("registrationValidator")
public class RegistrationValidator implements Validator {
    public static final String EMAIL_REGEX = "\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}\\b";

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

    @Override
    public void validate(final Object object, final Errors errors) {
        final RegisterForm registerForm = (RegisterForm) object;
        final String titleCode = registerForm.getTitleCode();
        final String firstName = registerForm.getFirstName();
        final String lastName = registerForm.getLastName();
        final String email = registerForm.getEmail();
        final String pwd = registerForm.getPwd();
        final String checkPwd = registerForm.getCheckPwd();

        if (StringUtils.isEmpty(titleCode)) {
            errors.rejectValue("titleCode", "register.title.invalid");
        } else if (StringUtils.length(titleCode) > 255) {
            errors.rejectValue("titleCode", "register.title.invalid");
        }

        if (StringUtils.isEmpty(firstName)) {
            errors.rejectValue("firstName", "register.firstName.invalid");
        } else if (StringUtils.length(firstName) > 255) {
            errors.rejectValue("firstName", "register.firstName.invalid");
        }

        if (StringUtils.isEmpty(lastName)) {
            errors.rejectValue("lastName", "register.lastName.invalid");
        } else if (StringUtils.length(lastName) > 255) {
            errors.rejectValue("lastName", "register.lastName.invalid");
        }

        if (StringUtils.length(firstName) + StringUtils.length(lastName) > 255) {
            errors.rejectValue("lastName", "register.name.invalid");
            errors.rejectValue("firstName", "register.name.invalid");
        }

        if (StringUtils.isEmpty(email)) {
            errors.rejectValue("email", "register.email.invalid");
        } else if (StringUtils.length(email) > 255 || !validateEmailAddress(email)) {
            errors.rejectValue("email", "register.email.invalid");
        }

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

        if (StringUtils.isNotEmpty(pwd) && StringUtils.isNotEmpty(checkPwd) && !StringUtils.equals(pwd, checkPwd)) {
            errors.rejectValue("checkPwd", "validation.checkPwd.equals");
        } else {
            if (StringUtils.isEmpty(checkPwd)) {
                errors.rejectValue("checkPwd", "register.checkPwd.invalid");
            }
        }
    }

    public boolean validateEmailAddress(final String email) {
        final Pattern pattern = Pattern.compile(EMAIL_REGEX);
        final Matcher matcher = pattern.matcher(email);
        return matcher.matches();
    }
}