You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Tauren Mills <ta...@tauren.com> on 2009/04/21 12:29:40 UTC

How to determine which validator failed in a CompoundValidator?

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: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: How to determine which validator failed in a CompoundValidator?

Posted by Daan van Etten <da...@stuq.nl>.
Hi,

I think you have to be sure that you do not send an unencrypted  
password across the wire...
That said, I think you can post back the value to the server and  
update the PasswordStrengthMeter with that.
Or you could use Javascript to make it all client-side.

It all depends on implementation if you could reuse classes from my  
blog post.
I think you can use standard Ajax behaviors to update the  
PasswordStrengthMeter. After that, you could use standard form  
validation to validate the strength of the password. With my code, you  
can display an error next to the password field and highlight it.

- Daan

Op 22 apr 2009, om 17:52 heeft Tauren Mills het volgende geschreven:

> Daan,
>
> Yes, this definitely looks like the direction I should go for most of
> my form components.  Thanks for sharing it!
>
> However, I also need to make a PasswordStrengthMeter, which will show
> different values (and css styling) based on the strength of a password
> entered into a password field.  Something like one of these:
> http://ui-patterns.com/pattern/PasswordStrengthMeter
> http://ui-patterns.com/userset/39/image/1104#focus
>
> Before I embark on building it, do you have any suggestions on easy
> ways to use your solution to do this?
>
> Thanks!
> Tauren
>
>
>
> On Tue, Apr 21, 2009 at 10:18 PM, nino martinez wael
> <ni...@gmail.com> wrote:
>> Yeah looks like the way Tauren should go.. Did'nt know that was what
>> he was looking fore..
>>
>> 2009/4/22 Daan van Etten <da...@stuq.nl>:
>>> Hi Tauren,
>>>
>>> A while ago I wrote this article, which may give you some hints on  
>>> how to
>>> achieve this:
>>> http://stuq.nl/weblog/2008-09-03/user-friendly-form-validation-with-wicket
>>>
>>> Regards,
>>>
>>> Daan
>>>
>>> Op 21 apr 2009, om 17:36 heeft Tauren Mills het volgende geschreven:
>>>
>>>> Thanks Nino,
>>>>
>>>> Actually, I want just one error message right next to the  
>>>> username field
>>>> that is specific to the username. I also want to highlight via  
>>>> css the
>>>> username field. A messagepanel would list errors in other fields  
>>>> as well,
>>>> wouldn't it?
>>>>
>>>> Tauren
>>>>
>>>> On Apr 21, 2009 5:06 AM, "nino martinez wael"
>>>> <ni...@gmail.com>
>>>> wrote:
>>>>
>>>> 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 <ta...@tauren.com>:
>>>>
>>>>> On a site registration form, I have three validators on the  
>>>>> username >
>>>>
>>>> field.  One tests to make s...
>>>>>
>>>>> ---------------------------------------------------------------------
>>>>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>>>>> For additional commands, e-mail: users-help@wicket.apache.org
>>>>>
>>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>>>> For additional commands, e-mail: users-help@wicket.apache.org
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>>> For additional commands, e-mail: users-help@wicket.apache.org
>>>
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: How to determine which validator failed in a CompoundValidator?

Posted by Tauren Mills <ta...@tauren.com>.
Daan,

Yes, this definitely looks like the direction I should go for most of
my form components.  Thanks for sharing it!

However, I also need to make a PasswordStrengthMeter, which will show
different values (and css styling) based on the strength of a password
entered into a password field.  Something like one of these:
http://ui-patterns.com/pattern/PasswordStrengthMeter
http://ui-patterns.com/userset/39/image/1104#focus

Before I embark on building it, do you have any suggestions on easy
ways to use your solution to do this?

Thanks!
Tauren



On Tue, Apr 21, 2009 at 10:18 PM, nino martinez wael
<ni...@gmail.com> wrote:
> Yeah looks like the way Tauren should go.. Did'nt know that was what
> he was looking fore..
>
> 2009/4/22 Daan van Etten <da...@stuq.nl>:
>> Hi Tauren,
>>
>> A while ago I wrote this article, which may give you some hints on how to
>> achieve this:
>> http://stuq.nl/weblog/2008-09-03/user-friendly-form-validation-with-wicket
>>
>> Regards,
>>
>> Daan
>>
>> Op 21 apr 2009, om 17:36 heeft Tauren Mills het volgende geschreven:
>>
>>> Thanks Nino,
>>>
>>> Actually, I want just one error message right next to the username field
>>> that is specific to the username. I also want to highlight via css the
>>> username field. A messagepanel would list errors in other fields as well,
>>> wouldn't it?
>>>
>>> Tauren
>>>
>>> On Apr 21, 2009 5:06 AM, "nino martinez wael"
>>> <ni...@gmail.com>
>>> wrote:
>>>
>>> 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 <ta...@tauren.com>:
>>>
>>>> On a site registration form, I have three validators on the username >
>>>
>>> field.  One tests to make s...
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>>>> For additional commands, e-mail: users-help@wicket.apache.org
>>>>
>>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: How to determine which validator failed in a CompoundValidator?

Posted by nino martinez wael <ni...@gmail.com>.
Yeah looks like the way Tauren should go.. Did'nt know that was what
he was looking fore..

2009/4/22 Daan van Etten <da...@stuq.nl>:
> Hi Tauren,
>
> A while ago I wrote this article, which may give you some hints on how to
> achieve this:
> http://stuq.nl/weblog/2008-09-03/user-friendly-form-validation-with-wicket
>
> Regards,
>
> Daan
>
> Op 21 apr 2009, om 17:36 heeft Tauren Mills het volgende geschreven:
>
>> Thanks Nino,
>>
>> Actually, I want just one error message right next to the username field
>> that is specific to the username. I also want to highlight via css the
>> username field. A messagepanel would list errors in other fields as well,
>> wouldn't it?
>>
>> Tauren
>>
>> On Apr 21, 2009 5:06 AM, "nino martinez wael"
>> <ni...@gmail.com>
>> wrote:
>>
>> 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 <ta...@tauren.com>:
>>
>>> On a site registration form, I have three validators on the username >
>>
>> field.  One tests to make s...
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>>> For additional commands, e-mail: users-help@wicket.apache.org
>>>
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: How to determine which validator failed in a CompoundValidator?

Posted by Daan van Etten <da...@stuq.nl>.
Hi Tauren,

A while ago I wrote this article, which may give you some hints on how  
to achieve this:
http://stuq.nl/weblog/2008-09-03/user-friendly-form-validation-with-wicket

Regards,

Daan

Op 21 apr 2009, om 17:36 heeft Tauren Mills het volgende geschreven:

> Thanks Nino,
>
> Actually, I want just one error message right next to the username  
> field
> that is specific to the username. I also want to highlight via css the
> username field. A messagepanel would list errors in other fields as  
> well,
> wouldn't it?
>
> Tauren
>
> On Apr 21, 2009 5:06 AM, "nino martinez wael" <nino.martinez.wael@gmail.com 
> >
> wrote:
>
> 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 <ta...@tauren.com>:
>
>> On a site registration form, I have three validators on the  
>> username >
> field.  One tests to make s...
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: How to determine which validator failed in a CompoundValidator?

Posted by Tauren Mills <ta...@tauren.com>.
Thanks Nino,

Actually, I want just one error message right next to the username field
that is specific to the username. I also want to highlight via css the
username field. A messagepanel would list errors in other fields as well,
wouldn't it?

Tauren

On Apr 21, 2009 5:06 AM, "nino martinez wael" <ni...@gmail.com>
wrote:

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 <ta...@tauren.com>:

> On a site registration form, I have three validators on the username >
field.  One tests to make s...
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org

Re: How to determine which validator failed in a CompoundValidator?

Posted by nino martinez wael <ni...@gmail.com>.
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 <ta...@tauren.com>:
> 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: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org