Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

struts validator

 
Raj Ohadi
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
     
    Raj Ohadi
    Ranch Hand
    Posts: 316
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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 ?

    Thanks.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic