Kinjal,
In my experience with dates and validation, the problem you have having is quite possibly when you are coping the date values out of the ActionForm if you are trying to convert the string to a date right there in the get method and the value is an empty string no null. Since the BeanUtils.popuplate will call a set method with "" instead of null. Here is a sample class that may help you track down the problem, basically I just check the value when the setter is called to make sure it isn't zero length string, if so I actually want the value to be null instead. Seeing your ActionForm in which this is happening would help, but here is a simple example of what may get you pasted the error.
public class Sample extends ValidatorForm /* or ValidatorActionForm */ { private String strExpiration;
public Date getExpiration() {
if(strExpiration != null) {
return Date.valueOf(strExpiration);
}
return null;
}
public void setExpiration(Date expiration) { if(expiration != null) { this.strExpiration = expiration.toString(); } }
public String getStrExpiration() { return strExpiration; }
public void setStrExpiration(String strExpiration) {
this.strExpiration = zeroLengthSet(strExpiration);
}
protected String zeroLengthSet(String str) {
if (s != null && s.length() > 0) {
return str.trim();
}
return null;
}
}
Good Hunting -daniel hopper
Kinjal Shares wrote:
Dear all,
I've been trying to get Dates validation for my Struts
project without any success. I'm using struts 1.2.4
with Validator 1.1.3 on tomcat 5.0.27.
In my application, I'm having a hier of MappingActionForm for Action, where individual action mappings in struts-config map to individual method in the Action class. The form is a child of ValidatorActionForm with a VO bean for holding business data and a string for holding the value for the date field. The VO bean contains a java.sql.Date field. In the getter and setter of the textual field, I get and set the data from VO bean, so as to have virtually a String-only ActionForm.
I'm pasting relevant codes below
struts-config.xml:
-------------------------------------------------
<action path="/inquiry/submitDetail" parameter="submitDetail" name="inquiryForm" scope="session" validate="true" input="/pages/inquiry/editDetail.jsp" type="foo.bar.struts.action.InquiryAction">
<forward name="previewDetail"
path="/pages/inquiry/previewDetail.jsp"/>
</action>
--------------------------------------------------
validation.xml: --------------------------------------------------
<form name="/inquiry/submitDetail">
<field property="offerEndDate" depends="required"
page="1">
<arg0 key="inquiry.form.header.offerEndDate"/>
</field>
</form>
--------------------------------------------------
editDetail.jsp: -------------------------------------------------- <html:form action="/inquiry/submitDetail"> <html:hidden property="page" value="1" /> <html:text name="inquiryForm" property="offerEndDate" /> .. .. </html:form> --------------------------------------------------
Now, everytime I submit the page and if the field is
empty it throws me an exception related to
beanUtils.populate(). According to my understanding
this type of exception should only come when struts
copying data into the ActionForm is problamatic.
However, it seems to me that the validator is somehow
bypassed.
If I remove the copying (to and from the VO bean into the textual date field) code from my ActionForm, it validates things properly.
I'm not sure what is going wrong here and hence I'm seeking the advise here. I've gone throgh all major mailing lists and forums for struts for any hints on this, but could find little. I'm sorry for such a long post, but I wanted include maximum data into the post.
Thanks and regards, Kinjal Sonpal
__________________________________
Do you Yahoo!?
Yahoo! Mail Address AutoComplete - You start. We finish.
http://promotions.yahoo.com/new_mail
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]