Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Struts server side validation problem

 
Andrey Levin
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 471
Hibernate Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Alaa Nassef
Ranch Hand
Posts: 471
Hibernate Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you added your validation logic into ActionForm class?
 
chandra mohan
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 126
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>
 
Andrey Levin
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic