Validations in struts2

0

I have a form on a server Tomcat that works with struts . I have not created this development environment so I do not know very well how it works.

The form is created, works correctly and returns the data correctly, now I can create the validations with struts . I understand slightly how it works but I have not found any examples on the Internet that can help me in my validation.

In the form you have the following fields to fill in.

  • Name
  • Last name 1
  • Last 2
  • Identification number

And the validation has to check the following.

  • Name, Surname 1 and Surname 2 are filled in.
  • Identification number and (First or last name 1 or Last name 2) are filled in.

In order for the form to validate, one of the 2 options must be fulfilled.

The form is this:

<html:form action="registroColegiados.do" method="post">    
    <input type="hidden" id="idaccion" name="accion" value="filtrar"/>  
    <div id="filtro">
        <fieldset>
            <legend><bean:message key="paginas.listadocolegiados.encabezado.filtro" /> </legend>
            <dl>
                <!-- NOMBRE -->
                <dt>
                    <label for="texto"> <bean:message key="paginas.filtros.nombrecolegiado" /> </label>
                </dt>
                <dd>
                    <html:text name="colegiadoFiltrosNavegacionActionForm" property="filtronombre" maxlength="50" size="50" />
                </dd>
                <!-- APELLIDO 1 -->
                <dt>
                    <label for="texto"> <bean:message key="paginas.filtros.ape1" /> </label>
                </dt>
                <dd>
                    <html:text name="colegiadoFiltrosNavegacionActionForm" property="filtroape1" maxlength="50" size="50" />
                </dd>                   
                <!-- APELLIDO 2 -->
                <dt>
                    <label for="texto"> <bean:message key="paginas.filtros.ape2" /> </label>
                </dt>
                <dd>
                    <html:text name="colegiadoFiltrosNavegacionActionForm" property="filtroape2" maxlength="50" size="50" />
                </dd>
                <!-- NIF -->

                <!-- N IDENTIFICACION -->
                <dt>
                    <label for="texto"> <bean:message key="paginas.filtros.ncidentificacion" /> </label>
                </dt>
                <dd>
                    <html:text name="colegiadoFiltrosNavegacionActionForm" property="filtroncidentificacion" maxlength="50" size="50" />
                </dd>                   
                <dt>
                    <label for="areadeconocimiento">&nbsp;</label>
                </dt>
                <dd>
                    <html:submit styleId="iddefaultsubmit" property="submit" ><bean:message key="paginas.filtros.filtrar" /></html:submit>
                    <input type="hidden" id="idaccion" name="accion" value=""/>
                </dd>
          </dl>
        </fieldset>
    </div>  
</html:form>

There is a page called validation.xml , this contains what validations are made in the forms, an example could be the following:

<form name ="FOGaleriaFiltrosNavegacionFormBean">
            <field
                property="filtrofechapubini"
                depends="date">
                    <arg position="0" key="filtro.fechaini"/>
                    <var>
                        <var-name>datePattern</var-name>
                        <var-value>dd/MM/yyyy</var-value>
                    </var>
            </field>
            <field
                property="filtrofechapubfin"
                depends="date">
                    <arg position="0" key="filtro.fechafin"/>
                    <var>
                        <var-name>datePattern</var-name>
                        <var-value>dd/MM/yyyy</var-value>
                    </var>
            </field>
            <field
                property="filtrofechapubfin"
                depends="mayorqueFecha">
                    <arg position="0" key="filtro.fechafin"/>
                    <arg position="1" key="filtro.fechaini"/>
                    <var>
                        <var-name>campo</var-name>
                        <var-value>filtrofechapubini</var-value>
                    </var>
            </field>
        </form>

The depends tag calls a validator-rules.xml method, for example, mayorqueFecha calls the following method on this page.

                       

             function esMayorqueFecha(form)
             {
               var isValid = true;
               var focusField = null;
               var i = 0;
               var fields = new Array();
               var formName = form.getAttributeNode("name"); 

               oMayorqueFecha = eval('new ' + formName.value + '_mayorqueFecha()');
               for (x in oMayorqueFecha)
               {
                   var field = form[oMayorqueFecha[x][0]];

                   if ((field.type == 'hidden' || field.type == 'text' || field.type == 'textarea') && (field.value.length > 0)  && field.disabled == false)
                   {
                       var fieldtwo = form[oMayorqueFecha[x][2]("campo")];

                       var partes  = field.value.split("/");
                       var field_value_alreves  = partes[2] + partes[1] + partes[0];
                       var partestwo  = fieldtwo.value.split("/");
                       var fieldtwo_value_alreves  = partestwo[2] + partestwo[1] + partestwo[0];

                       if(field_value_alreves < fieldtwo_value_alreves)
                       {
                             if (i == 0)
                             {
                                   focusField = field;
                             }

                             fields[i++] = oMayorqueFecha[x][1];
                             isValid = false;
                       }
                   }
              }

              if (fields.length > 0)
              {
                   focusField.focus();
                   alert(fields.join('\n'));
              }

              //alert(field_value_alreves);
              //alert(fieldtwo_value_alreves);

              return isValid;

             }
    ]]>
          </javascript>

As I say, I understand how it works, but I do not know how to do what I need.

    
asked by Lombarda Arda 01.03.2018 в 08:27
source

0 answers