Results 1 to 7 of 7

Thread: commons client side validation on a form with multiple buttons ..

  1. #1
    Join Date
    Mar 2007
    Posts
    17

    Default commons client side validation on a form with multiple buttons ..

    Hi Forum,

    I'm using spring web MVC. I have a form in which there are three buttons. They are "submit" , "cancel", "reset". Upon submitting the form by clicking any of these button some client side validations are performed. I'm using commons-validator for this. The problem is the validations are getting kicked in when any of the three buttons are clicked but I want the validations to be performed only when the "submit" button is clicked.

    I understand this is happening because I'm using the following code in the HTML form

    Code:
    <form method="POST" action="/webapp/myAction"  onsubmit="return validateShoppingCartForm(this)" >
    .
    .
    .
    .
    </form>
    Is there a way anybody knows of how to validate the form only when submitting from the "submit" button not when the "cancel" or "reset' buttons are clicked.


    Any ideas suggestions are welcome...

  2. #2
    Join Date
    Mar 2007
    Posts
    17

    Default

    Didn't anybody run into this problem at all? Am I clear with my qestion?

  3. #3
    Join Date
    Sep 2006
    Location
    Omaha, NE
    Posts
    60

    Default

    Would something like this work???

    Set a "clickedId" or something on each of your buttons. Then check in your validateShoppingCartForm() function which clickedId is stored on your form.

    JSP:
    Code:
    <input type="hidden" name="clickId" id="clickId" value=""/>
    <input type="submit" name="submit" value="submit" onclick="setClickedId(this.form, 'submit');"/>
    <input type="submit" name="cancel" value="cancel" onclick="setClickedId(this.form, 'cancel');"/>
    <input type="submit" name="reset" value="reset" onclick="setClickedId(this.form, 'reset');"/>

    Javascript:

    Code:
    function setClickedId (form, code){
         form.clickId.value = code;
    }
    
    function validateShoppingCartForm(form) {
    if (form.clickId.value == "submit") {
    //do validation
    }
    }

  4. #4
    Join Date
    Sep 2006
    Location
    Omaha, NE
    Posts
    60

    Default

    Then again...you could only run the validate function on the submit of the Submit button.

    Instead of putting on the form level your "onsubmit=...", do it on the onClick of the submit button.

    Code:
    <form....>
    ....
    ...
    JSP Stuff Here
    ...
    ....
    <input type="submit" name="submit" value="Submit" onclick="validateShoppingCartForm(this.form);"/>
    <input type="submit" name="cancel" value="Cancel"/>
    <input type="submit" name="reset" value="reset"/>
    
    </form>

  5. #5
    Join Date
    Mar 2007
    Posts
    17

    Default

    In general there are ways to make sure that java script validations are run only on the click of a particular button. Both the examples you gave exactly do that.

    My problem is how can I get either one of them working by using "Commons Validator"?

    Spring Modules provides the possibility to use a client side validations. The JavaScript is automatically generated when I map the form fields to java script functions in an xml file.

  6. #6
    Join Date
    Sep 2004
    Location
    Manchester, NH
    Posts
    1,236

    Default

    Well, all the Commons Validator integration does (I assume you're using the spring-modules integration) is generate the Javascript validation method for you. It's up to you to figure out where to call it from. In your example, you're calling the validateShoppingCartForm method (which was generated by the Commons Validator) in your onsubmit handler. Instead, use the "onclick on the submit button" pattern to call that generated method instead.
    Peter Mularien | Blog
    Author, Spring Security 3 (Book) - Packt Publishing, Available in print and eBook form
    SCJP 5, Oracle DBA
    Any postings are my own opinion, and should not be attributed to my employer or clients.


  7. #7
    Join Date
    Mar 2007
    Posts
    17

    Default

    Thanks pmularien and nllarson, This worked now.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •