The
year is handled last simply because the Select for it goes last in the template, so at
the time the setYear method is invoked, the date selected by the user is already fully
known to the component.
This means that one possible answer to where to handle validation is the setYear
method of the DateInput class:
public void setYear(int year)
{
c.set(Calendar.YEAR, year);
date = c.getTime();
if (dateFrom != null && date.before(dateFrom))
{
tracker.recordError(this, messages.get("too-early"));
}
}
The recordError method of ValidationTracker has two versions. The simpler
version takes only one parameter, a String for an error message. The other version,
which is used here, takes one additional parameter, a Field, and as you will see
soon, Tapestry will decorate the label of the field in error. Since our component is a
Field, we simply pass a reference to it as the first parameter.
Finally, we do not simply provide some String for an error message, but we do
it properly, using a reference to message catalogs, so that the error message is
displayed in a currently preferred locale. The last step of this experiment is to add
component-specific message catalogs (we could also put the error message into
either of the page's or root message catalogs too, if we considered that as a
better solution).
Pages:
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277