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

validation

Denis Mi
Greenhorn

Joined: Mar 02, 2006
Posts: 9
I am trying to use Struts validation but I cannot get it to work.
My configuration files are as follows:

struts-config

<form-bean name="profileForm" type="com.sample.form.RegistrationForm"/>

<action path="/profile" type="com.sample.RegistrationAction" name="profileForm" scope="request" validate="true" input="/profile/consumer/register.jsp" parameter="dispatch">
<forward name="register" path=".view.registration"/>
</action>


<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>


validation

<formset>
<form name="/profileForm">
<field property="username" depends="required" />
</form>
</formset>


The problem is that when I try to validate nothing happens. Is there anyway for me to find out if validator plugin was loaded and applied during request?
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Does the form bean defined as "profileForm" extend org.apache.struts.validator.ValidatorForm? If not, this could be the reason it doesn't work.

Is there anyway for me to find out if validator plugin was loaded and applied during request?


When the application initializes, you should see some messages in your SystemOut log that indicate whether the validator initialized properly or not.


Merrill
Consultant, Sima Solutions
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
Does RegistrationForm extend ValidatorForm?

- Brent
Denis Mi
Greenhorn

Joined: Mar 02, 2006
Posts: 9
It extends

org.apache.struts.validator.ValidatorActionForm

I was looking at the log file and couldn't find anything about validators.

We are running WebLogic through Eclipse. Can you tell me which objects are beeing called during request that have to do with validation? Maybe I could trace the call to validation plugin/objects using debug mode.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
If you want to trace the execution of the validation logic, I'd suggest downloading the source code for the version of Struts that you're using. In Eclipse, you can then attach this source code to the struts.jar file and use the debugger to follow the execution of the code. The method to look at is the validate() method in your form bean's superclass (ValidatorActionForm).

One other thing to consider: Did you include an

<html:errors />

Tag in your JSP to show the error should one occur?
[ March 02, 2006: Message edited by: Merrill Higginson ]
Denis Mi
Greenhorn

Joined: Mar 02, 2006
Posts: 9
I did just that, downloaded Struts source code but I don't know where to set the breakpoint. I didn't include <html:errors /> in my page because when I make the request it forwards not to error page but to resource specified in action.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
The method to look at is the validate() method in your form bean's superclass (ValidatorActionForm).
Denis Mi
Greenhorn

Joined: Mar 02, 2006
Posts: 9
I tried putting breakpoint in validate() but it did nothing.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Did you override the validate method in RegistrationForm? If you did so without calling super.validate(), that's the problem.

It's the superclass validate() method that does the validating, not your validate() method.
[ March 02, 2006: Message edited by: Merrill Higginson ]
Denis Mi
Greenhorn

Joined: Mar 02, 2006
Posts: 9
no I didn't override it
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Show us your JSP. If you set a breakpoint in validate() and the code never got there, it may be that Struts is not associating the action you specified in the <html:form> tag with the correct form bean.
[ March 02, 2006: Message edited by: Merrill Higginson ]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I'm also looking at your validation.xml entry, and it doesn't look right to mw:

try this:


There should be no slash in front of the form, and you need an <arg> entry.

You must also have an entry in ApplicationResources.properties something like:

profileForm.username=User Name must not be blank
Denis Mi
Greenhorn

Joined: Mar 02, 2006
Posts: 9
I changed validation definition to this

<formset>
<form name="profileForm">
<field property="username" depends="required">
<arg0 key="errors.username.is.required" />
</field>
</form>
</formset>


My JSP file:






<html:form action="/profile">


<table style="font-size: 12px; background-color: white;">

<html:errors />
<logic:messagesPresent property="errors.username.is.required">
<tr>
<td colspan="2" style="color: red;"><fmt:message key="errors.username.is.required"/></td>
</tr>
</ logic:messagesPresent>

<tr>
<td><fmt:message key="user.usernamewithmin"/> <span style="color: red; font-weight: bold;">*</span></td>
<td><html:text styleId="username" property="username" size="20" maxlength="20" /></td>
</tr>

<tr>
<td colspan="2">
<input type="submit">
</td>
</tr>
</table>

</html:form>
Denis Mi
Greenhorn

Joined: Mar 02, 2006
Posts: 9
Forgot, with all changes request still redirects to success page instead of error page
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Here's a small test application that I've tested and know for sure that it works.

Maybe if you compare your application to this, you'll be able to find what's wrong.

struts-config.xml


validation.xml

ApplicationResources.properties


LoginForm

login.jsp

[ March 03, 2006: Message edited by: Merrill Higginson ]
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
ValidatorActionForm you say...in that case I think that your validation definition has to use the path of your action (with ValidatorForm you use the name of the form). I have to admit to never using ValidatorActionForm, but I think your validation.xml entry should look like this:

<form name="/profile">

- Brent
Dhana Sen
Ranch Hand

Joined: Mar 05, 2006
Posts: 33
for validation
1. check the validation plugin - struts config (U 'r right)
2. check whether u have menction formbean in - validation.xml (U 'r right)i mean /profileForm
3. if u'r using validation() method inside Formbean class.. U want to call super.validate().. then only it will refer plugin / else it think that u'r using Error Management .. (inside u'r form class/Action class)

both plugin and error management will work for u'r validation (i mean verifying u'r jsp fields)

4. if u feel that u'r not using Error management u better remove that method.. and it will work...

if any reply to the post..

by
dhana



that U will find the

all u'r code are correct only thing u have missed is
Hariharan.S
Greenhorn

Joined: May 25, 2005
Posts: 7
hi dhana


i have tried the same , as u specified in the code (sruts validation)
I am getting problem in the login.jsp i.e validateLoginForm is not defined.
But i am getting the err msgs like 'Enter your Name" when i gave html error tag. I want to display the msgs in a msgbox. What could be wrong?
Denis Mi
Greenhorn

Joined: Mar 02, 2006
Posts: 9
Hi guys,
Thank you for all your help I got validation working. The problem was that I used form name when defining validation but should have used action path.

<formset>
<form name="/profileForm">
...
</form>
</formset>

Should be

<formset>
<form name="/profile">
...
</form>
</formset>
Denis Mi
Greenhorn

Joined: Mar 02, 2006
Posts: 9
But now I got new problem. I need to do client side validation. But I found that there is a JavaScript bug

To define rules of validation Struts creates functions likes these

function profileForm_required () {
this.a0 = new Array("username", "Username required is required.", new Function ("varName", " return this[varName];"));
}

This function is being called from appropriate validation function like this

oRequired = evl('new ' + formName.value + '_required()');

for (x in oRequired) {

The problem is that in this case even though you would expect this loop to iterate only once it is being iterated twice. First time value of x is 'a0' and second is 'extend'. 'extend' has to do with Javascript object prototyping. This problem appears both in IE and Firefox. I tried searching struts page but did find anything useful.


My doctype is

!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: validation