[ 12 posts ] Go to page 1, 2 Next

nickweavers

  • Joined: Fri Jan 29, 2010 2:05 am
  • Posts: 62
  • Offline
  • Profile
Tags:

validate on a button click event rather than onSubmit

Post Posted: Wed Jul 04, 2012 6:29 am
+0-
Hi,

I'm not a YUI or javascript expert, but I would really like to use Form to help me validate form fields.

I have a simple mortgage calculator with three input fields (loan_amount, interest_rate, loan_term) and I don't need to submit the form, just detect a click on a Calculate button, perform, the calculation and show the result in a read-only field. However, I do need to validate the 3 input fields before I perform the calculation. At the moment, it looks like validation is performed when the submit event is detected. Since I have my form set up with onsubmit="return(false);", can I configure form to use another event?

TIA,
Nick

Alberto Santini

YUI Contributor

  • Offline
  • Profile

Re: validate on a button click event rather than onSubmit

Post Posted: Wed Jul 04, 2012 7:29 am
+0-
Hello nickweavers.

It may help the validator attribute.

Did you give a look at form examples:
http://www.greghinch.com/examples/form/

Hope that helps,
IceBox

nickweavers

  • Joined: Fri Jan 29, 2010 2:05 am
  • Posts: 62
  • Offline
  • Profile

Re: validate on a button click event rather than onSubmit

Post Posted: Wed Jul 04, 2012 8:09 am
+0-
Hi Alberto,

Yes I looked at the examples but didn't see anything there on how to either use another event.

Nick

Alberto Santini

YUI Contributor

  • Offline
  • Profile

Re: validate on a button click event rather than onSubmit

Post Posted: Wed Jul 04, 2012 8:39 am
+0-
Hello Nick.

Maybe I misunderstood the use case.

In the examples the validation is done before the submit using validator.

Is it not your case?

Regards,
Alberto

Greg Hinch

YUI Contributor

  • Username: greghinch
  • Joined: Wed Jun 03, 2009 10:49 am
  • Posts: 73
  • Location: Fairfax, CA , USA
  • Twitter: greghinch
  • GitHub: ghinch
  • Gists: ghinch
  • IRC: gr-eg
  • Offline
  • Profile

Re: validate on a button click event rather than onSubmit

Post Posted: Wed Jul 04, 2012 9:20 am
+0-
It may make sense to use inline validation:
http://www.greghinch.com/yui3-gallery/docs/Form.html#config_inlineValidation
Setting this to true in your form will validate that individual field as soon as it is changed by the user. You can also set "validateInline" to true on individual fields to just do that on specific ones and not the whole form. Finally, you can call "validateField()" on any field at any time to do the validation when it makes sense for you, but you'll have to define when that is specifically for you application.
Hope that helps!

nickweavers

  • Joined: Fri Jan 29, 2010 2:05 am
  • Posts: 62
  • Offline
  • Profile

Re: validate on a button click event rather than onSubmit

Post Posted: Wed Jul 04, 2012 11:15 am
+0-
Hi Alberto,

I think it was me who misunderstood the order of things. Thanks for making it clearer.

Nick

nickweavers

  • Joined: Fri Jan 29, 2010 2:05 am
  • Posts: 62
  • Offline
  • Profile

Re: validate on a button click event rather than onSubmit

Post Posted: Wed Jul 04, 2012 11:29 am
+0-
Hi Greg,

Right, inline validation would work nicely I think.

The bit that confuses me in the code that is generated is that in my markup I have a button like this
<button id="repayment_calculator_submit" type="submit">Calculate</button>
but after calling myForm.render(); it changes to this <button id="repayment_calculator_submit-field" class="field">Calculate</button>

The problem for me is that I have an event listener for the onSubmit that looks like this

function calculateMortgageRepayment(e) {
e.preventDefault();
...
// Use form field contents to do calculation and show result
...
}

So this now isn't called and my Calculate button doesn't work.

How should I get around this?

Thanks,
Nick

Alberto Santini

YUI Contributor

  • Offline
  • Profile

Re: validate on a button click event rather than onSubmit

Post Posted: Wed Jul 04, 2012 12:19 pm
+0-
Hello Nick.

I think you should set to false the attribute (not documented) submitViaIO and define (override) the submit function with the content of the calculateMortgageRepayment function.

See the working example:
http://jsfiddle.net/HHEMZ/

Hope that helps,
Alberto

P.S.: Greg, I am sorry if I have been overlapping this thread. I hope my suggestion is correct.

P.S.: Nick, and you should remove onsubmit.

nickweavers

  • Joined: Fri Jan 29, 2010 2:05 am
  • Posts: 62
  • Offline
  • Profile
Tags:

Re: validate on a button click event rather than onSubmit

Post Posted: Wed Jul 04, 2012 3:19 pm
+0-
Thanks for all your help Alberto.

I have tried to replicate the essential parts of what you had in the jsfiddle example, but even with "submitViaIO: false" in the form config and the
repayment_calculator.get('srcNode').submit = function () {
alert('Pressed submit');
....
}
I am seeing the form get submitted and the alert does not show.

You can see the page with the form on here: https://www.chimneypotsonline.co.uk/ind ... owFormPage
(I'm only working on the lower form at the moment, there are two forms on the page.)
I must have overlooked something so I'll keep staring at the code.

Nick

Alberto Santini

YUI Contributor

  • Offline
  • Profile
Tags:

Re: validate on a button click event rather than onSubmit

Post Posted: Wed Jul 04, 2012 9:00 pm
+0-
Hello Nick.

In that page there is an error in yui_calculate_mortgage_repayment.js (line27): Uncaught ReferenceError: myForm is not defined.

I think you should use repayment_calculator.

And in submit function there is the line "e.preventDefault();": "e" is not defined.

Hope that helps,
Alberto
  [ 12 posts ] Go to page 1, 2 Next
Display posts from previous:  Sort by  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum