1

The validation works fine with the required string (and email, I tested just these). But when I try to use regex, the validation has no effect.

<!DOCTYPE validators PUBLIC "-//Apache Struts//XWork Validator 1.0.3//EN"
        "http://struts.apache.org/dtds/xwork-validator-1.0.3.dtd">
<validators>
    <field name="cognome">
        <field-validator type="requiredstring">
            <param name="trim">true</param>
            <message>Cognome è un campo obbligatorio</message>
        </field-validator>
    </field>
    <field name="telefono">
        <field-validator type="requiredstring">
            <message key="errors.required" />
        </field-validator>
        <field-validator type="regex">
            <param name="expression">[0-9]{9,15}</param>
            <message>Il numero di telefono deve essere minimo di 9 cifre
            </message>
        </field-validator>
    </field>

</validators>

telefono must have between 9 and 15 numeric digits. This give error when the fields are missing, and no error if I insert a telefono like "123".

the jsp:

<%@ page language="java" contentType="text/html;"
     import="java.util.*,it.almaviva.bean.*,it.almaviva.delegate.*;"%>
<%@ taglib prefix="s" uri="/struts-tags"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link href="${pageContext.request.contextPath}/css/stile1.css" rel="stylesheet" type="text/css" />
<title>Registrazione account</title>
</head>

<body>
<jsp:include page="header.jsp"/> 
<s:actionerror />

<s:form name="formDatiUtente" action="inviaRichiesta.action" method="post" theme="simple" validate="true">

<center>
<s:fielderror></s:fielderror>
<table width="48%" class="LISTA" border="0" cellPadding="3" cellSpacing="5" align="center">
                <tr>
                    <td width="35%">
                        <p class="testodx">
                            <s:text name="label.cognome" />
                        </p>
                    </td>
                    <td>
                        <p class="testosx">
                            <s:textfield name="cognome" id="idCognome"
                            size="30" value="%{anagraficaVDR.cognome}" />
                        </p>
                    </td>
                </tr>

                <tr>
                    <td>
                        <p class="testodx"><s:text name="label.telefono_Ufficio_reparto" /></p>
                    </td>
                    <td>
                        <s:textfield name="telefono" id="idTelefono_Ufficio_reparto" size="30" value="%{anagraficaVDR.telefono}"/>
                    </td>
                </tr>

</table>
<br>

<s:if test="!gestioneAmministratore">
    <s:submit method="execute" cssClass="bottone" key="label.invia" align="center" />
</s:if>    
</center>  

</s:form>
</body>
</html>

another secondary problem is that if I remove this:

<s:fielderror></s:fielderror>

the error messages are not displayed. That it's strange, because for example here this string is missing and the error messages are showed correctly (each under every field, thing that I prefer).

Another secondary problem is that if I doesn't insert a field and I press submit, I get (correctly) an error, but all the values in the fields are erased, instead I prefer to keep all (like in the previous example that I linked).

Accollativo
  • 1,537
  • 4
  • 32
  • 56

2 Answers2

5

The parameter should be regexExpression, not expression.

http://struts.apache.org/development/2.x/docs/regex-validator.html

To display a specific field's error, use the fieldName attribute, e.g.,

<s:fielderror fieldName="cogname" />

Note that <s:form>'s validate attribute regards client-side validation; it has nothing to do with validation on the server side.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • Thanks, this works! And about the other secondary problem? How do I show every message errors near the correspondent field? If I remove the error's feedback is lost. – Accollativo Feb 25 '14 at 16:36
2

For the secondary problem (but it should be another question, imho), the reason is that you need to put a fielderror with the name of the field under the field itself.

When using certain Struts Themes, like the default one, it's handled automatically.

Since you are probably using Simple Theme, then you need to do it yourself (also note that you should use label instead of paragraphs):

<label for="idCognome">
    <s:text name="label.cognome" />
</label>

<s:textfield name="cognome" id="idCognome" value="%{anagraficaVDR.cognome}" />

<s:fielderror fieldName="cognome"/>

The field will be rendered only in case a fielderror named "cognome" will be found.

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • Yes, maybe 3 questions should be better... and about the other? Why if do I get an error all fields are erased? How can I prevent this? – Accollativo Feb 25 '14 at 16:59
  • 1
    because you are using another source (anagraficaVDR) for the value of your objects, [and that source is not loaded yet](http://stackoverflow.com/a/17186258/1654265). Then use prepare() or something similar. – Andrea Ligios Feb 25 '14 at 17:06
  • Doesn't exist another alternative way instead of prepare()? I should copy the data somewhere and after copy them in the prepare(), it's quite long... – Accollativo Feb 26 '14 at 13:24
  • 1
    Fare domande multiple è il tuo tallone da killer ? :) Aprine una nuova e posta il codice (interessato) della action... – Andrea Ligios Feb 26 '14 at 13:42
  • I hope the god of code would forgive me: http://stackoverflow.com/questions/22045493/struts2-validation-repopulate-all-the-fields-when-validation-fails – Accollativo Feb 26 '14 at 15:13