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 [EMAIL PROTECTED]: 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 [EMAIL PROTECTED]: *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="@[EMAIL PROTECTED]()"/>
   </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="@[EMAIL PROTECTED]()"/>
    </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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



---------------------------------------------------------------------
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]



---------------------------------------------------------------------
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