jQuery in Action, 2nd edition*
The moose likes Struts and the fly likes problem with validator framework Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "problem with validator framework" Watch "problem with validator framework" New topic
Author

problem with validator framework

Ja vardhan
Ranch Hand

Joined: May 09, 2005
Posts: 169
Hi,

I am using validator framework and implementing custom validations.

I have written the custom class:


validator-rules.xml contains this required stuff:
--------------------------------------------------------------------
<validator name="required"
classname="org.apache.struts.validator.FieldChecks"
method="validateRequired"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionMessages,
org.apache.commons.validator.Validator,
javax.servlet.http.HttpServletRequest"
msg="errors.required"/>

<validator name="verifypwd"
classname="com.CustomValidation"
method="validatePwd"
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.pwd"/>
---------------------------------------------------------------------

validation.xml contains this required stuff:
---------------------------------------------------------------------
<formset>
<form name="lookupForm">
<field
property="name"
depends="required">
<arg key="user.name"/>
</field>

<field
property="pwd"
depends="required,verifypwd">
<arg key="user.password" />
</field>
</form>
</formset>
---------------------------------------------------------------------

and this is my jsp


verifypwd functionality is not working at all.
When the call goes to my custom class 'CustomValidation.java' , its showing the NullPointerException at this particular line:
errors.add(field.getKey(),Resources.getActionError(request,action,field));


I came to know the problem is with the third parameter 'field' .

I ahve placed SOPs in my class (pls refer the java code above) and its getting printed at the server console:
request-->org.apache.coyote.tomcat5.CoyoteRequestFacade@1567e6c
action-->ValidatorAction: verifypwd
field--> key = pwd
property = pwd
indexedProperty = null
indexedListProperty = null

depends = required,verifypwd
page = 0
fieldOrder = 0
Vars:

java.lang.NullPointerException
at com.CustomValidation.validatePwd(CustomValidation.java:33)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)


Now indexedProperty and indexedListProperty are null, so I am getting NullPointerException.

I dont know where I need to specify about these two properties.

Please tell me what to do in this regard, where I need to declare these two properties ?

Thanks.

[ November 09, 2006: Message edited by: Ja vardhan ]
[ November 09, 2006: Message edited by: Ja vardhan ]

Javardhan:
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
What are indexedProperty and indexedListProperty? They are not in the code that you included.

- Brent
Ja vardhan
Ranch Hand

Joined: May 09, 2005
Posts: 169
Brent,

Thanks for your reply.

Thats what I want to know...where we have to include indexedProperty and indexedListProperty ???

Here http://mail-archives.apache.org/mod_mbox/struts-dev/200109.mbox/%3C20010925175914.97938.qmail@icarus.apache.org%3EI found that - in Field.java (hope its from struts API) they have declared these properties (assigned null initially) and having getters and setters.

Since these 'indexedProperty' and 'indexedListProperty' are defined just beside 'depends' attribute, I also defined them beside 'depends' attrubute in validation.xml like this :
--------------------------------------------------

<formset>

<form name="lookupForm">
<field
property="name"
depends="required">
<arg key="user.name"/>
</field>

<field
property="pwd"
depends="required,verifypwd"
indexedProperty="pwd"
indexedListProperty="pwd">

<arg key="user.password" />
</field>
</form>

</formset>
-----------------------------------------------------
now I am getting the following error while starting the Tomcat server itself.
INFO: Loading validation rules file from '/WEB-INF/validation.xml'
Nov 10, 2006 9:04:32 AM org.apache.commons.digester.Digester error
SEVERE: Parse Error at line 26 column 36: Attribute "indexedProperty" must be de
clared for element type "field".
org.xml.sax.SAXParseException: Attribute "indexedProperty" must be declared for
element type "field".
at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Un
known Source)
at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source
)


Afetr I invoke the application in browser, its showing the error:
Nov 10, 2006 9:08:58 AM org.apache.struts.validator.ValidatorForm validate
SEVERE: pwd[].pwd is not indexed
org.apache.commons.validator.ValidatorException: pwd[].pwd is not indexed
at org.apache.commons.validator.Field.getIndexedProperty(Field.java:800)

at org.apache.commons.validator.Field.validate(Field.java:891)


So I think 'indexedListProperty' is expecting some collection.

I dint understand what these two properties are ...

Please suggest me what I need to do.

Thanks.
[ November 09, 2006: Message edited by: Ja vardhan ]
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
I am not clear why you think that those properties are causing the problem. Have you stepped through the code in a debugger? Is "errors" null? If you look at that line of code what can cause a NullPointerException? "field.getKey()" might, but your debug statement shows that field is not null. The only other thing that could cause the exception is "errors.add()" if errors is null.

- Brent
[ November 10, 2006: Message edited by: Brent Sterling ]
Ja vardhan
Ranch Hand

Joined: May 09, 2005
Posts: 169
Brent,

You are right, 'errors' is null.

From validator-rules.xml I am passing:


But in my Java class (I have written this CustomValidation.java) it is coming as null.
what is the reason ? what I need to do?
Pls help me.

Thanks.
[ November 13, 2006: Message edited by: Ja vardhan ]
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
hmmm...I am not sure. Writing a custom validation method has been on my "to do" list for a while, but I have never written one. I doubt that new'ing off an ActionErrors instance at this point is what you need to do because that would not get passed back to the calling method. The examples that I have seen look like they expect a non-null errors parameter.

- Brent
Ja vardhan
Ranch Hand

Joined: May 09, 2005
Posts: 169
Thanks for your reply Brent.

I am getting NullPointerException while I am passing ActionErrrors. I tried with ActionMessages and its getting passed to my custom class perfectly, not getting any NullPointerException with this ActionMessages. But again the error messages are not getting displayed at the client side while I am using ActionMessages. I want to display error messages as pop-up alerts at client side, but its not happenig.

I think we have to use ActionErrors for that, but with ActionErros I am getting NullPointerException.

My ultimate aim is to display error messages as alerts at client side. I dont bother it is ActionErrors or ActionMessages.

Hope now you folks understood my problems in both the cases, Pls suggest me what to do.

Thanks.
[ November 13, 2006: Message edited by: Ja vardhan ]
Ja vardhan
Ranch Hand

Joined: May 09, 2005
Posts: 169
Hi Frends,

Please explain me why ActionErrors is becoming null in my CustomValidation.java ?

If I replace the the ActionErrors with ActionMessages, ActionMessages is not becoming as null in my CustomValidation.java

Please tell me whats the problem with ActionErrors.
Its very urgent for me.

Thanks.
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
Sorry, I cannot answer your question. The great thing about Struts is that all the source code is available to you. Have you looked at the Struts source code to see why this value might be null or have you looked at how any of the standard validations are implemented?

- Brent
Ja vardhan
Ranch Hand

Joined: May 09, 2005
Posts: 169
Brent,

I have looked at some sample applications, some applications using ActionErros and some using ActionMessages. I dint understand exactly what is the specific purpose of these two classes.

Any suggestions are welcome..

Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem with validator framework