On Thu, 17 Jun 2010 23:51:40 -0300, Geoff Callender <geoff.callender.jumpst...@gmail.com> wrote:

Thanks, Thiago, but it lacks AJAX. I'd like the validator to behave like any other client-side validator except that it asks the server to help, eg. to validate that a name has not already been used.

Yep, I just noticed you wanted an AJAX example after I hit "send". :P


Geoff

On 18/06/2010, at 12:24 PM, Thiago H. de Paula Figueiredo wrote:

On Thu, 17 Jun 2010 22:20:45 -0300, Geoff Callender <geoff.callender.jumpst...@gmail.com> wrote:

Hi all,

Hi!

This is an example of a RGB string validator that I created for my Tapestry courses (basic and advanced):

public class RGBValidator extends AbstractValidator<Void, String> {

        final private static Pattern PATTERN =
                Pattern.compile("[0-9A-Fa-f]{6}");
        
        final private RenderSupport renderSupport;
        
        final private Asset javascript;

        public RGBValidator(
                        AssetSource assetSource, RenderSupport renderSupport) {
                
                super(null, String.class, "rgb-validation");
                this.renderSupport = renderSupport;
                javascript = assetSource.getClasspathAsset(
                                
"br/com/arsmachina/cursotapestry/validacao/rgb.js");
                
        }

        public void render(Field field, Void constraintValue,
                        MessageFormatter formatter, MarkupWriter writer,
                        FormSupport formSupport) {

                String message = buildMessage(field, formatter);
                
                // adds JavaScript validation
                formSupport.addValidation(field, "rgb", message, null);
                
                // includes rgb.js in the page
                renderSupport.addScriptLink(javascript);
                
                // adds a hint to the user. just an example of generating
                // HTML inside a validator.
                writer.element("span", "class", "hint");
                writer.write("Must be filled with 6 digits or letters from A to 
F.");
                writer.end();

        }

        @Override
        public void validate(Field field, Void constraintValue,
                        MessageFormatter formatter, String value)
                        throws ValidationException {

                if (PATTERN.matcher(value).matches() == false) {
                        String message = buildMessage(field, formatter);
                        throw new ValidationException(mensagem);
                }

        }

        private String buildMesssage(Field field, MessageFormatter formatter) {
                return formatter.format(field.getLabel());
        }

}

rgb.js:

Tapestry.Validator.rgb = function(field, message) {
        var regexp = /[0-9A-F]{6}/i;
        field.addValidator(function(value) {
                if (regexp.test(value) == false && value.length > 0) {
                        throw message;
                }
        });
};

--
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer, and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

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



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



--
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer, and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
Consultor, desenvolvedor e instrutor em Java, Tapestry e Hibernate
Coordenador e professor da Especialização em Engenharia de Software com Ênfase em Java da Faculdade Pitágoras
http://www.arsmachina.com.br

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

Reply via email to