Reputation: 1555
Is there any way to show errors not on top of form page, but next to field, that fired an error?
Upvotes: 13
Views: 2801
Reputation: 605
<td class="error"><%[email protected][:firstname].join(",") %></td>
Upvotes: 0
Reputation: 17735
This used to be part of Rails, now it's available in a gem:
https://github.com/joelmoss/dynamic_form
It allows you to easily display the errors for any particular attribute of a form builder object, for example:
<%= f.text_field :foo %>
<%= f.error_message_on :foo %>
Upvotes: 1
Reputation: 2013
initializers/my_custom_error_messages.rb
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
errors = Array(instance.error_message).join(',')
%(#{html_tag}<span class="validation-error"> #{errors}</span>).html_safe
end
update:
without label
ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
errors = Array(instance.error_message).join(',')
if html_tag =~ /^<label/
html_tag
else
%(#{html_tag}<span class="validation-error"> #{errors}</span>).html_safe
end
end
ref: rails guide
Upvotes: 8
Reputation: 15109
The errors are displayed on the top of your form because of the @model.errors
iteration in the top div of your layout. If you move this code to check for errors on your hash on each field, you can acomplish what you want.
Upvotes: 0
Reputation: 75
You can use simple_form gem to show the validation error with the fields
<%= simple_form_for @user do |f| %>
<%= f.input :username %>
<%= f.input :password %>
<%= f.button :submit %>
<% end %>
And if you want to show the validation error on the top of form use
object.error_messages
After form tag
Upvotes: 1