wood burning stoves 2.0*
The moose likes Struts and the fly likes Error messages are not being displayed while using validator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Error messages are not being displayed while using validator" Watch "Error messages are not being displayed while using validator" New topic
Author

Error messages are not being displayed while using validator

Meera Godse
Greenhorn

Joined: Jan 09, 2007
Posts: 28
hi,
I plan to use the struts validator for client & server side validations. I dont have much knowledge in validator, but i coded as per sample code and tutorial examples that i found on the web. But it doesnt seem to work. when i hit the submit key, the page reloads without any error messages. the execute method in my action is also being called. for eg in the below pasted code, the userid & password fields are "required". so if nothing is entered, it should display an err message & not go through the execute method. here are the steps i followed:

1. uncommented the plug-in tag in struts-config as:
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>

2. form bean and action declaration:
<form-bean name="loginForm" type="form.LoginForm" />
...
<action path="/LoginAdmin" type="action.SigninAction"
name="loginForm"
parameter="methodToCall"
scope="request"
input="/pages/AdminLogin.jsp"
validate="true">
<forward name="success" path="/pages/Homepage.jsp" />
<forward name="failure" path="/pages/AdminLogin.jsp" />
</action>

3. in my validation.xml file:
...
<form name="loginForm">
<field property="userid" depends="required">
<arg0 key="loginForm.userid"/>
</field>
<field property="password" depends="required">
<arg0 key="loginForm.password"/>
</field>
</form>

4. in message resources :
errors.required={0} is a mandatory field. Please enter a value.
loginForm.userid=User ID
loginForm.password=Password

5. in validation-rules.xml file :
...
<validator name="required"
classname="org.apache.struts.util.StrutsValidator"
method="validateRequired"
methodparams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionErrors,
javax.servlet.http.HttpServletRequest"
msg="errors.required"/>

6. the form bean LoginForm extends ValidatorForm. action does not contain a validate method.

7. JSP file:
<html:errors/>
<html:form method="post" action="/LoginAdmin?methodToCall=login"
onsubmit="return validateloginForm(this);">
<table border=0 width="100%">
<tr><td>User Id:</td><td><html:text property="userid"/></td></tr>
<tr><td>Password:</td><td><html:password
property="password"/></td></tr>
<tr><td><html:submit property="submit" value="Login" /></td></tr>
</table>
</html:form>

Please advice. any help is appreciated.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I'm not sure why your server-side validation is not working, but I can see why the client-side is not working. You need to add the following line to your JSP:


Also, one thing to check on your server-side validation: You've said your action does not have a validate method, but does your Actionform have one? If so, remove the method.
[ March 13, 2007: Message edited by: Merrill Higginson ]

Merrill
Consultant, Sima Solutions
Meera Godse
Greenhorn

Joined: Jan 09, 2007
Posts: 28
Oh, i am sorry. thats a typing mistake. I meant my form does not have a validate method. i also tried the javascript tag , but that too did not work.
So do you mean that the code that i have written is correct ? i wonder why it doesnt show the msgs then ??!! :-(
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Which version of Struts are you using? For the later versions, your should specify <arg position="0"> not <arg0> in your validation.xml file.
Meera Godse
Greenhorn

Joined: Jan 09, 2007
Posts: 28
i am using struts 1.3.5
is there any problem in the following:-

1. validation.xml: (dtd include stmt)
<!DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.3.0//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_1.dtd">

2. validation-rules.xml:
<!DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.3.0//EN"
"http://jakarta.apache.org/struts/dtds/validator-rules_1_1.dtd">

this maybe totally unrelated to the current problem, but i just thought of asking.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Originally posted by Meera Godse:
i am using struts 1.3.5
is there any problem in the following...

Yes, there's a big problem with it. It's the wrong version. When using Struts Validation framework it is imperative that all artifacts, including XML files and jar files be from the same version and copied from the same download file. Mix 'n match may be a good way to dress yourself, but it doesn't work when you're building a Struts application.

Another change in Struts 1.3.5 and above is that the validator-rules.xml file is now embedded in the struts-1.3.5.jar file. Therefore, your Validation plugin declaration should look like this:

As for the validation.xml file, copy the sample one from the struts-blank-1.3.5.war file that comes with the download, and then add your validations to it.
[ March 14, 2007: Message edited by: Merrill Higginson ]
Meera Godse
Greenhorn

Joined: Jan 09, 2007
Posts: 28
ok. i changed the plug-in declaration and now i am using all the files from struts 1.3.5.
it still doesnt show me the messages. validations seem to get bypassed & the action executes.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Did you change <arg0> to <arg position="0"> ? Spend some time studying the validation dtd to make sure that everything you put in the file coforms to the DTD. The URL at the top of the document is a real URL. You can use it in your browser to actually see the DTD.

If it still doesn't work, post your revised versions of everything you posted earlier.
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
I also scanned you posted code and nothing jumps out at me. Is this a new project or one that was migrated to Struts 1.3? Have you been able to get validation working for any other actions? Is the <arg position="0"> versus <arg0> issue that Merrill pointed out something that is important? Have you changed that?

Edited to add in this link...though it must be for Struts 1.1 or 1.2 and it seems like you have everything covered: http://www.oracle.com/technology/oramag/oracle/04-jan/o14dev_struts.html

- Brent
[ March 15, 2007: Message edited by: Brent Sterling ]
Meera Godse
Greenhorn

Joined: Jan 09, 2007
Posts: 28
Thank you Merrill for all the help. I greatly appreciate it. I had made 2 mistakes.
1. i was using my own validation-rules file that i had copied from the web instead of using the one inside struts jar. (i didnt know it comes with the jar)
2. my message resources file was placed under web-inf folder instead of web-inf/classes. I moved it to its right place & the msgs were displayed.

Brent, this is a project i am doing on my own from scratch just to learn struts. so it has always been struts 1.3.5 from start. I tried the code with both <arg0> & the position parameter & they both work.
The link you mentioned was quite informative. thank you.

So the only difference between client & server side validation is the use of html:errors & javascript tag, is it ? So do we use struts client side validations to avoid using javascript directly ?
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Originally posted by Meera Godse:
So the only difference between client & server side validation is the use of html:errors & javascript tag, is it ?

Actually, <html:errors> is used in server-side validation, not client-side. It is true that <html:javascript> enables client-side validation, but it still won't work without the required onsubmit event in the <html:form> tag.
Originally posted by Meera Godse:
So do we use struts client side validations to avoid using javascript directly ?

Yes. It's one way of doing client-side validation without having to write your own JavaScript validation code.
Meera Godse
Greenhorn

Joined: Jan 09, 2007
Posts: 28
For one form-bean, i forgot to change the 'extends' value from ActionForm to ValidatorForm. The client-side struts validations still seem to work ! the alert box pops up with the error message. Isnt it mandatory to use ValidatorForm (or ValidatorActionForm etc) for a form bean ?
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
It's required in order for server-side validation to work. Client-side validation will still work, even if your form bean doesn't extend ValidatorForm.
Meera Godse
Greenhorn

Joined: Jan 09, 2007
Posts: 28
oh got it.
I am a bit confused. If i use both the declarations in my jsp file (html:errors and javascript tag with onsubmit), which one will get evaluated ? Also, if there is a (server side)validation like say, check for the entered value to the one in database & then give message to user, how is that handled using validator ? how is a error message rendered in such a case ?
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Originally posted by Meera Godse:
If i use both the declarations in my jsp file (html:errors and javascript tag with onsubmit), which one will get evaluated ?

They will both get evaluated. The html:errors tag renders server-side errors (from the ActionErrors object) if they are present. If there are no server-side errors, nothing is displayed. It's important to understand that when client-side validation is enabled, it's always going to catch the error before server-side validation does. In this situation, server-side validation serves as a backup plan in case the client turns off JavaScript.
Originally posted by Meera Godse:
Also, if there is a (server side)validation like say, check for the entered value to the one in database & then give message to user, how is that handled using validator ? how is a error message rendered in such a case ?

The Struts Validation framework was never intended to handle all validations. It was designed only to handle simple existence or formatting validations. If you have business rules to apply, such as finding an account number in a database, you must write your own logic to do that in your Action class. If you find a validation error while appying business rules, you populate the ActionErrors object with an ActionMessage, call the saveErrors() method, and return a forward that goes back to the input page, where the error message will be displayed by the <html:errors /> tag.

See the headings "displaying message" and "displaying error messages" in this link.
[ March 15, 2007: Message edited by: Merrill Higginson ]
Meera Godse
Greenhorn

Joined: Jan 09, 2007
Posts: 28
thank you merrill. things are clear now.
 
jQuery in Action, 2nd edition
 
subject: Error messages are not being displayed while using validator
 
Similar Threads
Validation failure
Struts Validation Empty alert Box
validation
Validation framework is not working
regarding validations