You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@struts.apache.org by "Martin Petzsch (JIRA)" <ji...@apache.org> on 2007/09/02 19:29:34 UTC

[jira] Issue Comment Edited: (WW-2112) @ConversionErrorFieldValidator and Collections not working

    [ https://issues.apache.org/struts/browse/WW-2112?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_42101 ] 

mpetzsch edited comment on WW-2112 at 9/2/07 10:28 AM:
-------------------------------------------------------------

This problem is being caused by the Validator not being able to pick up the conversion error from the map of conversion errors.  This is because of the full field path does not correctly have the parent visitorfieldvalidators prepended.  For example the error is at myBean.innerfield[0].validatedField  the validator is trying to find a type conversion in innerfield[0].validatedField.

Possible solution:
Alter com.opensymphony.xwork2.validator.validators.VisitorFieldValidator.AppendingValidatorContext to store the ValidatorContext parent passed into its constructor.

Alter the getFullFieldName method fhe same class as follows:
 public String getFullFieldName(String fieldName) {
        	if (parent instanceof AppendingValidatorContext) {
        		return parent.getFullFieldName("") + field + "." + fieldName;
        	}
            return field + "." + fieldName;
        }

So that it uses the method from its parent context - thus appending previous context names.

This appears to work currently - are there unit tests which i can use to confirm?

      was (Author: mpetzsch):
    This problem is being caused by the Validator not being able to pick up the conversion error from the map of conversion errors.  This is because of the full field path does not correctly have the parent visitorfieldvalidators prepended.  For example the error is at myBean.innerfield[0].validatedField  the validator is trying to find a type conversion in innerfield[0].validatedField.

Possible solution:
Alter com.opensymphony.xwork2.validator.validators.VisitorFieldValidator.AppendingValidatorContext to store the ValidatorContext parent passed into its constructor.

Alter the getFullFieldName method fhe same class as follows:
 public String getFullFieldName(String fieldName) {
        	if (parent instanceof AppendingValidatorContext) {
        		return parent.getFullFieldName("") + field + "." + fieldName;
        	}
            return field + "." + fieldName;
        }

So that it uses the method from its parent context - thus appending previous context names.
  
> @ConversionErrorFieldValidator and Collections not working
> ----------------------------------------------------------
>
>                 Key: WW-2112
>                 URL: https://issues.apache.org/struts/browse/WW-2112
>             Project: Struts 2
>          Issue Type: Bug
>          Components: Interceptors
>    Affects Versions: 2.0.9
>         Environment: WebSphere 6.1.0.9 on Windows
>            Reporter: Martin Petzsch
>             Fix For: 2.0.11
>
>
> @ConversionErrorFieldValidator does not work with property inside bean in collection.  Validation in the same location does work.
> A data bean which a jsp uses through an action.  This bean contains a date field. I annotate the set method with:
> 	@ConversionErrorFieldValidator(message="aDate: Conversion Error")
> 	@RequiredFieldValidator(message="aDate: Required Field")
> 	public void setADate(Date date) {
> 		aDate = date;
> 	}
> This works fine and I get the messages about type conversion and validation failure in the expected order depending on my input.
> The same data bean also contains a List of another data bean and the associated methods:
> 	private List<InnerBean> innerList;
> 	@VisitorFieldValidator(message="Inner Message:",appendPrefix=true)
> 	public List<InnerBean> getInnerList() {
> 		if (innerList == null) {
> 			innerList = new ArrayList<InnerBean>();
> 			innerList.add(new InnerBean());
> 		}
> 		return innerList;
> 	}
> 	public void setInnerList(List<InnerBean> innerList) {
> 		this.innerList = innerList;
> 	}
> My inner bean is as follows:
> 	private Date innerProp;
> 	public Date getInnerProp(){
> 		return innerProp;
> 	}
> 	
> 	@ConversionErrorFieldValidator(message="innerProp: Conversion Error")
> 	@RequiredFieldValidator(message="innerProp: Required Field")
> 	public void setInnerProp(Date str){
> 		innerProp = str;
> 	}
> The required field validator works as expected.  I do not however get any result EVER back from the ConversionErrorFieldValidator on the inner bean.  If I enter an incorrect value it simply does not get set.
> Source Code Follows
> ===============================================
> import java.util.ArrayList;
> import java.util.Date;
> import java.util.List;
> import com.opensymphony.xwork2.validator.annotations.ConversionErrorFieldValidator;
> import com.opensymphony.xwork2.validator.annotations.RequiredFieldValidator;
> import com.opensymphony.xwork2.validator.annotations.VisitorFieldValidator;
> public class MyDataBean {
> 	private Date aDate;
> 	private List<InnerBean> innerList;	
> 	
> 	public MyDataBean() {
> 	}
> 	@VisitorFieldValidator(message="Inner Message",appendPrefix=false)
> 	public List<InnerBean> getInnerList() {
> 		if (innerList == null) {
> 			innerList = new ArrayList<InnerBean>();
> 			innerList.add(new InnerBean());
> 		}
> 		return innerList;
> 	}
> 	public void setInnerList(List<InnerBean> innerList) {
> 		this.innerList = innerList;
> 	}
> 	public Date getADate() {
> 		return aDate;
> 	}
> 	// this works fine
> 	@ConversionErrorFieldValidator(message="aDate: Conversion Error")
> 	@RequiredFieldValidator(message="aDate: Required Field")
> 	public void setADate(Date date) {
> 		aDate = date;
> 	}
> }
> --------
> /**
>  * 
>  */
> import java.util.Date;
> import com.opensymphony.xwork2.validator.annotations.ConversionErrorFieldValidator;
> import com.opensymphony.xwork2.validator.annotations.RequiredFieldValidator;
> /**
>  * @author MPETZSCH
>  *
>  */
> public class InnerBean {
> 	private Date innerProp;
> 	
> 	public Date getInnerProp(){
> 		return innerProp;
> 	}
> 	
> 	// this does not work
> 	@ConversionErrorFieldValidator(message="innerProp: Conversion Error")
> 	@RequiredFieldValidator(message="innerProp: Required Field")
> 	public void setInnerProp(Date str){
> 		innerProp = str;
> 	}
> }

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.