I wrote one that appears to work.  I copied the contents of TegexFieldValidator 
to get it to work.  Is there a need for more of the validators to support 
collections?   



 
Norris Shelton
Software Engineer
Sun Certified Java 1.1 Programmer
Shelton Consulting, LLC
ICQ# 26487421
AIM NorrisEShelton
YIM norrisshelton




________________________________
From: Dave Newton <newton.d...@yahoo.com>
To: Struts Users Mailing List <user@struts.apache.org>
Sent: Friday, August 28, 2009 6:19:45 PM
Subject: Re: [U] Email Validation Issue

Ginn, Timothy D Mr CTR USA TRADOC USAAC wrote:
> I have run into a bump in the road.  I am trying to validate a list of
> Strings that contain email addresses.  I can't seem to get it to check
> the emails to be a valid email using the annotation validation. 
> Could someone please let me know if this is possible or if I am doing
> something wrong 
>    @Validations(
>        requiredStrings = {...@requiredstringvalidator(message = "At Least
> one email address is required.", trim = true)},
>        emails = {...@emailvalidator(message = "Please enter a valid email
> address.", type = ValidatorType.FIELD)}
>    )
>    public void setLdapAdmins(List<String> ldapAdmins) {
>        this.ldapAdmins = ldapAdmins;
>    }
> 
> ldapAdmins is a list of Strings that contain email addresses. I need to
> validate that they are truly email addresses and that the fields are not
> "".

AFAIK the validations will validate only a single object; I believe you'd need 
custom validation either via a validate() method, custom validator, etc.

Dave


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org


      
package mil.army.usaac.certs.struts_api;


import com.opensymphony.xwork2.validator.*;
import com.opensymphony.xwork2.validator.validators.*;

import java.util.*;
import java.util.regex.*;


/**
 /**
 * <!-- START SNIPPET: javadoc -->
 * EmailValidator checks that a given String field, if not empty,
 * is a valid email address.
 * <p/>
 * <p/>
 * The regular expression used to validate that the string is an email address
 * is:
 * </p>
 * <pre>
 * 
\\b(^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@([A-Za-z0-9-])+((\\.com)|(\\.net)|(\\.org)|(\\.info)|(\\.edu)|(\\.mil)|(\\.gov)|(\\.biz)|(\\.ws)|(\\.us)|(\\.tv)|(\\.cc)|(\\.aero)|(\\.arpa)|(\\.coop)|(\\.int)|(\\.jobs)|(\\.museum)|(\\.name)|(\\.pro)|(\\.travel)|(\\.nato)|(\\..{2,3})|(\\..{2,3}\\..{2,3}))$)\\b
 * </pre>
 * <!-- END SNIPPET: javadoc -->
 *
 *
 * <!-- START SNIPPET: parameters -->
 * <ul>
 *              <li>fieldName - The field name this validator is validating. 
Required if using Plain-Validator Syntax otherwise not required</li>
 * </ul>
 * <!-- END SNIPPET: parameters -->
 *
 *
 * <pre>
 * <!-- START SNIPPET: example -->
 *     &lt;!-- Plain Validator Syntax --&gt;
 *     &lt;validators&gt;
 *         &lt;validator type="emailCollection"&gt;
 *             &lt;param name="fieldName"&gt;myEmail&lt;/param&gt;
 *             &lt;message&gt;Must provide a valid email&lt;/message&gt;
 *         &lt;/validator&gt;
 *     &lt;/validators&gt;
 *
 *     &lt;!-- Field Validator Syntax --&gt;
 *     &lt;field name="myEmail"&gt;
 *        &lt;field-validator type="emailCollection"&gt;
 *           &lt;message&gt;Must provide a valid email&lt;/message&gt;
 *        &lt;/field-validator&gt;
 *     &lt;/field&gt;
 * <!-- END SNIPPET: example -->
 * </pre>
 *
 * @author Norris Shelton
 * @version $Date: $ $Id: $ */
public class EmailCollectionValidator extends EmailValidator {
    /**
     * The validation implementation must guarantee that setValidatorContext 
will be called with a non-null
     * ValidatorContext before validate is called.
     * @param object the object to be validated.
     * @throws ValidationException is thrown if there is validation error(s).
     */
    @Override
    public void validate(Object object) throws ValidationException {
        Object fieldValue = getFieldValue(getFieldName(), object);
        if (fieldValue instanceof Collection) {
            for (Object value: (Collection) fieldValue) {
                // if there is no value - don't do comparison
                // if a value is required, a required validator should be added 
to the field
                if (value == null || getExpression() == null) {
                    return;
                }

                // XW-375 - must be a string
                if (!(value instanceof String)) {
                    return;
                }

                // string must not be empty
                String str = ((String) value).trim();
                if (str.length() == 0) {
                    return;
                }

                // match against expression
                Pattern pattern;
                if (isCaseSensitive()) {
                    pattern = Pattern.compile(getExpression());
                } else {
                    pattern = Pattern.compile(getExpression(), 
Pattern.CASE_INSENSITIVE);
                }

                String compare = (String) value;
                if (isTrimed()) {
                    compare = compare.trim();
                }
                Matcher matcher = pattern.matcher(compare);

                if (!matcher.matches()) {
                    addFieldError(getFieldName(), object);
                }
            }
        }
    }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org

Reply via email to