You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Martin Carel <ma...@pricerunner.com> on 2006/02/22 10:23:35 UTC

Date validation tweaks

Hi!

There are 2 tweaks I'd like to do with date validations.


Tweak #1

Would there be a way to set the minDate to the beginning of the day (and 
thus make the exact time at which it was instanciated irrelevant)?
At this moment, in my page spec, I have:

<bean name="minDate1" class="org.apache.tapestry.form.validator.MinDate">
  <set name="minDate" value="new java.util.Date()"/>
</bean>

Now with my DatePicker component, when I enter today's date, it does not 
get through validation, as 2006-02-22 00:00 (the DatePicker's date) is 
before 2006-02-22 10:04 (the minDate) for example.


Tweak #2

And is it possible to tweak the validation error message not to display 
the time?

Now I have:
Drop off date must be on or after 2006-02-23 10:04.

And I would like:
Drop off date must be on or after 2006-02-23.

Thanks in advance.

/Martin


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Date validation tweaks

Posted by Bryan Lewis <br...@maine.rr.com>.
While we're on this subject...  Tapestry's built-in DateTranslator uses
a lenient DateFormat, one of my pet peeves.  When a user enters a date
like 03/44/2005 it's probably an error... I don't want it silently
converted to 04/13/2005.

I could override the DateTranslator's behavior in code, add a
format.setLenient(false), and tell hivemind to use that as my
DateTranslator... not too bad I guess.  Is there a better way?



Bryan Lewis wrote:

>Thanks guys.  Now I can get rid of my custom MaxDate validator class!
>
>
>Martin Carel wrote:
>
>  
>
>>Hi Kirby,
>>
>>    
>>
>>><set name="message" value="{0} must be on or before {1,date,MM dd
>>>      
>>>
>>yyyy}" />
>>
>>I just want to mention that I needed to add 'literal' in order for
>>this to work (otherwise I have a parsing exception as 'ognl' is the
>>default prefix for page spec):
>>
>><set name="message" value="literal:{0} must be on or before {1,date,MM
>>dd yyyy}" />
>>
>>Thanks for this, I can now get rid of my custom MinDate validator class!
>>
>>
>>    
>>
>>>To answer your localisation question - just change the above set to:
>>><set name="message" value="message:dateBeforeErrorMessage" />
>>>And put into your localisation properties file:
>>>dateBeforeErrorMessage={0} must be on or before {1,date,MM/dd/yyyy}
>>>And the equivalent in your Swedish localisation properties file.
>>>      
>>>
>>I understand what you want me to do, but I see it as a workaround (I
>>shouldn't be forced to play with the Swedish locale properties file).
>>What I really want to do is to force my locale to English :(
>>
>>/Martin
>>
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>>For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>>
>>    
>>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>  
>


Re: Date validation tweaks

Posted by Bryan Lewis <br...@maine.rr.com>.
Thanks guys.  Now I can get rid of my custom MaxDate validator class!


Martin Carel wrote:

> Hi Kirby,
>
> > <set name="message" value="{0} must be on or before {1,date,MM dd
> yyyy}" />
>
> I just want to mention that I needed to add 'literal' in order for
> this to work (otherwise I have a parsing exception as 'ognl' is the
> default prefix for page spec):
>
> <set name="message" value="literal:{0} must be on or before {1,date,MM
> dd yyyy}" />
>
> Thanks for this, I can now get rid of my custom MinDate validator class!
>
>
> > To answer your localisation question - just change the above set to:
> > <set name="message" value="message:dateBeforeErrorMessage" />
> > And put into your localisation properties file:
> > dateBeforeErrorMessage={0} must be on or before {1,date,MM/dd/yyyy}
> > And the equivalent in your Swedish localisation properties file.
>
> I understand what you want me to do, but I see it as a workaround (I
> shouldn't be forced to play with the Swedish locale properties file).
> What I really want to do is to force my locale to English :(
>
> /Martin
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Date validation tweaks

Posted by Martin Carel <ma...@pricerunner.com>.
Hi Kirby,

 > <set name="message" value="{0} must be on or before {1,date,MM dd 
yyyy}" />

I just want to mention that I needed to add 'literal' in order for this 
to work (otherwise I have a parsing exception as 'ognl' is the default 
prefix for page spec):

<set name="message" value="literal:{0} must be on or before {1,date,MM 
dd yyyy}" />

Thanks for this, I can now get rid of my custom MinDate validator class!


 > To answer your localisation question - just change the above set to:
 > <set name="message" value="message:dateBeforeErrorMessage" />
 > And put into your localisation properties file:
 > dateBeforeErrorMessage={0} must be on or before {1,date,MM/dd/yyyy}
 > And the equivalent in your Swedish localisation properties file.

I understand what you want me to do, but I see it as a workaround (I 
shouldn't be forced to play with the Swedish locale properties file). 
What I really want to do is to force my locale to English :(

/Martin


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Date validation tweaks

Posted by Richard Kirby <rb...@capdm.com>.
Hi Martin,

Sorry, looks like I wasn't looking closely enough at what you wanted to do.

I believe your problem is this:

<bean name="minDate1" 
class="org.apache.tapestry.form.validator.MinDate,message={0} must be on 
or before {1,minDate,MMM/dd/yyyy}">
 <set name="minDate" value="new java.util.Date()"/>     
</bean>

Try instead:

<bean name="minDate1" class="org.apache.tapestry.form.validator.MinDate">
 <set name="message" value="{0} must be on or before {1,date,MM/dd/yyyy}" />
 <set name="minDate" value="new java.util.Date()"/>     
</bean>

Note that the 1,date,MM/dd/yyyy means that the second value used to 
format the message is of type date (look at the 
MessageFormat/DateFormat/SimpleDateFormat standard Java classes to 
understand this).

To answer your localisation question - just change the above set to:

  <set name="message" value="message:dateBeforeErrorMessage" />

And put into your localisation properties file:

dateBeforeErrorMessage={0} must be on or before {1,date,MM/dd/yyyy}

And the equivalent in your Swedish localisation properties file.

Richard

Martin Carel wrote:
> Hi!
>
> Thanks both for your feedback. I successfully solved my tweak 1 (will 
> post another message for explaining how today).
>
>
> > You should also be able to specify a custom format:
> > {0} must be on or before {1,date,MM/dd/yyyy}
>
> That'd be nice if I could do that. I used:
>
> <bean name="minDate1" 
> class="org.apache.tapestry.form.validator.MinDate,message={0} must be 
> on or before {1,minDate,MMM/dd/yyyy}">
>  <set name="minDate" value="new java.util.Date()"/>      </bean>
>
> But I have this error:
>
> Unable to read OGNL expression '<parsed OGNL expression>' of 
> $Search_0@1005f31[Search]: Error initializing property message={0} 
> must be on or before {1,minDate,MMM/dd/yyyy} of bean 'minDate1' (of 
> component Search): Unable to update property minDate of object 
> org.apache.tapestry.form.validator.MinDate@1e9caac: *argument type 
> mismatch
>
> -- 
>
> *Therefore I used my own MinDate validator and it worked out fine 
> (using SimpleDateFormat when building my error message), although I 
> would have preferred the first approach above:
>
> private static SimpleDateFormat dateFormat = new SimpleDateFormat("dd 
> MMM yyyy");
>
> private String buildMessage(ValidationMessages messages, 
> IFormComponent field)
>    {
>        return messages.formatValidationMessage(
>                getMessage(),
>                ValidationStrings.DATE_TOO_EARLY,
>                new Object[] { field.getDisplayName(), 
> dateFormat.format(_minDate) });
>    }
>
> One thing is really annoying though. I have all my validation messages 
> in Swedish instead of English :( I already asked that on that mailing 
> list, but I'm still stuck with this problem :( Is there something 
> special to do in order to force the locale for validation messages?
>
> Here is my custom Engine:
>
> public MyEngine(){
>  setLocale(Locale.ENGLISH);
> }
>
> public void setLocale(Locale locale) {
>        if (getLocale() == null) {
>    super.setLocale(Locale.ENGLISH);
>  } else {
>    super.setLocale(locale);
>  }      TheLog.note("locale = " + getLocale()); // does show up with 
> English in the log all the time
> }
>
> Thank you again.
> *
> */Martin
>
>
> Richard Kirby wrote:
>> Hi Bryan,
>>
>> Try
>>
>>   <bean name="maxDateValidator"
>> class="org.apache.tapestry.form.validator.MaxDate,message={0} must be on
>> or before {1,date,short}">
>>        <set name="maxDate" value="@common.CDate@today()"/>
>>    </bean>
>>
>> Assuming the message conforms to the standard java.text.MessageFormat 
>> class, then it should display the date in the standard short format.
>>
>> You should also be able to specify a custom format:
>>
>> {0} must be on or before {1,date,MM/dd/yyyy}
>>
>> Cheers
>>
>> Richard
>>
>> Bryan Lewis wrote:
>>> It's certainly possible to do those things with code, although that
>>> approach seems to be falling out of fashion these days. :-)
>>>
>>> #1.  Write a utility method to call instead of java.util.Date().  
>>> What I
>>> use:
>>>
>>>     public static Date today() {
>>>         return org.apache.commons.lang.time.DateUtils.truncate(new
>>> java.util.Date(), Calendar.DATE);
>>>     }
>>>
>>> #2.  I also couldn't see how to tweak the error message elegantly.  
>>> Here
>>> are my notes from a couple of months ago:
>>>
>>> I can tweak the error message format like so:
>>>
>>>     <bean name="maxDateValidator"
>>> class="org.apache.tapestry.form.validator.MaxDate,message={0} must 
>>> be on
>>> or before {1}">
>>>         <set name="maxDate" value="@common.CDate@today()"/>
>>>     </bean>
>>>
>>> But that's not flexible enough.  It still says:
>>>
>>>     Last Password Date must be on or before 12/31/05 12:00 AM
>>>
>>> when I want:
>>>
>>>     Last Password Date must be on or before 12/31/2005
>>>
>>> It isn't hard to customize our own subclass of the validator though. 
>>> Start by
>>> copying the code for 
>>> org.apache.tapestry.form.validator.MaxDate.java, and
>>> change the buildMessage() method:
>>>
>>>     private String buildMessage(ValidationMessages messages,
>>> IFormComponent field) {
>>>         String maxDateFormattedString = // format the maxDate the 
>>> way we
>>> want
>>>
>>> I can't use the shorthand declaration though... can't put code in the
>>> initializer:
>>>
>>>     <binding name="validators" 
>>> value="validators:required,maxDate=today()"/>
>>>
>>> So stick with the bean technique.
>>>
>>>
>>>  
>>>
>>>
>>> Martin Carel wrote:
>>>
>>>  
>>>> Hi!
>>>>
>>>> There are 2 tweaks I'd like to do with date validations.
>>>>
>>>>
>>>> Tweak #1
>>>>
>>>> Would there be a way to set the minDate to the beginning of the day
>>>> (and thus make the exact time at which it was instanciated 
>>>> irrelevant)?
>>>> At this moment, in my page spec, I have:
>>>>
>>>> <bean name="minDate1" 
>>>> class="org.apache.tapestry.form.validator.MinDate">
>>>>  <set name="minDate" value="new java.util.Date()"/>
>>>> </bean>
>>>>
>>>> Now with my DatePicker component, when I enter today's date, it does
>>>> not get through validation, as 2006-02-22 00:00 (the DatePicker's
>>>> date) is before 2006-02-22 10:04 (the minDate) for example.
>>>>
>>>>
>>>> Tweak #2
>>>>
>>>> And is it possible to tweak the validation error message not to
>>>> display the time?
>>>>
>>>> Now I have:
>>>> Drop off date must be on or after 2006-02-23 10:04.
>>>>
>>>> And I would like:
>>>> Drop off date must be on or after 2006-02-23.
>>>>
>>>> Thanks in advance.
>>>>
>>>> /Martin
>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>>>> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>>>>
>>>>     
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>>> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>>>
>>>   
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Date validation tweaks

Posted by Martin Carel <ma...@pricerunner.com>.
Hi!

Here is how I solved my tweak #1 (specifying a date (at the beginning of 
the day) as a default value).

I implemented my own TypeConverter:

public class IntToDateConverter implements TypeConverter {           

    static private Calendar getTodaysDate(){
       
        Calendar now = Calendar.getInstance();       
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(now.get(Calendar.YEAR), now.get(Calendar.MONTH), 
now.get(Calendar.DATE));       
        return calendar;       
    }
   
    public Object convertValue(Object arg0) {       
       
        int amountDays = ((Integer) arg0).intValue();
        Calendar calendar = getTodaysDate();
        calendar.add(Calendar.DAY_OF_MONTH, amountDays);
        return calendar.getTime();
    }
}


And I did a contribution to the TypeConverters in hivemodule.xml:

<contribution configuration-id="tapestry.coerce.TypeConverters">
        <converter class="java.util.Date" 
object="service:IntToDateConverter"/>
</contribution>


And then I can do:

<bean name="minDate" class="org.apache.tapestry.form.validator.MinDate">
  <set name="minDate" value="0"/>
</bean>

or for example to specify two days ahead:

<bean name="minDate" class="org.apache.tapestry.form.validator.MinDate">
  <set name="minDate" value="2"/>
</bean>

Works fine for me :)

/Martin


Martin Carel wrote:
> Hi!
>
> Thanks both for your feedback. I successfully solved my tweak 1 (will 
> post another message for explaining how today).
>
>
> > You should also be able to specify a custom format:
> > {0} must be on or before {1,date,MM/dd/yyyy}
>
> That'd be nice if I could do that. I used:
>
> <bean name="minDate1" 
> class="org.apache.tapestry.form.validator.MinDate,message={0} must be 
> on or before {1,minDate,MMM/dd/yyyy}">
>  <set name="minDate" value="new java.util.Date()"/>      </bean>
>
> But I have this error:
>
> Unable to read OGNL expression '<parsed OGNL expression>' of 
> $Search_0@1005f31[Search]: Error initializing property message={0} 
> must be on or before {1,minDate,MMM/dd/yyyy} of bean 'minDate1' (of 
> component Search): Unable to update property minDate of object 
> org.apache.tapestry.form.validator.MinDate@1e9caac: *argument type 
> mismatch
>
> -- 
>
> *Therefore I used my own MinDate validator and it worked out fine 
> (using SimpleDateFormat when building my error message), although I 
> would have preferred the first approach above:
>
> private static SimpleDateFormat dateFormat = new SimpleDateFormat("dd 
> MMM yyyy");
>
> private String buildMessage(ValidationMessages messages, 
> IFormComponent field)
>    {
>        return messages.formatValidationMessage(
>                getMessage(),
>                ValidationStrings.DATE_TOO_EARLY,
>                new Object[] { field.getDisplayName(), 
> dateFormat.format(_minDate) });
>    }
>
> One thing is really annoying though. I have all my validation messages 
> in Swedish instead of English :( I already asked that on that mailing 
> list, but I'm still stuck with this problem :( Is there something 
> special to do in order to force the locale for validation messages?
>
> Here is my custom Engine:
>
> public MyEngine(){
>  setLocale(Locale.ENGLISH);
> }
>
> public void setLocale(Locale locale) {
>        if (getLocale() == null) {
>    super.setLocale(Locale.ENGLISH);
>  } else {
>    super.setLocale(locale);
>  }      TheLog.note("locale = " + getLocale()); // does show up with 
> English in the log all the time
> }
>
> Thank you again.
> *
> */Martin
>
>
> Richard Kirby wrote:
>> Hi Bryan,
>>
>> Try
>>
>>   <bean name="maxDateValidator"
>> class="org.apache.tapestry.form.validator.MaxDate,message={0} must be on
>> or before {1,date,short}">
>>        <set name="maxDate" value="@common.CDate@today()"/>
>>    </bean>
>>
>> Assuming the message conforms to the standard java.text.MessageFormat 
>> class, then it should display the date in the standard short format.
>>
>> You should also be able to specify a custom format:
>>
>> {0} must be on or before {1,date,MM/dd/yyyy}
>>
>> Cheers
>>
>> Richard
>>
>> Bryan Lewis wrote:
>>> It's certainly possible to do those things with code, although that
>>> approach seems to be falling out of fashion these days. :-)
>>>
>>> #1.  Write a utility method to call instead of java.util.Date().  
>>> What I
>>> use:
>>>
>>>     public static Date today() {
>>>         return org.apache.commons.lang.time.DateUtils.truncate(new
>>> java.util.Date(), Calendar.DATE);
>>>     }
>>>
>>> #2.  I also couldn't see how to tweak the error message elegantly.  
>>> Here
>>> are my notes from a couple of months ago:
>>>
>>> I can tweak the error message format like so:
>>>
>>>     <bean name="maxDateValidator"
>>> class="org.apache.tapestry.form.validator.MaxDate,message={0} must 
>>> be on
>>> or before {1}">
>>>         <set name="maxDate" value="@common.CDate@today()"/>
>>>     </bean>
>>>
>>> But that's not flexible enough.  It still says:
>>>
>>>     Last Password Date must be on or before 12/31/05 12:00 AM
>>>
>>> when I want:
>>>
>>>     Last Password Date must be on or before 12/31/2005
>>>
>>> It isn't hard to customize our own subclass of the validator though. 
>>> Start by
>>> copying the code for 
>>> org.apache.tapestry.form.validator.MaxDate.java, and
>>> change the buildMessage() method:
>>>
>>>     private String buildMessage(ValidationMessages messages,
>>> IFormComponent field) {
>>>         String maxDateFormattedString = // format the maxDate the 
>>> way we
>>> want
>>>
>>> I can't use the shorthand declaration though... can't put code in the
>>> initializer:
>>>
>>>     <binding name="validators" 
>>> value="validators:required,maxDate=today()"/>
>>>
>>> So stick with the bean technique.
>>>
>>>
>>>  
>>>
>>>
>>> Martin Carel wrote:
>>>
>>>  
>>>> Hi!
>>>>
>>>> There are 2 tweaks I'd like to do with date validations.
>>>>
>>>>
>>>> Tweak #1
>>>>
>>>> Would there be a way to set the minDate to the beginning of the day
>>>> (and thus make the exact time at which it was instanciated 
>>>> irrelevant)?
>>>> At this moment, in my page spec, I have:
>>>>
>>>> <bean name="minDate1" 
>>>> class="org.apache.tapestry.form.validator.MinDate">
>>>>  <set name="minDate" value="new java.util.Date()"/>
>>>> </bean>
>>>>
>>>> Now with my DatePicker component, when I enter today's date, it does
>>>> not get through validation, as 2006-02-22 00:00 (the DatePicker's
>>>> date) is before 2006-02-22 10:04 (the minDate) for example.
>>>>
>>>>
>>>> Tweak #2
>>>>
>>>> And is it possible to tweak the validation error message not to
>>>> display the time?
>>>>
>>>> Now I have:
>>>> Drop off date must be on or after 2006-02-23 10:04.
>>>>
>>>> And I would like:
>>>> Drop off date must be on or after 2006-02-23.
>>>>
>>>> Thanks in advance.
>>>>
>>>> /Martin
>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>>>> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>>>>
>>>>     
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>>> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>>>
>>>   
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Date validation tweaks

Posted by Martin Carel <ma...@pricerunner.com>.
Hi!

Thanks both for your feedback. I successfully solved my tweak 1 (will 
post another message for explaining how today).


 > You should also be able to specify a custom format:
 > {0} must be on or before {1,date,MM/dd/yyyy}

That'd be nice if I could do that. I used:

<bean name="minDate1" 
class="org.apache.tapestry.form.validator.MinDate,message={0} must be on 
or before {1,minDate,MMM/dd/yyyy}">
  <set name="minDate" value="new java.util.Date()"/>      
</bean>

But I have this error:

Unable to read OGNL expression '<parsed OGNL expression>' of 
$Search_0@1005f31[Search]: Error initializing property message={0} must 
be on or before {1,minDate,MMM/dd/yyyy} of bean 'minDate1' (of component 
Search): Unable to update property minDate of object 
org.apache.tapestry.form.validator.MinDate@1e9caac: *argument type mismatch

--

*Therefore I used my own MinDate validator and it worked out fine (using 
SimpleDateFormat when building my error message), although I would have 
preferred the first approach above:

private static SimpleDateFormat dateFormat = new SimpleDateFormat("dd 
MMM yyyy");

private String buildMessage(ValidationMessages messages, IFormComponent 
field)
    {
        return messages.formatValidationMessage(
                getMessage(),
                ValidationStrings.DATE_TOO_EARLY,
                new Object[] { field.getDisplayName(), 
dateFormat.format(_minDate) });
    }

One thing is really annoying though. I have all my validation messages 
in Swedish instead of English :( I already asked that on that mailing 
list, but I'm still stuck with this problem :( Is there something 
special to do in order to force the locale for validation messages?

Here is my custom Engine:

public MyEngine(){
  setLocale(Locale.ENGLISH);
}

public void setLocale(Locale locale) {
       
  if (getLocale() == null) {
    super.setLocale(Locale.ENGLISH);
  } else {
    super.setLocale(locale);
  }     
  TheLog.note("locale = " + getLocale()); // does show up with English 
in the log all the time
}

Thank you again.
*
*/Martin


Richard Kirby wrote:
> Hi Bryan,
>
> Try
>
>   <bean name="maxDateValidator"
> class="org.apache.tapestry.form.validator.MaxDate,message={0} must be on
> or before {1,date,short}">
>        <set name="maxDate" value="@common.CDate@today()"/>
>    </bean>
>
> Assuming the message conforms to the standard java.text.MessageFormat 
> class, then it should display the date in the standard short format.
>
> You should also be able to specify a custom format:
>
> {0} must be on or before {1,date,MM/dd/yyyy}
>
> Cheers
>
> Richard
>
> Bryan Lewis wrote:
>> It's certainly possible to do those things with code, although that
>> approach seems to be falling out of fashion these days. :-)
>>
>> #1.  Write a utility method to call instead of java.util.Date().  What I
>> use:
>>
>>     public static Date today() {
>>         return org.apache.commons.lang.time.DateUtils.truncate(new
>> java.util.Date(), Calendar.DATE);
>>     }
>>
>> #2.  I also couldn't see how to tweak the error message elegantly.  Here
>> are my notes from a couple of months ago:
>>
>> I can tweak the error message format like so:
>>
>>     <bean name="maxDateValidator"
>> class="org.apache.tapestry.form.validator.MaxDate,message={0} must be on
>> or before {1}">
>>         <set name="maxDate" value="@common.CDate@today()"/>
>>     </bean>
>>
>> But that's not flexible enough.  It still says:
>>
>>     Last Password Date must be on or before 12/31/05 12:00 AM
>>
>> when I want:
>>
>>     Last Password Date must be on or before 12/31/2005
>>
>> It isn't hard to customize our own subclass of the validator though. 
>> Start by
>> copying the code for org.apache.tapestry.form.validator.MaxDate.java, 
>> and
>> change the buildMessage() method:
>>
>>     private String buildMessage(ValidationMessages messages,
>> IFormComponent field) {
>>         String maxDateFormattedString = // format the maxDate the way we
>> want
>>
>> I can't use the shorthand declaration though... can't put code in the
>> initializer:
>>
>>     <binding name="validators" 
>> value="validators:required,maxDate=today()"/>
>>
>> So stick with the bean technique.
>>
>>
>>  
>>
>>
>> Martin Carel wrote:
>>
>>  
>>> Hi!
>>>
>>> There are 2 tweaks I'd like to do with date validations.
>>>
>>>
>>> Tweak #1
>>>
>>> Would there be a way to set the minDate to the beginning of the day
>>> (and thus make the exact time at which it was instanciated irrelevant)?
>>> At this moment, in my page spec, I have:
>>>
>>> <bean name="minDate1" 
>>> class="org.apache.tapestry.form.validator.MinDate">
>>>  <set name="minDate" value="new java.util.Date()"/>
>>> </bean>
>>>
>>> Now with my DatePicker component, when I enter today's date, it does
>>> not get through validation, as 2006-02-22 00:00 (the DatePicker's
>>> date) is before 2006-02-22 10:04 (the minDate) for example.
>>>
>>>
>>> Tweak #2
>>>
>>> And is it possible to tweak the validation error message not to
>>> display the time?
>>>
>>> Now I have:
>>> Drop off date must be on or after 2006-02-23 10:04.
>>>
>>> And I would like:
>>> Drop off date must be on or after 2006-02-23.
>>>
>>> Thanks in advance.
>>>
>>> /Martin
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>>> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>>>
>>>     
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>>
>>   
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Date validation tweaks

Posted by Richard Kirby <rb...@capdm.com>.
Hi Bryan,

Try

   <bean name="maxDateValidator"
class="org.apache.tapestry.form.validator.MaxDate,message={0} must be on
or before {1,date,short}">
        <set name="maxDate" value="@common.CDate@today()"/>
    </bean>

Assuming the message conforms to the standard java.text.MessageFormat 
class, then it should display the date in the standard short format.

You should also be able to specify a custom format:

{0} must be on or before {1,date,MM/dd/yyyy}

Cheers

Richard

Bryan Lewis wrote:
> It's certainly possible to do those things with code, although that
> approach seems to be falling out of fashion these days. :-)
>
> #1.  Write a utility method to call instead of java.util.Date().  What I
> use:
>
>     public static Date today() {
>         return org.apache.commons.lang.time.DateUtils.truncate(new
> java.util.Date(), Calendar.DATE);
>     }
>
> #2.  I also couldn't see how to tweak the error message elegantly.  Here
> are my notes from a couple of months ago:
>
> I can tweak the error message format like so:
>
>     <bean name="maxDateValidator"
> class="org.apache.tapestry.form.validator.MaxDate,message={0} must be on
> or before {1}">
>         <set name="maxDate" value="@common.CDate@today()"/>
>     </bean>
>
> But that's not flexible enough.  It still says:
>
>     Last Password Date must be on or before 12/31/05 12:00 AM
>
> when I want:
>
>     Last Password Date must be on or before 12/31/2005
>
> It isn't hard to customize our own subclass of the validator though. 
> Start by
> copying the code for org.apache.tapestry.form.validator.MaxDate.java, and
> change the buildMessage() method:
>
>     private String buildMessage(ValidationMessages messages,
> IFormComponent field) {
>         String maxDateFormattedString = // format the maxDate the way we
> want
>
> I can't use the shorthand declaration though... can't put code in the
> initializer:
>
>     <binding name="validators" value="validators:required,maxDate=today()"/>
>
> So stick with the bean technique.
>
>
>  
>
>
> Martin Carel wrote:
>
>   
>> Hi!
>>
>> There are 2 tweaks I'd like to do with date validations.
>>
>>
>> Tweak #1
>>
>> Would there be a way to set the minDate to the beginning of the day
>> (and thus make the exact time at which it was instanciated irrelevant)?
>> At this moment, in my page spec, I have:
>>
>> <bean name="minDate1" class="org.apache.tapestry.form.validator.MinDate">
>>  <set name="minDate" value="new java.util.Date()"/>
>> </bean>
>>
>> Now with my DatePicker component, when I enter today's date, it does
>> not get through validation, as 2006-02-22 00:00 (the DatePicker's
>> date) is before 2006-02-22 10:04 (the minDate) for example.
>>
>>
>> Tweak #2
>>
>> And is it possible to tweak the validation error message not to
>> display the time?
>>
>> Now I have:
>> Drop off date must be on or after 2006-02-23 10:04.
>>
>> And I would like:
>> Drop off date must be on or after 2006-02-23.
>>
>> Thanks in advance.
>>
>> /Martin
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>>
>>     
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>   


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Date validation tweaks

Posted by Bryan Lewis <br...@maine.rr.com>.
It's certainly possible to do those things with code, although that
approach seems to be falling out of fashion these days. :-)

#1.  Write a utility method to call instead of java.util.Date().  What I
use:

    public static Date today() {
        return org.apache.commons.lang.time.DateUtils.truncate(new
java.util.Date(), Calendar.DATE);
    }

#2.  I also couldn't see how to tweak the error message elegantly.  Here
are my notes from a couple of months ago:

I can tweak the error message format like so:

    <bean name="maxDateValidator"
class="org.apache.tapestry.form.validator.MaxDate,message={0} must be on
or before {1}">
        <set name="maxDate" value="@common.CDate@today()"/>
    </bean>

But that's not flexible enough.  It still says:

    Last Password Date must be on or before 12/31/05 12:00 AM

when I want:

    Last Password Date must be on or before 12/31/2005

It isn't hard to customize our own subclass of the validator though. 
Start by
copying the code for org.apache.tapestry.form.validator.MaxDate.java, and
change the buildMessage() method:

    private String buildMessage(ValidationMessages messages,
IFormComponent field) {
        String maxDateFormattedString = // format the maxDate the way we
want

I can't use the shorthand declaration though... can't put code in the
initializer:

    <binding name="validators" value="validators:required,maxDate=today()"/>

So stick with the bean technique.


 


Martin Carel wrote:

> Hi!
>
> There are 2 tweaks I'd like to do with date validations.
>
>
> Tweak #1
>
> Would there be a way to set the minDate to the beginning of the day
> (and thus make the exact time at which it was instanciated irrelevant)?
> At this moment, in my page spec, I have:
>
> <bean name="minDate1" class="org.apache.tapestry.form.validator.MinDate">
>  <set name="minDate" value="new java.util.Date()"/>
> </bean>
>
> Now with my DatePicker component, when I enter today's date, it does
> not get through validation, as 2006-02-22 00:00 (the DatePicker's
> date) is before 2006-02-22 10:04 (the minDate) for example.
>
>
> Tweak #2
>
> And is it possible to tweak the validation error message not to
> display the time?
>
> Now I have:
> Drop off date must be on or after 2006-02-23 10:04.
>
> And I would like:
> Drop off date must be on or after 2006-02-23.
>
> Thanks in advance.
>
> /Martin
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org