2

I have a DateTextField component in my application and I want the input of date using a predefined pattern. The pattern that I need is "yyyy-MM-dd". I created the DateTextField using the following code.

    DateTextField dtf_ExpiryDate = DateTextField.forDatePattern("ExpDate", "yyyy-MM-dd");
    dtf_ExpiryDate.add(new DatePicker());

It helps to prevent date input in most of other formats. But it accepts input in dd-MM-yyyy format and converts it into some weird format. For example, 12-06-2013 is automatically converted to 0012-06-20.

Is there a way to throw an error when the date is given in dd-MM-yyyy ?

Javadocs of DateTextField says that the conversion is done internally using the Joda time parser. Is there a way to add more constraints ?

I really don't want the data to be captured as String and add a StringValidator to check if it confirms to the pattern using Regex.

Thank you

Jay
  • 690
  • 1
  • 10
  • 27
  • Doesn't DateTextField have some overridable `validate()` method? – Ondra Žižka Jul 07 '13 at 07:52
  • The validate() method is available if we add a custom Validator to the DateTextField. But the data (of type Validatable) that the method get is of 'Date' Type, which is already converted. So we cannot validate the pattern at that point. As I said, if I use a string to capture the data, then I can use the validate() method to throw the error as I will be validating a String. – Jay Jul 08 '13 at 09:20
  • 1
    And why don't you want StringValidator with regex? That's kind of normal way in Wicket... bound to other mechanism. – Ondra Žižka Jul 08 '13 at 10:57

2 Answers2

1

As far as I know, the answer is NO. DateTextField uses DatePattern or DateStyles internally and it is not possible to enforce stricter rules. If you don't want to use Javascript (custom) or JQuery, you have to use a REGEX to validate the input.

Have a look at here. Use the StrictPatternDateConverter class shown below to enforce a strict validation for the input.

  • I guess, fundamentally that's all I could do. Thank you. StrictPatternDateConverter is a good pointer. – Jay Jul 15 '13 at 12:49
0

Yep! StringRegExp is a good solution. But another solution could be the use of jqWicket. It provides a MaskedInputBehavior. Take a look at: http://code.google.com/p/jqwicket/wiki/MaskedInputExample

LinuxLuigi
  • 693
  • 6
  • 14