This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Struts and the fly likes Struts server side validation problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts server side validation problem" Watch "Struts server side validation problem" New topic
Author

Struts server side validation problem

Andrey Levin
Greenhorn

Joined: May 01, 2008
Posts: 7
Hi, all.
I need to validate couple fields, using struts validation. It works for client side (except 'validwhen'), but I need server side.
Please, help! Below is my code:

validation.xml:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.3.0//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_3_0.dtd">
<!-- delete this comment 2 -->
<form-validation>
<formset>
<form name="/doAdd">
<field property="originZip" depends="integer">
<arg key="originZip" />
</field>
<field property="originZipFrom" depends="validwhen">
<arg key="originZipFrom" resource="false" />
<var>
<var-name>test</var-name>
<var-value>
(((*this* != null) and (originZip != null)) and (originZip < *this* ))
</var-value>
</var>
</field>
</form>
</formset>
</form-validation>

add.jsp:
<html:javascript formName='predictISCForm'/>
<html:form action="/doAdd">
<html:errors property="error" />
<h4 style="color: red"><html:messages id="messages"
message="true">
<bean:write name="messages" />
</html:messages></h4>

Thanks you.
Alaa Nassef
Ranch Hand

Joined: Jan 28, 2008
Posts: 460
In the html:form, add an onsubmit attribute for validation to be something like this

You just replace FormName to your form's name in struts config.


Visit my blog: http://jnassef.blogspot.com/
Alaa Nassef
Ranch Hand

Joined: Jan 28, 2008
Posts: 460
OH, sorry, the first time I read your question, I thought you said you DON'T want server side validation. Ok, for server side validation, in the action definition (the one you're submitting to, "/doAdd" in your case), add two attributes:
validate=true
input=[page or action you want the action to forward to in case of validation error]
Andrey Levin
Greenhorn

Joined: May 01, 2008
Posts: 7
I did it , but it still doesn't work. here is what I have in struts-config.xml:
<action path="/doAdd" name="formName" scope="request" validate="true" forward="predict.add" input="predict.add"></action>

Can it happen because I use tiles? Or becouse 'forward' and 'input' have the same destionation?

Thank.
Vishal Matere
Ranch Hand

Joined: Jan 22, 2008
Posts: 81
Have you added your validation logic into ActionForm class?


SCJP <br />SCWCD <br />SCBCD <br />SCEA-1
chandra mohan
Greenhorn

Joined: May 05, 2008
Posts: 20
Originally posted by Vishal Matere:
Have you added your validation logic into ActionForm class?


I am adding to vishal's point. Check whether you have added validate method in your form and post that method.
Andrey Levin
Greenhorn

Joined: May 01, 2008
Posts: 7
No, I didn't. I don't use custom validation:
<form-bean name="formName" type="org.apache.struts.validator.DynaValidatorActionForm">
<form-property name="dfdfd" type="java.lang.String">
</form-property>
</form-bean>

I tried to extend DynaValidatorActionForm and put sysout inside of validate().
It printed sysout, but didn't return any messages or stoped form from submiting.
Vishal Matere
Ranch Hand

Joined: Jan 22, 2008
Posts: 81
Originally posted by Andrey Levin:
I tried to extend DynaValidatorActionForm and put sysout inside of validate().
It printed sysout, but didn't return any messages or stoped form from submiting.


You need to write logic for validation in validate(ActionMapping mapping, HttpServletRequest request)
e.g.


If your validate method doesnt return anything, validation is said to be successfull and form is submitted.

HTH
V
Anderson Fonseca
Ranch Hand

Joined: Feb 05, 2004
Posts: 126
try to put your action mapping this way:

<action path="/crudXxxxxVal" name="xxxxxForm"
type="br.org.XxxxxxAction"
parameter="metodo"
scope="request"
validate="true"
input="formulario">
<forward name="formulario"
path="tiles.path" />
</action>


Anderson Fonseca :: Brazil<br />SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCDJWS, SCEA(I), SCEA 5 (I,II,III)
Andrey Levin
Greenhorn

Joined: May 01, 2008
Posts: 7
I tried this:
public class MyForm extends DynaValidatorActionForm {
public ActionErrors validate(ActionMapping arg0, HttpServletRequest arg1) {
ActionErrors errors = new ActionErrors();
if ("".length() < 6) {
errors.add(ActionErrors.GLOBAL_MESSAGE, new ActionMessage("errors.username.invalid"));
}
return super.validate(arg0, arg1);
}

}
I debug and step into it. Message was added. "errors.username.invalid" message is not exist in my resource file, but it even didn't give me warning about it!
The only situation when i can display message, if it is set inside of action class.
Andrey Levin
Greenhorn

Joined: May 01, 2008
Posts: 7
Also, here is part of my .jsp:
<html:errors property="error" />
<h4 style="color: red"><html:messages id="messages"
message="true">
<bean:write name="messages" />
<br>
</html:messages></h4>
Vishal Matere
Ranch Hand

Joined: Jan 22, 2008
Posts: 81
return super.validate(arg0, arg1);
do you see any problem here.

I am sure you can now show error message. I gave you all hints...

HTH
V
chandra mohan
Greenhorn

Joined: May 05, 2008
Posts: 20
Andrey,
Change validate method like this

add message in your properties file and use that key for creating action message

I hope this solves your problem
[ May 10, 2008: Message edited by: chandra mohan ]
Andrey Levin
Greenhorn

Joined: May 01, 2008
Posts: 7
Thank you everyone for your time you spent trying to resolve my issue. I wasn't able to resolve it , but I've run out of time to work on it.
So, I moved my validation to my action. Again, I really appreciate your help.Thanks.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Struts server side validation problem
 
Similar Threads
why no errors shown for password validation in struts 1.2.4
Error logging in Client side validation
custom validator and custom tags
doubt in using validwhen
Help with validation rules for form with multiple submit buttons