aspose file tools*
The moose likes Struts and the fly likes Error logging in Client side validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Error logging in Client side validation" Watch "Error logging in Client side validation" New topic
Author

Error logging in Client side validation

Abhishek Dwaraki
Ranch Hand

Joined: Feb 13, 2006
Posts: 44
I'm using the Struts Validator Plugin to validate data on the client side without hitting the server with a request to validate. The problem now is with the error logging. Suppose I use <html:errors /> in my jsp, it results in a return to the server to log the error. If I use ActionErrors in my Action class, the result is again the same. Is there any way to display an error so that the control does not go back to the server and the errors can be coded into the JavaScript that is generated by the Validator itself? Is it possible to display errors using the dynamic JavaScript....? Plz advice in this regard... Thanks in advance. I'm using Struts 1.2.7 for my requirements.


Regards,
Abhishek Dwaraki
Dept of Electrical and Comp Engg
University of Massachusetts, Amherst
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I'm not entirely sure I understand your question, but here's some information that may help:

When you use the Struts validation framework, Struts generates both client-side and server-side code to handle the validation. If the error is caught on the client-side through the generated JavaScript code, the error is displayed in a JavaScript alert dialog box, and the form is not submitted and no request is made to the server.

If the client-side validation passes, the form is submitted and the server-side validation is also executed, but in this case it's redundant. The server-side validation exists as a kind of backup plan in case the user has turned off JavaScript on the browser. It is only server-side validation that displays errors using the <html:errors /> tag. This tag is not used at all by client-side validation.


Merrill
Consultant, Sima Solutions
Abhishek Dwaraki
Ranch Hand

Joined: Feb 13, 2006
Posts: 44
Dear Merrill,
Thanks for that tip... But the problem happening here is I'm unable to figure out whethet the problem is with the server or client side validation because of the fact that the alert box that displays the error msg is totally blank and I'm not able to see the message at all... I'm not able to see the OK on the dialog box's button also.... That is the problem about not being able to decipher the error message. Plz do advice me in this regard.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
The way to tell the difference between a client-side error message and a server-side message is quite simple: A client-side message will always show in an alert box, while a server-side message will always show as text in the place of the <html:errors /> or <html:messages/> tag in your JSP.

As far as the message showing up blank, I'm not really sure what may be causing that. Even if the message is blank, the "OK" button should always show. You may want to play around with your browser settings to see if that makes a difference. I'd also run a test, such as putting on load="alert('This is a test')" inside your <body tag>. If this alert box doesn't show up properly, then it must be something in your browser settings.
Abhishek Dwaraki
Ranch Hand

Joined: Feb 13, 2006
Posts: 44
Thanks for that Merrill...
There was some problem with the browser settings after all. Regarding the message, there seems to be a problem with the mapping because, as of now, a message is getting displayed, but the problem is that the value of the key of the properties file being passed is not being replaced as the placeholder value. I get a msg saying 'null is required' where null is the {0} placeholder in the error msg in the Struts config thingy of my ApplicationResources.properties file. Pls do advice in this regard. Thanks in advance.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Are you specifying <arg-key> in your validation definition in the validation.xml file?

Here's an example:
Abhishek Dwaraki
Ranch Hand

Joined: Feb 13, 2006
Posts: 44
Here's my validation.xml file.... Hope this can do something to help tou figure out the problem... Plz do advice...

<!DOCTYPE form-validation
PUBLIC "-//Apache Software Foundation//
DTD Commons Validator Rules
Configuration 1.2//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_2_0.dtd">

<form-validation>
<formset>

<form name="AddressForm">
<field property="name" depends="required">
<arg position="0" key="Name" resource="false" />
</field>
<field property="address" depends="required">
<arg position="0" key="AddressForm.address" bundle="/com.jpmchase.mm.struts.ApplicationResources" />
</field>
<field property="emailAddress" depends="required">
<arg position="0" key="AddressForm.emailAddress" bundle="/com.jpmchase.mm.struts.ApplicationResources" />
</field>
</form>

<form name="StrutsValidationForm">
<field property="username" depends="required">
<arg position="0" key="StrutsValidationForm.name" resource="true" />
</field>
<field property="password" depends="required,minlength">
<arg position="0" key="StrutsValidationForm.password" bundle="/ApplicationResources.properties" resource="true" />
<arg position="1" name="minlength" key="${var:minlength}" />
<var>
<var-name>minlength</var-name>
<var-value>6</var-value>
</var>
</field>
<field property="testRangeValue" depends="required,intRange">
<arg position="0" key="StrutsValidationForm.testRangeValue" bundle="/ApplicationResources.properties" resource="true" />
<arg position="1" name="intRange" key="${var:min}" bundle="/ApplicationResources.properties" resource="true" />
<arg position="2" name="intRange" key="${var:max}" bundle="/ApplicationResources.properties" resource="true" />
<var>
<var-name>min</var-name>
<var-value>18</var-value>
</var>
<var>
<var-name>max</var-name>
<var-value>65</var-value>
</var>
</field>
</form>

</formset>
</form-validation>
Abhishek Dwaraki
Ranch Hand

Joined: Feb 13, 2006
Posts: 44
With reference to the above post, both the JSP's are giving the same problem of not displaying the error msg... It does not pertain or restrict to one set of JSP's and Action classes.. Thanks in advance...
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Do you have entries in your ApplicationResources.properties file that match these names?

Given the validation.xml file you showed us, you should have:

Name=Name
AddressForm.address=Address
AddressForm.emailAddress=Email Address
StrutsValidationForm.name=User Name
StrutsValidationForm.password=Password


One more thing: Unless you're using different resource bundles in the same application, remove the bundle attribute from all your definitions. This will cause Struts to use the default bundle that you defined in struts-config.xml
[ March 21, 2006: Message edited by: Merrill Higginson ]
Abdul Gafoor
Greenhorn

Joined: Nov 22, 2005
Posts: 11
Client Side Validation You must add this Line is Correctly in jsp page
<form action="log.do" onsubmit="return validateLogForm(this);" >


<html:javascript formName="logForm">
This is code For Client Side Validation
If you want server side validation you disable the <html:javascript> tag

Then you must add <html:errors/>
or <logic:messagesPresent>
<html:messages id="error">
<bean:write name="error" />
</html:messages>
</logic:messagesPresent>
that's all for Validation.
If you are getting blank messages in alert box.
If you want this message
for example User Name is Required.
User name is from Validation.xml
is Required is from MessageProperties (# Errors
errors.required= is required
errors.email= is email is not valid.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error logging in Client side validation