Reputation: 1559
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).
Upvotes: 1
Views: 2148
Reputation: 50203
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.
Upvotes: 2
Reputation: 160191
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.
Upvotes: 5