# Rail3: UJS submit after client-side validation

**URL:** <https://www.ruby-forum.com/t/rail3-ujs-submit-after-client-side-validation/193279>\
**Category:** Rails\
**Created:** [August 14, 2010, 5:26pm UTC](https://www.ruby-forum.com/t/rail3-ujs-submit-after-client-side-validation/193279 "2010-08-14T17:26:01Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![AppleII717](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@AppleII717](https://www.ruby-forum.com/u/AppleII717)\
**Post date:** [August 14, 2010, 5:26pm UTC](https://www.ruby-forum.com/t/rail3-ujs-submit-after-client-side-validation/193279/1 "2010-08-14T17:26:01Z")

</div>

I am have some problems with UJS. I have a (potentially) large form  
that I was doing client side validation on (yes there is also some  
server side validation) using an old prototype validation routine that  
is not working with the current version.

I actually got validation to work by coping and modifying some of the  
rail.js routine to observer submit. I also had some javascript toggles  
that I got to work.

I then found a post on behaviors:

[http://weblog.jamisbuck.org/2010/3/2/unobtrusive-yet-explicit](http://weblog.jamisbuck.org/2010/3/2/unobtrusive-yet-explicit)

This method worked great for my toggles, but then I tried to create  
one for the submit button. While it did the validation, It would  
always submit.

I then changed the input type to button and the validation would work,  
but I could not get the form to submit.

Debugging all seemed to work (names, classes, etc)

Behaviors.add(“click”, “validate”, function(element) {  
var formElements = $$(“.required”, “.required-one”);  
var valid = true;  
var thisform = element.form  
… validations stuff

but when I got to the end  
if(valid){thisform.submit()}

would fail saying the input element does not have a function submit.  
thisform is reported as and htmlformelement.

I know this may be a little off target but I bet I am not the only one  
using client-side validation.

If someone can point me in the right direction, I’ll go learn some  
more.

Steve

---

<div class="post-metadata">

**Author:** ![AppleII717](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/59ef9b/32.png) [@AppleII717](https://www.ruby-forum.com/u/AppleII717)\
**Post date:** [August 15, 2010, 5:00pm UTC](https://www.ruby-forum.com/t/rail3-ujs-submit-after-client-side-validation/193279/2 "2010-08-15T17:00:01Z")

</div>

On Aug 14, 10:24 am, AppleII717 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
…

> would fail saying the input element does not have a function submit.  
> thisform is reported as and htmlformelement.  
> …

This must have been one of those posts where nobody knew what I was  
talking about. That just spawns another learning exercise.

My problem was I didn’t understand Javascript as much as I should to  
even ask the question. At least I didn’t know about anonymous  
functions.

I solved my problem when I figured out how those functions work, and  
more importantly, what arguments are passed to them.

Just in case anyone wants to know about Unobtrusive Javascript on the  
client side, I posted a gist [http://gist.github.com/525561](http://gist.github.com/525561) that  
outlines my learning experience and how I got it to work.

The sort version is, all that I needed was:  
if(!valid){event.stop()}

To stop the onsubmit event from propagating. I didn’t know I had  
“event” available in my Behaviors.add function.

Steve
