Date validation in struts2

I create a form in which the user logs in to the struts2 application. The user will have to enter the date in a specific format.

Since I will not use the ajax datepicker tag, I use a text box with a date tag in a form similar to this:

<s:date name="birthDate" id="bDateId" format="yyyy-MM-dd"/>  
<s:textfield name="birthDate" id="%{bDateId}" label="Birth Date (yyyy-MM-dd)"/>

The main user object has a String field to represent the date. So my question is that there is a straighforward method to apply validation regarding user input format for date field. The date validator provided by struts2 can be used to check only date ranges, but not for specific formats.

Can someone recommend a way to do this or not point to an example of a custom validator?

Thank you for your advice. Respectfully to all.

+3
source share
2 answers

You will probably pretty easily confirm this with the regex validator. Sort of:

<field name="birthDate">
  <field-validator type="regex">
      <param name="expression">[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]</param>
      <message>The value must be in the format yyyy-MM-dd</message>
 </field-validator>

+1
source

You can verify this with a date validator. Sort of:

   <field name="fecha">

        <field-validator type="date">
            <param name="min">01/01/1900</param>
            <param name="max">01/01/9999</param>
            <message key="error.fecha.rango.invalido"/>
        </field-validator>       
    </field>
+1
source

Source: https://habr.com/ru/post/1711838/


All Articles