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

Struts 2 Validation

Bejoy Kaladhar
Greenhorn

Joined: Jul 09, 2008
Posts: 9
Hello experts,
I'm am stuck with a issue in struts 2 validation can you please help me sort out this.I had implemented client side validation on struts 2 and i am successful in using validators as required,requiredString,stringLength but now facing 2 basic issues now

1.I'm not able to use the expression validator
2.i need to check first whether a text box is empty or not if empty it should give a validation message also if it contains any value other than integer then also it should give a message

My code snippets are here
Struts.xml


this is my validation xml file CarClubAction-carClubSave-validation.xml




also in my jsp page i've given all my controls within a forma tag as



Any idea what the problem could be?


Bejoy.K.S
Bejoy Kaladhar
Greenhorn

Joined: Jul 09, 2008
Posts: 9
Also i tried a few options for my second issue, using required and int

1.with and without short-circuit="true" in both the validators
2.using requiredString validator instead of required validator

unfortunately even that is not working.

With the expression validator i've tried a few options as




Even this is not making any difference.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

The expression validator is not supported on the client side; that would require a clinet-side version of OGNL and access to server-side domain objects. It wouldn't be impossible to implement, but it would take effort.

I have a patch going in to allow manually-configured custom client-side validation that you could use to accomplish this; follow the Struts-user mailing list to see when it goes in--I'm anticipating tomorrow, but it could be as late as Friday. The patch *should* work with earlier versions of Struts 2 as well; assuming it does I'll put it in both the S2.0 and S2.1 branches.

Your second issue is likely to be caused by providing useless parameters to the "int" field-validator; you're giving it "minLength" and "maxLength"--length has no meaning to an integer validation. You need to use the "min" and "max" parameters.
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 689

Hi,


You can use this for expression validation.


<field name="userId">
<field-validator type="stringlength">
<param name="minLength">3</param>
<param name="maxLength">50</param>
<message>
User ID must be between ${minLength} and ${maxLength}.
</message>
</field-validator>
<field-validator type="regex">
<param name="expression">^[0-9a-zA-Z\-._]*$</param>
<message>User Id contains only characters A-Z, a-z, 1-9, ., _, -.</message>
</field-validator>
</field>

For check equality user this :

<validator type="expression">
<param name="expression">
user.password eq confirmPassword
</param>
<message>Confirm password should be same as password.</message>
</validator>


Thanks, Nishan Patel
SCJP 1.5, SCWCD 1.5, OCPJWSD Java Developer,My Blog
Bejoy Kaladhar
Greenhorn

Joined: Jul 09, 2008
Posts: 9
hello guys,sorry for a late response.met with an accident tats why.
ok..Nishan i tried the expression validator the way you specified using equals(eq) but sory to ay that the validation is not being performed
i gave it as


the senario is that i'm having a select which contains a list of cities.The first element of the list is "Select City" . I need to validate whether a ciy has been selected or not i mean if the citySelect slect box is having the value "Select City" then the message should be displayed

Initially i tried the expression validaor as





but these 3 didn't work so i created text field like this within my form



and gave the validator expression as


Unfortunately even that is not working.any thoughts?
[ December 19, 2008: Message edited by: Bejoy KS ]
Bejoy Kaladhar
Greenhorn

Joined: Jul 09, 2008
Posts: 9
Hello David
It was a typing mistake form my side i had tried the int validation with parameters "min" and "max" only.It is not working with the short-circuit.any intelligent solutions?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Re: client-side expression validator: nothing you do will make client-side expression validation work. It's not supported.

Re: client-side "int" validation--what exactly isn't working?
Bejoy Kaladhar
Greenhorn

Joined: Jul 09, 2008
Posts: 9
hi,
I need to check whether a text field is empty or not.If not empty then i need to check whether that it is a number beteen 1 and 10.If empty it should show "Enter a value for field".if Any value other than nos beteen 1 and 10 is entered then it should show "Enter a value between 1 and 10".This is exactly how i need my validation to work. I tried out this as

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

When I "what exactly isn't working" I mean "describe the behavior you're seeing, and compare it to the behavior you're expecting".
Bejoy Kaladhar
Greenhorn

Joined: Jul 09, 2008
Posts: 9
ok David.Sorry for confusing you.
When i validate with the required validator alone i get the validation done
also the validation works fine when i use the int validator alone

But when both are used in the order i mentioned only the required validator works and the int validator doesn't.ie if i dont give any value then it prompts for a value but if i give a string value it accepts it rather than prompting for an int value.This is my short stopper right now.
deep kathuria
Greenhorn

Joined: Jun 13, 2011
Posts: 2
the correct way is this

use
<field-validator type="fieldexpression"> like this in your example


or

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struts 2 Validation