File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes struts validator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "struts validator" Watch "struts validator" New topic

struts validator

Raj Ohadi
Ranch Hand

Joined: Jun 30, 2006
Posts: 316
I am new to struts. I download some sample code and I got lost when reading it. It seems none of the form extends ValidatorForm, they all extend ActionForm. So it appears to me this application is not using struts validator, right ? But I found the code includes validation.xml and validation-rules.xml files. So is it possible for a struts application use struts validator without extending ValidationForm for its form beans ?

Second question is -- is struts validator a server side validator or client side ? For example, if I use struts validator, wehn I enter some value in a text field, will the validator only work AFTER I click the submit button ? If it is javascript it can validate te input before I hit the submit button, right ?
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
There are two ways of validating in Struts:
  • Extending ActionForm and overriding the validate method with your own validation code
  • Overriding ValidatorForm and defining your validations in the validator.xml file
  • So, in answer to your question, it is possible to validate in Struts without extending ValidatorForm, but only if you write code, rather than using the validator.xml fil.

    Regarding your second question, the Struts validator framework will do client-side and server-side validation. However your definition of client-side validation is not quite correct. Client-side validation is any validation done by the client or browser, regardless of whether it's done before or after the submit button is pressed. Struts client-side validation is done after the submit button is pressed, but it is done using JavaScript on the browser. This means that JavaScript code is checking for errors and will return an error message if necessary without ever submitting the form to the server. It does this by intercepting the submit action and stopping it if there's an error.

    Consultant, Sima Solutions
    Raj Ohadi
    Ranch Hand

    Joined: Jun 30, 2006
    Posts: 316
    Hi Merrill,

    Appreciate your response. Just have few more questions to confirm--

    1. could you elaborate how do I specify client side or server side validation in struts ? is it a simple flag ? If I do client side struts validation, do I need to write javascript separately by myself or does struts automatically generate it (assuming I am using struts 1 still) ?

    2. regardless of client or server side struts validation, as long as it is struts validation, the validation only happens after you click the submit button, correct (the difference is for client it may intercept the form before sending to server, and server side validation is done after it is sent to server) ?

    3. You said if the form extends ActionForm instead of ValidationForm, then the validation-rules.xml and validation.xml are useless, and the only validation logic should be in the form class ?

    4. I notice that in some form, after I filled in some text in a text field and moved to the next field, sometimes it immediately pops up an alert telling me I entered wrong format for last text field. At that point I hvaen't clicked submit button yet. For such validation, can I say this is not struts validation, it may be java script validation, correct ?

    I agree. Here's the link:
    subject: struts validator
    It's not a secret anymore!