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/09/30 21:36:12 UTC

FormValidators and messages property file

Hi,

I have created form validator classes as shown below.  The problem is that
the code is not picking up messages from the corresponding
'FromDateBeforeToDate.properties' file in the same package. It works if the
messages are in the page.properties or application.properties file.   

I can see works OK for class implementing IValidator but can't seem to make
it work for class extending AbstractFormValidator.    Are my expectations
wrong?  If not then please tell me how I can fix the code.



public class FromDateBeforeToDate extends AbstractFormValidator
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 3503966266288025266L;
	
	
	DateField fromDateFormComponent;
	DateField toDateFormComponent;
	
	
	public FromDateBeforeToDate(
		DateField fromDateFormComponent,
		DateField toDateFormComponent)
	{
		if (fromDateFormComponent == null)
		{
			throw new IllegalArgumentException("Argument dateFromFormComponent cannot
be null");
		}
		this.fromDateFormComponent = fromDateFormComponent;
		

		if (toDateFormComponent == null)
		{
			throw new IllegalArgumentException("Argument dateToFormComponent cannot
be null");
		}
		this.toDateFormComponent = toDateFormComponent;		
	}
	

	@Override
	public FormComponent<?>[] getDependentFormComponents() 
	{
		return new FormComponent<?>[]
		{
			fromDateFormComponent,
			toDateFormComponent,
		};
	}
	
	

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

		if ( 	fromDate != null
			 && toDate != null
			 && fromDate.after(toDate))
		{                                 
			//error(fromDateFormComponent, "fromDateAfterToDate");
			
			ValidationError error = new ValidationError();
			error.addMessageKey(getClass().getSimpleName() + "." +
"fromDateAfterToDate");
			fromDateFormComponent.newValidatable().error(error);
			//this.error(fromDateFormComponent);
		}		
	}
}

FromDateBeforeToDate.properties

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



Thanks.

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/FormValidators-and-messages-property-file-tp4667767.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
>
>

Re: FormValidators and messages property file

Posted by msalman <mo...@yahoo.com>.
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,

According to
https://github.com/apache/wicket/blob/master/wicket-core/src/main/java/org/apache/wicket/resource/loader/ValidatorStringResourceLoader.java#L62
it should work !
There is even a test for this:
https://github.com/apache/wicket/tree/master/wicket-core/src/test/java/org/apache/wicket/resource/loader

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

On Tue, Sep 30, 2014 at 9:36 PM, msalman <mo...@yahoo.com> wrote:

> Hi,
>
> I have created form validator classes as shown below.  The problem is that
> the code is not picking up messages from the corresponding
> 'FromDateBeforeToDate.properties' file in the same package. It works if the
> messages are in the page.properties or application.properties file.
>
> I can see works OK for class implementing IValidator but can't seem to make
> it work for class extending AbstractFormValidator.    Are my expectations
> wrong?  If not then please tell me how I can fix the code.
>
>
>
> public class FromDateBeforeToDate extends AbstractFormValidator
> {
>
>         /**
>          *
>          */
>         private static final long serialVersionUID = 3503966266288025266L;
>
>
>         DateField fromDateFormComponent;
>         DateField toDateFormComponent;
>
>
>         public FromDateBeforeToDate(
>                 DateField fromDateFormComponent,
>                 DateField toDateFormComponent)
>         {
>                 if (fromDateFormComponent == null)
>                 {
>                         throw new IllegalArgumentException("Argument
> dateFromFormComponent cannot
> be null");
>                 }
>                 this.fromDateFormComponent = fromDateFormComponent;
>
>
>                 if (toDateFormComponent == null)
>                 {
>                         throw new IllegalArgumentException("Argument
> dateToFormComponent cannot
> be null");
>                 }
>                 this.toDateFormComponent = toDateFormComponent;
>         }
>
>
>         @Override
>         public FormComponent<?>[] getDependentFormComponents()
>         {
>                 return new FormComponent<?>[]
>                 {
>                         fromDateFormComponent,
>                         toDateFormComponent,
>                 };
>         }
>
>
>
>         @Override
>         public void validate(Form<?> form)
>         {
>                 Date fromDate = fromDateFormComponent.getConvertedInput();
>                 Date toDate = toDateFormComponent.getConvertedInput();
>
>                 if (    fromDate != null
>                          && toDate != null
>                          && fromDate.after(toDate))
>                 {
>                         //error(fromDateFormComponent,
> "fromDateAfterToDate");
>
>                         ValidationError error = new ValidationError();
>                         error.addMessageKey(getClass().getSimpleName() +
> "." +
> "fromDateAfterToDate");
>
> fromDateFormComponent.newValidatable().error(error);
>                         //this.error(fromDateFormComponent);
>                 }
>         }
> }
>
> FromDateBeforeToDate.properties
>
> #fromDateAfterToDate=Invalid input: The '${label}' date is after the
> '${label}' date
> FromDateBeforeToDate.fromDateAfterToDate=Invalid input: The '${label}' date
> is after the '${label}' date
>
>
>
> Thanks.
>
> --
> View this message in context:
> http://apache-wicket.1842946.n4.nabble.com/FormValidators-and-messages-property-file-tp4667767.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
>
>