Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing validation errors next to its field

Is there any way to show errors not on top of form page, but next to field, that fired an error?

like image 931
sanny Sin Avatar asked Dec 19 '12 10:12

sanny Sin


1 Answers

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">&nbsp;#{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">&nbsp;#{errors}</span>).html_safe
  end

end

ref: rails guide

like image 127
emrahbasman Avatar answered Oct 11 '22 00:10

emrahbasman