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

Java tutorial

Introduction

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

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 entries on Guest user forms.
 */
@Component("guestValidator")
public class GuestValidator 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 GuestForm.class.equals(aClass);
    }

    @Override
    public void validate(final Object object, final Errors errors) {
        final GuestForm guestForm = (GuestForm) object;
        final String email = guestForm.getEmail();

        if (StringUtils.isEmpty(email)) {
            errors.rejectValue("email", "profile.email.invalid");
        } else if (StringUtils.length(email) > 255 || !Pattern.matches(EMAIL_REGEX, email)) {
            errors.rejectValue("email", "profile.email.invalid");
        }
    }
}