You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Raul Arabaolaza Barquin <ra...@mundivia.net> on 2009/02/17 18:06:50 UTC

Using both custom converters and declarative validation with struts 2

Hi,

I´m currently evaluating Struts 2 and i have some problems when mixing custom converters and declarative validation.

I´ve a Person class like this:

public class Person {
	
	private String nombre;

	private Person padre;
	public String getNombre() {
		return nombre;
	}

	public void setNombre(String nombre) {
		this.nombre = nombre;
	}

	public Person getPadre() {
		return padre;
	}

	public void setPadre(Person padre) {
		this.padre = padre;
	}

}

And a custom converter like this (i think is a very bad example of the use of converters, but i´m following orders)

public class PersonConverter extends StrutsTypeConverter {

	@Override
	public Object convertFromString(Map arg0, String[] arg1, Class arg2) {
		Person devolver=new Person();
		devolver.setNombre(arg1[0]);
		devolver.setPadre(new Person());
		return devolver;
	}

	@Override
	public String convertToString(Map arg0, Object arg1) {
		Person p=(Person)arg1;
		return p.getNombre();
	}

}

Now i have a very easy form like this:

<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
		<title>Hola Mundo</title>
		<s:head/>
	</head>
	<body>
		<s:actionerror/>
		<s:fielderror/>
		<s:form action="procesar" validate="true">
			<s:textfield label="Nombre del hijo" name="persona" />
			<s:textfield label="Nombre del padre" name="persona.padre" />
			<s:submit />
		</s:form>
	</body>
</html> 

And in my action i have a private Person called persona

When using server side validation all works fine, checking the logs it seems validation happends after conversion, so I check my persona bean fields.

<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
	<validators>
    <field name="persona.nombre">
        <field-validator type="requiredstring">
            <message>Debe introducir un nombre valido</message>
        </field-validator>
    </field>
    <field name="persona.padre.nombre">
        <field-validator type="requiredstring">
            <message>Debe introducir un nombre para el padre valido</message>
        </field-validator>
    </field>
    <validator type="expression">
      <param name="expression">persona.nombre.length() gt persona.padre.nombre.length() </param>
      <message>El nombre del hijo debe ser más grande que el del padre: Nombre del hijo ${persona.nombre}, nombre del padre ${persona.padre.nombre}</message>
  </validator>
    
</validators>

But if i want to use client side validation i have a problem, in client side conversion has not happend, so it seems i need the following configuration.

<!DOCTYPE validators PUBLIC "-//OpenSymphony Group//XWork Validator 1.0.2//EN" "http://www.opensymphony.com/xwork/xwork-validator-1.0.2.dtd">
	<validators>
    <field name="persona">
        <field-validator type="requiredstring">
            <message>Debe introducir un nombre valido</message>
        </field-validator>
    </field>
    <field name="persona.padre">
        <field-validator type="requiredstring">
            <message>Debe introducir un nombre para el padre valido</message>
        </field-validator>
    </field>

I would like to use the same configuration for both validations, client based and server based but i don´t find anything in the docs about mixing converters and validation. I Can create a custom validator for my server side needs and use the last configuration file for my client side validation, but i would like a 100% declarative aproach.

Can anyone help me??

Best Regards, Raúl 
------------------------------------------------------------------
This e-mail and the documents attached are confidential and intended 
solely for the addressee; it may also be privileged. If you receive 
this e-mail in error, please notify the sender immediately and destroy it. 
As its integrity cannot be secured on the Internet, the Atos Origin 
group liability cannot be triggered for the message content. Although 
the sender endeavours to maintain a computer virus-free network, 
the sender does not warrant that this transmission is virus-free and 
will not be liable for any damages resulting from any virus transmitted. 

Este mensaje y los ficheros adjuntos pueden contener informacion confidencial 
destinada solamente a la(s) persona(s) mencionadas anteriormente 
pueden estar protegidos por secreto profesional. 
Si usted recibe este correo electronico por error, gracias por informar 
inmediatamente al remitente y destruir el mensaje. 
Al no estar asegurada la integridad de este mensaje sobre la red, Atos Origin 
no se hace responsable por su contenido. Su contenido no constituye ningun 
compromiso para el grupo Atos Origin, salvo ratificacion escrita por ambas partes. 
Aunque se esfuerza al maximo por mantener su red libre de virus, el emisor 
no puede garantizar nada al respecto y no sera responsable de cualesquiera 
danos que puedan resultar de una transmision de virus. 
------------------------------------------------------------------


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org