You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Julio Alberto Jalón <be...@yahoo.es> on 2008/10/15 13:32:05 UTC

Struts2 Collection validation

I'm learning struts2 through the struts2-showcase application example. From the "Person Manager" functionality I've learned how to send multiples values from client to server using collections to manage these values, but now I'd like to know how to validate these values using struts2 validation. I don't know what to put in *-validation.xml in order to validate these values. Should I use custom validation?

Thanks in advance.



      

Re: Struts2 Collection validation

Posted by fgsantoyo <fg...@gmail.com>.
If you want  to validate generated fields, i mean a Collection you can do
something like this.

* Action class
private List collection;

	public void setCollection(String collection) {
	    this.collection = collection;
	}

	public String getCollection() {
	    return collection;
	}

 List list = this.getCollection() ; 
	    for (int i = 0; i < list.size(); i++) {
		if(list.get(i).equals(""))
		    addFieldError("collection[" + i + " ]
",getText("validation.required")); 


	    }
by fgsantoyo

Julio Alberto Jalón wrote:
> 
> 
> I'm learning struts2 through the struts2-showcase application example.
> From the "Person Manager" functionality I've learned how to send multiples
> values from client to server using collections to manage these values, but
> now I'd like to know how to validate these values using struts2
> validation. I don't know what to put in *-validation.xml in order to
> validate these values. Should I use custom validation?
> 
> Thanks in advance.
> 
> 
> 
>    
> 

-- 
View this message in context: http://www.nabble.com/Struts2-Collection-validation-tp19991675p20255180.html
Sent from the Struts - User mailing list archive at Nabble.com.


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


Re: Struts2 Collection validation

Posted by harpreet <ha...@bankofamerica.com>.
Hi,
I am new to the Struts2 and facing the similar issue , 
u said u have managed to get the data from the dynmically  generated fields
to the action , can u share how it can be done.......a sample will be of
great help.......
my requirment is my form is dynamically generat the fileds for name date and
amount and it could raneg from one to man fields how would i get the details
of the form data in my actions .... bcoz i am not sure how many  fields will
be filled out .....


Thanks you 
java.harpreet@gmail.com


Julio Alberto Jalón wrote:
> 
> 
> May I didn't explain myself correctly. What I would like to know is how to
> validate multiple fields.  I'll write an example. I have a form in a JSP
> which generate the field dynamically. E.g: 
> 
> <s:form action="editPerson" theme="simple" validate="false">
>     <table>
>         <tr>
>             <th>ID</th>
>             <th>First Name</th>
>             <th>Last Name</th>
>         </tr>
>         <s:iterator id="p" value="persons">
>             <tr>
>                 <td>
>                     <s:property value="%{id}" />
>                 </td>
>                 <td>
>                     <s:textfield label="First Name"
> name="persons(%{id}).name" value="%{name}" theme="simple" />
>                 </td>
>                 <td>
>                     <s:textfield label="Last Name"
> name="persons(%{id}).lastName" value="%{lastName}" theme="simple"/>
>                 </td>
>             </tr>
>         </s:iterator>
>     </table>
> 
>     <s:submit method="save" value="Save all persons"/>
> </s:form>
> 
> 
> 
> Where person is a List in the action class. Now, what I would like to know
> is how could I achieve this fields to be validated. For example, I would
> like that if the name is filled the last name should be also filled and
> both can only contains letters and blanks. 
> 
> Well, I don't want you to give me the complete solution ( the expresion or
> reg expresion), just want to know how I can refer all these fields from
> the MyActionClass-validation.xml. 
> 
> Hope I've explained better than in the first post.
> 
> Please, help me.
> 
> Thanks in advance.
> 
> 
> ----- Mensaje original ----
> De: Julio Alberto Jalón <be...@yahoo.es>
> Para: user@struts.apache.org
> Enviado: miércoles, 15 de octubre, 2008 12:32:05
> Asunto: Struts2 Collection validation
> 
> 
> I'm learning struts2 through the struts2-showcase application example.
> From the "Person Manager" functionality I've learned how to send multiples
> values from client to server using collections to manage these values, but
> now I'd like to know how to validate these values using struts2
> validation. I don't know what to put in *-validation.xml in order to
> validate these values. Should I use custom validation?
> 
> Thanks in advance.
> 
> 
> 
> 

-- 
View this message in context: http://old.nabble.com/Struts2-Collection-validation-tp19991675p28674052.html
Sent from the Struts - User mailing list archive at Nabble.com.


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


RE: Struts2 Collection validation

Posted by Néstor Boscán <ne...@gmail.com>.
>From what I have investigated 6 months ago there is no validation for collections. I think I even tested that you can create validators for employees[0].firstName, employees[1].lastName, but not something that will apply to all indexes. I even posted if it was possible to add expressions to the fieldname so I can apply a validator form employees[.*].firstName. For my projects this is the most required missing functionality of Struts 2.

Regards,

Néstor Boscán

-----Mensaje original-----
De: gaelle [mailto:gaelle.hignette@gmail.com] 
Enviado el: Monday, October 20, 2008 9:59 AM
Para: user@struts.apache.org
Asunto: Re: Struts2 Collection validation


I hope there exists a simpler solution, but here is my workaround: use an
expression validator.
This idea was taken from 
http://www.opensymphony.com/webwork/wikidocs/collection%20validator.html
webwork wiki: collection validator  (it seems that the collection validator
is not yet available in struts).

For example, if you want to say that lastName is a requiredString, then you
can create an expression validator like this:

<validator type="expression">
   persons.{#this.lastName.length() > 0}.{? #this == false }.size() &lt;= 0
   <message>lastName cannot be empty</message>
 </validator>

which means: the size of the sub-collection of persons that does not satisfy
the condition "lastName.length>0"  must be <=0.



Julio Alberto Jalón wrote:
> 
> 
> May I didn't explain myself correctly. What I would like to know is how to
> validate multiple fields.  I'll write an example. I have a form in a JSP
> which generate the field dynamically. E.g: 
> 
> <s:form action="editPerson" theme="simple" validate="false">
>     <table>
>         <tr>
>             <th>ID</th>
>             <th>First Name</th>
>             <th>Last Name</th>
>         </tr>
>         <s:iterator id="p" value="persons">
>             <tr>
>                 <td>
>                     <s:property value="%{id}" />
>                 </td>
>                 <td>
>                     <s:textfield label="First Name"
> name="persons(%{id}).name" value="%{name}" theme="simple" />
>                 </td>
>                 <td>
>                     <s:textfield label="Last Name"
> name="persons(%{id}).lastName" value="%{lastName}" theme="simple"/>
>                 </td>
>             </tr>
>         </s:iterator>
>     </table>
> 
>     <s:submit method="save" value="Save all persons"/>
> </s:form>
> 
> 
> 
> Where person is a List in the action class. Now, what I would like to know
> is how could I achieve this fields to be validated. For example, I would
> like that if the name is filled the last name should be also filled and
> both can only contains letters and blanks. 
> 
> 

-- 
View this message in context: http://www.nabble.com/Struts2-Collection-validation-tp19991675p20068978.html
Sent from the Struts - User mailing list archive at Nabble.com.


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


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


Re: Struts2 Collection validation

Posted by gaelle <ga...@gmail.com>.
I hope there exists a simpler solution, but here is my workaround: use an
expression validator.
This idea was taken from 
http://www.opensymphony.com/webwork/wikidocs/collection%20validator.html
webwork wiki: collection validator  (it seems that the collection validator
is not yet available in struts).

For example, if you want to say that lastName is a requiredString, then you
can create an expression validator like this:

<validator type="expression">
   persons.{#this.lastName.length() > 0}.{? #this == false }.size() &lt;= 0
   <message>lastName cannot be empty</message>
 </validator>

which means: the size of the sub-collection of persons that does not satisfy
the condition "lastName.length>0"  must be <=0.



Julio Alberto Jalón wrote:
> 
> 
> May I didn't explain myself correctly. What I would like to know is how to
> validate multiple fields.  I'll write an example. I have a form in a JSP
> which generate the field dynamically. E.g: 
> 
> <s:form action="editPerson" theme="simple" validate="false">
>     <table>
>         <tr>
>             <th>ID</th>
>             <th>First Name</th>
>             <th>Last Name</th>
>         </tr>
>         <s:iterator id="p" value="persons">
>             <tr>
>                 <td>
>                     <s:property value="%{id}" />
>                 </td>
>                 <td>
>                     <s:textfield label="First Name"
> name="persons(%{id}).name" value="%{name}" theme="simple" />
>                 </td>
>                 <td>
>                     <s:textfield label="Last Name"
> name="persons(%{id}).lastName" value="%{lastName}" theme="simple"/>
>                 </td>
>             </tr>
>         </s:iterator>
>     </table>
> 
>     <s:submit method="save" value="Save all persons"/>
> </s:form>
> 
> 
> 
> Where person is a List in the action class. Now, what I would like to know
> is how could I achieve this fields to be validated. For example, I would
> like that if the name is filled the last name should be also filled and
> both can only contains letters and blanks. 
> 
> 

-- 
View this message in context: http://www.nabble.com/Struts2-Collection-validation-tp19991675p20068978.html
Sent from the Struts - User mailing list archive at Nabble.com.


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