开发者

Rails 3: form_tag validation

开发者 https://www.devze.com 2023-02-03 03:56 出处:网络
Can I use ActiveRecord-like form validation when using form_tag and not form_form (hence afor not bound to a model)? 开发者_开发知识库How?You can\'t have validations with form_tag as your form is not

Can I use ActiveRecord-like form validation when using form_tag and not form_form (hence a for not bound to a model)? 开发者_开发知识库How?


You can't have validations with form_tag as your form is not bound to a model.

But the good part is that with Rails 3 and ActiveModel you can create models that are not bound to your database. So you can create "virtual" models with validations for search forms, contact forms, etc.

See : https://github.com/novagile/basic_active_model


As form_tag does not bound to any models, so you have to validate the data and write code for displaying the errors by yourself.

Or you could try to create non-database-backend models with validations and use form_for instead.


This works well for me in Rails 3.0.9:

<%= form_tag("/events", :id => "new_event") do %>
  <%= error_messages_for @event %>
  <%= submit_tag "Submit" %>
<% end %>


As long as there is an object that is getting saved and you have some sort of validations on the model AR will return errors.

0

精彩评论

暂无评论...
验证码 换一张
取 消