# How can I check form input before submit?

**URL:** <https://www.ruby-forum.com/t/how-can-i-check-form-input-before-submit/171356>\
**Category:** Rails\
**Created:** [July 1, 2009, 2:37am UTC](https://www.ruby-forum.com/t/how-can-i-check-form-input-before-submit/171356 "2009-07-01T02:37:34Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [July 1, 2009, 2:37am UTC](https://www.ruby-forum.com/t/how-can-i-check-form-input-before-submit/171356/1 "2009-07-01T02:37:34Z")

</div>

I want to check user input in a form before submit. Is there a standard  
way to do this in Rails?

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [July 1, 2009, 3:21am UTC](https://www.ruby-forum.com/t/how-can-i-check-form-input-before-submit/171356/2 "2009-07-01T03:21:31Z")

</div>

You will want to use validation checks.

[http://guides.rubyonrails.org/activerecord\_validations\_callbacks.html](http://guides.rubyonrails.org/activerecord_validations_callbacks.html)

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [July 1, 2009, 4:00am UTC](https://www.ruby-forum.com/t/how-can-i-check-form-input-before-submit/171356/3 "2009-07-01T04:00:31Z")

</div>

Ã„lphÃ¤ BlÃ¼Ã« wrote:

> You will want to use validation checks.
> 
> [http://guides.rubyonrails.org/activerecord\_validations\_callbacks.html](http://guides.rubyonrails.org/activerecord_validations_callbacks.html)

This is the Active Record validation. I am looking for something like  
Action Controller validation( I am not sure whether rails has this  
feature). I want to validate the user input in the web form when user  
clicks sumbit button.

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [July 1, 2009, 4:48am UTC](https://www.ruby-forum.com/t/how-can-i-check-form-input-before-submit/171356/4 "2009-07-01T04:48:06Z")

</div>

So, the first question you have to answer is what type of form are you  
using? If you are using a form\_for then that’s backed by a model and  
you can use validation checks. If you are using form\_tag then that  
isn’t backed by a model and there are other ways of checking, depending  
on what you are using.

Example: I have a form\_tag that works with an ajax widget so I can  
actually create a validation method using javascript and call the  
validation on submit.

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [July 1, 2009, 4:57am UTC](https://www.ruby-forum.com/t/how-can-i-check-form-input-before-submit/171356/5 "2009-07-01T04:57:15Z")

</div>

Zhao Yi wrote:  
[…]

> This is the Active Record validation. I am looking for something like  
> Action Controller validation( I am not sure whether rails has this  
> feature). I want to validate the user input in the web form when user  
> clicks sumbit button.

If you set up the validations in the model, Rails will validate form  
submissions just as you are describing. Try it!

## Best,

Marnen Laibow-Koser

> **[marnen.org: Homepage of Marnen Laibow-Koser](https://marnen.org/)**

[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [July 1, 2009, 5:09am UTC](https://www.ruby-forum.com/t/how-can-i-check-form-input-before-submit/171356/6 "2009-07-01T05:09:04Z")

</div>

Ã„lphÃ¤ BlÃ¼Ã« wrote:

> So, the first question you have to answer is what type of form are you  
> using? If you are using a form\_for then that’s backed by a model and  
> you can use validation checks. If you are using form\_tag then that  
> isn’t backed by a model and there are other ways of checking, depending  
> on what you are using.
> 
> Example: I have a form\_tag that works with an ajax widget so I can  
> actually create a validation method using javascript and call the  
> validation on submit.

I use form\_tag. How can I do the validation? Do you mean I have to write  
it in javascript? Does Rails have a validation helper on this?

thanks.

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [July 1, 2009, 5:33am UTC](https://www.ruby-forum.com/t/how-can-i-check-form-input-before-submit/171356/7 "2009-07-01T05:33:32Z")

</div>

I think I might have found a fix for your issue:

[http://www.hackido.com/2009/06/validating-formtag-in-activerecord.html](http://www.hackido.com/2009/06/validating-formtag-in-activerecord.html)

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [July 1, 2009, 5:22am UTC](https://www.ruby-forum.com/t/how-can-i-check-form-input-before-submit/171356/8 "2009-07-01T05:22:39Z")

</div>

Hey mate, so I wish I could give you a lot of advice on this but I’m  
getting my feet wet on similar issues. 🙂

The best practices you should remember is that you use form\_for when you  
are doing inserting of rows or data (create/edit) etc. and form\_tag is  
just for creating normal forms.

Because it doesn’t have a model, from my understanding you have to call  
a validation method either one of two ways:

1. with a validation check via javascript like so:

\<% form\_tag your\_controller\_path, :method =\> ‘get’, :onsubmit =\> ‘return  
validate(this)’ do %\>  
form…  
\<% end %\>

The validate() would be a javascript validation method that would return  
true or false depending on the criteria you are testing.

1. you could also use the config/initializers folder to write your ruby  
code as helpers and apply code that will disable the submit button until  
the conditions are properly met or something similar. Here’s a good  
example of that:

[http://www.webficient.com/2008/10/18/easy-rails-patching](http://www.webficient.com/2008/10/18/easy-rails-patching)

However, as always (when someone like me who is also new) answers  
questions, someone with a lot more experience with rails might come  
along and teach us both something new. 🙂

Marnen is a really good expert and has helped me out a ton in the past.  
If he says you can do the validation checks in the model, it might  
possibly be that you can. I haven’t found a way to pass the validation  
checks to the model without using a form\_for tag though…

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [July 1, 2009, 5:59am UTC](https://www.ruby-forum.com/t/how-can-i-check-form-input-before-submit/171356/9 "2009-07-01T05:59:12Z")

</div>

Zhao Yi wrote:  
[…]

> I use form\_tag. How can I do the validation?

The easiest way would be to use form\_for, in which case Rails will do  
the validation automatically. I’m not sure if there’s an automatic  
method with form\_tag.

## Best,

Marnen Laibow-Koser

> **[marnen.org: Homepage of Marnen Laibow-Koser](https://marnen.org/)**

[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [July 2, 2009, 5:09am UTC](https://www.ruby-forum.com/t/how-can-i-check-form-input-before-submit/171356/10 "2009-07-02T05:09:12Z")

</div>

OK, I understand. Thanks all your help.

---

<div class="post-metadata">

**Author:** ![youhaodeyi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/b9bd4f/32.png) [@youhaodeyi](https://www.ruby-forum.com/u/youhaodeyi)\
**Post date:** [July 2, 2009, 4:49am UTC](https://www.ruby-forum.com/t/how-can-i-check-form-input-before-submit/171356/11 "2009-07-02T04:49:30Z")

</div>

If what you want is to validate the form on the browser you’ll need  
Javascript. In any case you’ll need to also validate it in the server  
because anybody can fake a page with appropriate modifications to try  
and hack your app., completely bypassing any validations you might put  
in place with Javascript.

Pepe
