I want to check user input in a form before submit. Is there a standard way to do this in Rails? -- Posted via http://www.ruby-forum.com/.
You will want to use validation checks. http://guides.rubyonrails.org/activerecord_validations_callbacks.html -- Posted via http://www.ruby-forum.com/.
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. -- Posted via http://www.ruby-forum.com/.
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 http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
Ä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. -- Posted via http://www.ruby-forum.com/.
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. 2) 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 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... -- Posted via http://www.ruby-forum.com/.
I think I might have found a fix for your issue: http://www.hackido.com/2009/06/validating-formtag-in-activerecord.html -- Posted via http://www.ruby-forum.com/.
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 http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
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 On Jun 30, 8:37 pm, Zhao Yi <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> I want to check user input in a form before submit. Is there a standard > way to do this in Rails? > -- > Posted viahttp://www.ruby-forum.com/.