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