aspose file tools*
The moose likes Struts and the fly likes Validate() method not showing the validate messages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Validate() method not showing the validate messages" Watch "Validate() method not showing the validate messages" New topic
Author

Validate() method not showing the validate messages

Shanmugam Muthukumarasamy
Ranch Hand

Joined: Jan 11, 2003
Posts: 83
Hi All,

I need a help ,

Here is the situation. I have a form with 10 fields. All the 10 fields needs to be validated, when I leave from the form. But on the form load I have to validate only first 5 fields.

So What I did is, In the validation.xml, I defined the rules for the first 5 fields and it is getting validated fine. For the rest of the fields in the form bean I defined the validate() method as shown below:

public ActionErrors validate(
ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = super.validate(mapping, request);

AutoAppApplicationResources resource =
new AutoAppApplicationResources();
String save = resource.getString("button.save");
String cont = resource.getString("button.continue");
String select = resource.getString("button.select");

String src = this.getSource();
// Only validate on "save" button not others ("Continue" etc)

if (src.equals("NameInsuredDetails1") && (task.equals(save) | task.equals(cont) | task.equals(select))) {
//First Named Insured
//Other Named Insured
if(this.getPriority().equals("First")){
errors = super.validate(mapping, request);
if("".equals(this.getAddress().trim())){
//errors.add("Address is required", new ActionError(ActionErrors.GLOBAL_ERROR, new ActionError(resource.getString("namedInsured2.addr"))));
errors.add("Address is required", new ActionError("Address is required"));
}
/*if("".equals(this.getCity().trim())){
//errors.add("City is required", new ActionError("errors.required", resource.getString("namedInsured2.city")));
errors.add("City is required", new ActionError("City is required"));
}
if("".equals(this.getState().trim())){
errors.add("State is required", new ActionError("State is required"));
}
if("".equals(this.getZipcode().trim())){
errors.add("Zipcode is required", new ActionError("Zipcode is required"));
}*/
}
}
if(errors.isEmpty()) return null;
return errors;
}


As in the above code, I need to validate the last 5 fields only when the priority is "First".

But it never shows up the message "Address is required". Is there anything I am messing up here...

Thanks in Advance...
I would really appreciate your help...

Shanmugam


SJCP 1.4<br />SCWCD 1.4<br />SCEA
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Shouldn't

task.equals(save) | task.equals(cont) | task.equals(select)

be:

task.equals(save) || task.equals(cont) || task.equals(select)

?

If that isn't it, I'd run this through a debugger and see if the values this logic is based on are what you think they should be.


Merrill
Consultant, Sima Solutions
Shanmugam Muthukumarasamy
Ranch Hand

Joined: Jan 11, 2003
Posts: 83
Hi Merrill Higginson

Thanks for you reply...that is not a problem. I want all the conditions to be evaluated on that experssion. Even in the debug mode, I saw it it is going inside that if condition and adding error messages on erros. But still it is not showing up on the jsp screen.

Even I added <html:erros/> just before the <html:form>...Any Idea ?

Thanks,
Shanmugam
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
A few of points:

1) If you're using Struts Version 1.2 or above, you should be using ActionMessage instead of ActionError, as ActionError is deprecated.

2) Unless you're displaying errors for each field in separate locations, the first parameter for the constructor for ActionErrors should be ActionMessages.GLOBAL_MESSAGE.

3) The parameter passed to the constructor of ActionMessage or ActionError should be a message key found in the ApplicationResources.properties file (or the name you defined in struts-config.xml). It should not be just a String containing the text of the error message.

Here's an example. Make the following entry in ApplicationResources.properties:

error.stateRequired=State is required.

Then code the following in your validate() method.


errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionError("error.stateRequired"));
Shanmugam Muthukumarasamy
Ranch Hand

Joined: Jan 11, 2003
Posts: 83
Thanks again.

1. I am using Struts 1.1
2. My "namedInsured2.addr" key is defined in the ApplicationResources.properties
After your suggestion I changed my code as

errors = super.validate(mapping, request);
if("".equals(this.getAddress().trim())){
errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionError("namedInsured2.addr"));
//errors.add("Address is required", new ActionError("Address is required"));
}


Still, in the debug mode, I could see the message is getting added, but it is not showing up in the screen.


But the screen is not let me to go further, unless I enter the address value. It means, it is validating but it is not showing the message.
Thanks,
Shan.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Even I added <html:erros/> just before the <html:form>

You misspelled <html:errors> here. Did you misspell it in your JSP?

I'd also try putting the <html:errors/> tag inside <html:form></html:form>.

Also, if you're using Struts 1.1, use ActionErrors.GLOBAL_ERROR rather than ActionErrors.GLOBAL_MESSAGE.
[ January 27, 2006: Message edited by: Merrill Higginson ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Validate() method not showing the validate messages