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 tobase
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
Generating a Short Uuid String Using Uuidtools in Rails
Certificate Verify Failed in "Gem Install Foundation"
Can't Install Rmagick, Pkg-Config: Command Not Found
How to Turn a Ruby Method into a Block
Use Global or Constant Variable in Ruby/Rails
How to Alias a Class Method in Rails Model
Ruby: "&& Return" VS "And Return"
Ruby $Stdin.Gets Without Showing Chars on Screen
Ruby: How to Generate CSV Files That Has Excel-Friendly Encoding
Is Alias_Method_Chain Synonymous with Alias_Method
Is Time.Zone.Now.To_Date Equivalent to Date.Today
If Else Statements in .Html.Erb in Views
Why Do I Get "No Implicit Conversion of String into Integer (Typeerror)"