You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by msalman <mo...@yahoo.com> on 2014/10/01 19:04:09 UTC

Re: FormValidators and messages property file

Hi Martin,

Thanks for your help.  That worked but I had to do a few extra things such
as setting the values for '${label0}', etc.  What I don't get is why do we
need to do this extra thing for Form validation classes (extending
AbstractFormValidator) but not for component validation classes
(implementing IValidator).



For others who may face the same problem, this is how got it working:


	@Override
	public void validate(Form<?> form) 
	{
		Date fromDate = fromDateFormComponent.getConvertedInput();
		Date toDate = toDateFormComponent.getConvertedInput();

		if ( 	fromDate != null
			 && toDate != null
			 && fromDate.after(toDate))
		{                                 
			ClassStringResourceLoader loader = new
ClassStringResourceLoader(FromDateBeforeToDate.class);
		
Application.get().getResourceSettings().getStringResourceLoaders().add(loader);
			
			ValidationError error = new ValidationError();
			error.addMessageKey(getClass().getSimpleName() + "." +
"fromDateAfterToDate");
			error.setVariable("label0",
fromDateFormComponent.getLabel().getObject());
			error.setVariable("label1", toDateFormComponent.getLabel().getObject());
			
			fromDateFormComponent.newValidatable().error(error);			
		}		
	}



 FromDateBeforeToDate.properties 

FromDateBeforeToDate.fromDateAfterToDate=Invalid input: The '${label}' date
is after the '${label}' date


I thank you again for teh quick and helpful response.



--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/FormValidators-and-messages-property-file-tp4667767p4667783.html
Sent from the Users forum mailing list archive at Nabble.com.

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


Re: FormValidators and messages property file

Posted by Martin Grigorov <mg...@apache.org>.
Hi,

As I said the simplest is to just use form.error("the.key"):

@Override
        public void validate(Form<?> form)
        {
                Date fromDate = fromDateFormComponent.getConvertedInput();
                Date toDate = toDateFormComponent.getConvertedInput();

                if (    fromDate != null
                         && toDate != null
                         && fromDate.after(toDate))
                {
                        form.error(getClass().getSimpleName() + "." +
"fromDateAfterToDate");
                }
        }


Martin Grigorov
Wicket Training and Consulting
https://twitter.com/mtgrigorov

On Thu, Oct 2, 2014 at 12:30 AM, msalman <mo...@yahoo.com> wrote:

> Well, I wanted these to be independent validation classes.  But now it
> seems
> like a bad idea.  I will make them part of the form.
>
> Martin, your comments and help are much appreciated.
>
> Thanks.
>
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/FormValidators-and-messages-property-file-tp4667767p4667790.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

Re: FormValidators and messages property file

Posted by msalman <mo...@yahoo.com>.
Well, I wanted these to be independent validation classes.  But now it seems
like a bad idea.  I will make them part of the form.

Martin, your comments and help are much appreciated.

Thanks.

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/FormValidators-and-messages-property-file-tp4667767p4667790.html
Sent from the Users forum mailing list archive at Nabble.com.

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


Re: FormValidators and messages property file

Posted by Martin Grigorov <mg...@apache.org>.
Hi,

I see what happens.
form.getString() would work because of
https://github.com/apache/wicket/blob/master/wicket-core/src/main/java/org/apache/wicket/resource/loader/ValidatorStringResourceLoader.java#L100
But using ValidationError has no reference to the form, and thus its form
validators, and it fails.

Martin Grigorov
Wicket Training and Consulting
https://twitter.com/mtgrigorov

On Wed, Oct 1, 2014 at 7:04 PM, msalman <mo...@yahoo.com> wrote:

> Hi Martin,
>
> Thanks for your help.  That worked but I had to do a few extra things such
> as setting the values for '${label0}', etc.  What I don't get is why do we
> need to do this extra thing for Form validation classes (extending
> AbstractFormValidator) but not for component validation classes
> (implementing IValidator).
>
>
>
> For others who may face the same problem, this is how got it working:
>
>
>         @Override
>         public void validate(Form<?> form)
>         {
>                 Date fromDate = fromDateFormComponent.getConvertedInput();
>                 Date toDate = toDateFormComponent.getConvertedInput();
>
>                 if (    fromDate != null
>                          && toDate != null
>                          && fromDate.after(toDate))
>                 {
>                         ClassStringResourceLoader loader = new
> ClassStringResourceLoader(FromDateBeforeToDate.class);
>
>
> Application.get().getResourceSettings().getStringResourceLoaders().add(loader);
>

I hope this is not your real code for production.
This will add the loader to the list of loaders on every validation.


>
>                         ValidationError error = new ValidationError();
>                         error.addMessageKey(getClass().getSimpleName() +
> "." +
> "fromDateAfterToDate");
>                         error.setVariable("label0",
> fromDateFormComponent.getLabel().getObject());
>                         error.setVariable("label1",
> toDateFormComponent.getLabel().getObject());
>
>
> fromDateFormComponent.newValidatable().error(error);
>                 }
>         }
>
>
>
>  FromDateBeforeToDate.properties
>
> FromDateBeforeToDate.fromDateAfterToDate=Invalid input: The '${label}' date
> is after the '${label}' date
>
>
> I thank you again for teh quick and helpful response.
>
>
>
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/FormValidators-and-messages-property-file-tp4667767p4667783.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>