In implementing an e-mail validator myself, one thing I notice in all
this is a Javascript error that reads

Error: Tapestry.Field.email is not a function...

I did some digging and found in org/apache/tapestry/tapestry.js the
building up of the Tapestry object has in it a section involving
"Collection of field based functions related to validation."  In that
part of the object prototyping (I guess), each of the built-in
validation types (required, minlength, maxlength, min, and max) has a
function assigned that, essentially, duplicates the functionality of
the Java-based validators.  All that prototyping must be for the
client-side functionality.

So I'm wondering why my AppModule-provided EmailValidator class'
render() method isn't contributing the script.  Its code is

pageRenderSupport.addScript(
               "Tapestry.Field.email('%s', %s);",
               field.getClientId(),
               quote(buildMessage(formatter, field)));

Since I implementing something for render(), I would think that at
least some kind of function for Tapestry.Field.email would show up
(even if it has the wrong number of fields, etc), knocking out the
"...is not a function" error.

Bill

On 5/16/07, Juan Maya <[EMAIL PROTECTED]> wrote:
Hi Marcus. Here are the classes:

Email.java
package com.dodo.community.web.validators;

import static org.apache.tapestry.TapestryUtils.quote;

import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.apache.tapestry.Field;
import org.apache.tapestry.MarkupWriter;
import org.apache.tapestry.PageRenderSupport;
import org.apache.tapestry.ValidationException;
import org.apache.tapestry.Validator;
import org.apache.tapestry.ioc.MessageFormatter;

/**
* A validator that enforces that the value is a valid email. This validator
is not configurable.
*/
public class Email implements Validator<Void, Object> {
    public static final String NAME = "email";
    private static final String MESSAGE_KEY = "invalid";
    private static final Pattern EMAIL_PATTERN = Pattern.compile
("[EMAIL PROTECTED]");

    /* (non-Javadoc)
     * @see org.apache.tapestry.Validator#getConstraintType()
     */
    public Class getConstraintType() {
    return String.class;
    }

    /* (non-Javadoc)
     * @see org.apache.tapestry.Validator#getMessageKey()
     */
    public String getMessageKey() {
    return MESSAGE_KEY;
    }

    /* (non-Javadoc)
     * @see org.apache.tapestry.Validator#getValueType()
     */
    public Class getValueType() {
    return String.class;
    }

    /* (non-Javadoc)
     * @see org.apache.tapestry.Validator#invokeIfBlank()
     */
    public boolean invokeIfBlank() {
    return true;
    }



    /* (non-Javadoc)
     * @see org.apache.tapestry.Validator#render(org.apache.tapestry.Field,
java.lang.Object, org.apache.tapestry.ioc.MessageFormatter,
org.apache.tapestry.MarkupWriter, org.apache.tapestry.PageRenderSupport)
     */
    public void render(Field field, Void constraintValue, MessageFormatter
formatter, MarkupWriter writer, PageRenderSupport pageRenderSupport) {
            //TODO: add JS support
    }

    /* (non-Javadoc)
     * @see org.apache.tapestry.Validator#validate(org.apache.tapestry.Field,
java.lang.Object, org.apache.tapestry.ioc.MessageFormatter, java.lang.Object
)
     */
    public void validate(Field field, Void constraintValue, MessageFormatter
formatter, Object value) throws ValidationException {
    System.out.println("validate email");
    if (value == null || StringUtils.isBlank(value.toString()))
        throw new ValidationException(buildMessage(formatter, field));

    if (!EMAIL_PATTERN.matcher(value.toString()).matches())
        throw new ValidationException(buildMessage(formatter, field));
    }

    /**
     * Builds a localized message
     * @param formatter
     * @param field
     * @return
     * @author juanm
     */
    private String buildMessage(MessageFormatter formatter, Field field)
    {
        return formatter.format(field.getLabel());
    }

}

add in AppModule.java
public static void
contributeFieldValidatorSource(MappedConfiguration<String, Validator>
configuration){
        configuration.add(Email.NAME, new Email());
}

The validators work for me perfectly when i use them in a page but when i
use them in a Component the server side validation only works in the second
form. I would really appreciate if u can test to use the validators inside a
component.


On 5/15/07, Marcus <[EMAIL PROTECTED]> wrote:
>
> Juan,
>
> I'm trying, but having a lot of compilation errors.
> If you post all code added to AppModule.java and Email class, maybe i can
> help you.
>
> Marcus
>



--
"The future is here.  It's just not evenly distributed yet."

    -- Traditional

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to