How to Highlight Fields on Rails Validation Errors

How to Highlight Fields on Rails Validation Errors

Assuming you have an error class for fields in your CSS file:

<% if @user.errors[:name] %>
<%= f.label :name, :class => "error" %>
<% else %>
<%= f.label :name %>
<% end %>

Is this what you want?

Extra: here's a section about customizing default ActiveRecord validations CSS.

Edit: (about extra ifs)

# app/helpers/application_helper.rb

def field_class(resource, field_name)
if resource.errors[field_name]
return "error".html_safe
else
return "".html_safe
end
end

And then:

# in your view

<%= f.label :name, :class => field_class(@user, :name) %>
<%= f.label :password, :class => field_class(@user, :password) %>
[...]

(I may have make a mistake in there - I'm writing on a phone - but you get the general idea. You can code this in number of ways = infinity, so do it the way you like...)

Highlighting additional fields during custom validation

You should leverage Rails built in mechanisms: they consist in wrapping the form fields containing errors with a div with field_with_error class.

If this doesn't suit you:

  • customize it, see Railscast

  • more manual solution, check which errors your instance bears and act accordingly, ie @instance.errors.include? :filed_name. In this scope, don't add all your errors to base

How to get number of fields with validation error on rails

obj.errors.keys will give you the columns which have errors, you can do obj.errors.keys.count to get the total number of fields having errors.

Also, it will work for nested attributes because the nested attributes column having error will come as nested_model.nested_model_column_name

Rails 3 custom validation: Highlighting offending fields

So in your form you'd have something like this for an input field

<%= form.text_field :title %>

Since errors is a hash you could use the "include?" method like so...

errors.include?(:title)

This tells you that there's something wrong with this field. Now all you need to do is style it.

Whack on a ternary operator asi...

<% css_class = errors.include?(:title) ? "highlight_error_class" : "no_problem_class" %>
<%= form.text_field :title, :class => css_class %>

Done.

Rails 4: How to display fields with error in red?

# app/models/subject.rb
class Subject < ActiveRecord::Base
belongs_to :semester
validates :semester, :university, presence: true # new syntax http://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html#method-i-validates
end

# app/views/subjects/_form.html.erb
<%= form_for @subject do |f| %>
Semestr: <%= f.collection_select :semester_id, Semestr.all, :id, :name, prompt: true %>
University: <%= f.text_field :univercity %>
<% end %>

For more information about building forms in rails (with validations enabled) you could find there http://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html

Rails form errors - validation

yes it's very easy, you will likely find that the field with errors will have a class of ".field_with_errors" or something similar applied to it

just style all inputs that fall within that class to have 2px solid red border

i.e.

// style.sass
.field_with_errors
input
border: 2px solid red


Related Topics



Leave a reply



Submit