On a site registration form, I have three validators on the username
field.  One tests to make sure the username is at least 3 characters
long, another one checks to make sure it uses valid characters, and
the last one checks if the username is already taken.  This all works
fine.

Now I'm working on adding an ajax behavior to the field so that as the
user types, validation happens in real time.  There is a label called
"valid" that should change to contain on of the following messages, as
appropriate based on the success/failure of the validation:

You have entered a username that is too short.
You have entered a username with invalid characters.
You have entered a username that is already taken.
Your username is available!

The problem is, I don't know how to determine which of the validators
in the CompoundValidator have failed.  Is this possible to determine?
How should my AjaxUsernameBehavior know which message to display?

Here is some preliminary code:

            RequiredTextField userName = new
RequiredTextField("username",new PropertyModel(user,"username"));

            CompoundValidator validators = new CompoundValidator();
            validators.add(StringValidator.minimumLength(3));
            validators.add(new
PatternValidator("[_A-Za-z0-9-]*",Pattern.CASE_INSENSITIVE));
            validators.add(new UsernameValidator());
            userName.add(validators);

            AjaxFormComponentUpdatingBehavior usernameBehavior = new
AjaxUsernameBehavior("onkeyup", validators, valid);
            usernameBehavior.setThrottleDelay(Duration.ONE_SECOND);
            userName.add(usernameBehavior);
            add(userName);

            Label valid = new Label("valid","");
            valid.setOutputMarkupId(true);
            add(valid);

I haven't implemented AjaxUsernameBehavior yet, thus no code.  But I'm
thinking I'll need to provide it with the validators and the component
to update ("valid").

Thanks in advance for any ideas!
Tauren

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to