Hmm, it happens automatically, a validator can register errors.. Like this:

        form.add(new TextField<String>("email",new
PropertyModel<String>(form.getModel(),"email")).add(
                                EmailAddressValidator.getInstance()).add(new 
IValidator() {
                        public void validate(IValidatable validatable) {
                                String string = (String) validatable.getValue();
                                if (userRepository.areEmailThere(string)) {
                                        validatable.error(new 
ValidationError().addMessageKey(
                                                        
"error.unique").setVariable("email",
                                                        
validatable.getValue()));
                                }

                        }
                }));

                form.add(new CheckBox("agree", new Model<Boolean>(false))
                                .add(new IValidator<Boolean>() {
                                        public void 
validate(IValidatable<Boolean> validatable) {
                                                Boolean agree = 
validatable.getValue();
                                                if (!agree) {
                                                        validatable.error(new 
ValidationError()
                                                                        
.addMessageKey("error.mustagree"));
                                                }

                                        }
                                }));


And then just in your ajax add the error message panel to the
response.. Works just fine... Or is it something more you want?

2009/4/21 Tauren Mills <[email protected]>:
> 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]
>
>

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

Reply via email to