Code tagged with error

A better field error proc

Posted by Chad Humphries 2 months ago
# this applies a css class of field_with_error to the label/select/textarea/input tags, no wrapping containers

ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
  msg = instance.error_message
  error_style = "field_with_error"
  if html_tag =~ /<(label|input|textarea|select)[^>]+class=/
    class_attribute = html_tag =~ /class=['"]/
    html_tag.insert(class_attribute + error_style.size.next, " #{error_style} ")
  elsif html_tag =~ /<(label|input|textarea|select)/
    first_whitespace = html_tag =~ /\s/
    html_tag[first_whitespace] = " class='#{error_style}' "
  end
  html_tag
end
Language Ruby / Tagged with rails, error