I have a form_for
form to which I added custom fields using *_tag
methods.
For normal form_for
elements I have validation inside the model, however I am not sure what should I do with my text_field_tag
elements.
What would be the most elegant way to validate those fields?
Code update:
<%= form_for(@member, :html => {:class => "generic-form"}) do |f| %>
<% if @member.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@member.errors.count, "error") %> prohibited this member from being saved:</h2>
<ul>
<% @member.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<%= f.label :username, "Username:" %>
<%= f.text_field :username %>
<%= f.label :password, "Password:" %>
<%= f.password_field :password %>
<%= label_tag "repeatPassword", "Repeat password:" %>
<%= password_field_tag "repeatPassword" %>
<%= f.label :email, "Email:" %>
<%= f.text_field :email %>
<button type="submit" name="commit">Register</button>
<% end %>
I guess you have an action which update/create a Member instance. In this action you could do something like this:
def update
[...]
if params[:repeatPassword] == params[:member][:password]
@member.save
else
@member.errors.add('password', 'Passwords werent the same')
redirect_to :action => :update, :id => params[:id]
end
[...]
end
Hope this Helps!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.