Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 708803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:25:56+00:00 2026-05-14T04:25:56+00:00

Rails displays all validation error messages associated with a given field. If I have

  • 0

Rails displays all validation error messages associated with a given field. If I have three validates_XXXXX_of :email, and I leave the field blank, I get three messages in the error list.

Example:

validates_presence_of :name
validates_presence_of :email
validates_presence_of :text

validates_length_of :name, :in => 6..30
validates_length_of :email, :in => 4..40
validates_length_of :text, :in => 4..200

validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i<br/>

<%= error_messages_for :comment %> gives me:

7 errors prohibited this comment from being saved

There were problems with the following fields:

Name can't be blank
Name is too short (minimum is 6 characters)
Email can't be blank
Email is too short (minimum is 4 characters)
Email is invalid
Text can't be blank
Text is too short (minimum is 4 characters)

It is better to display one messages at a time. Is there an easy way to fix this problem? It looks straightforward to have a condition like: If you found an error for :email, stop validating :email and skip to the other field.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:25:56+00:00Added an answer on May 14, 2026 at 4:25 am

    Bert over at RailsForum wrote about this a little while back. He wrote the code below and I added some minor tweaks for it to run on Rails-3.0.0-beta2.

    Add this to a file called app/helpers/errors_helper.rb and simply add helper "errors" to your controller.

    module ErrorsHelper
    
      # see: lib/action_view/helpers/active_model_helper.rb
      def error_messages_for(*params)
            options = params.extract_options!.symbolize_keys
    
            objects = Array.wrap(options.delete(:object) || params).map do |object|
              object = instance_variable_get("@#{object}") unless object.respond_to?(:to_model)
              object = convert_to_model(object)
    
              if object.class.respond_to?(:model_name)
                options[:object_name] ||= object.class.model_name.human.downcase
              end
    
              object
            end
    
            objects.compact!
            count = objects.inject(0) {|sum, object| sum + object.errors.count }
    
            unless count.zero?
              html = {}
              [:id, :class].each do |key|
                if options.include?(key)
                  value = options[key]
                  html[key] = value unless value.blank?
                else
                  html[key] = 'errorExplanation'
                end
              end
              options[:object_name] ||= params.first
    
              I18n.with_options :locale => options[:locale], :scope => [:errors, :template] do |locale|
                header_message = if options.include?(:header_message)
                  options[:header_message]
                else
                  locale.t :header, :count => count, :model => options[:object_name].to_s.gsub('_', ' ')
                end
    
                message = options.include?(:message) ? options[:message] : locale.t(:body)
    
                error_messages = objects.sum do |object|
                  object.errors.on(:name)
                  full_flat_messages(object).map do |msg|
                    content_tag(:li, ERB::Util.html_escape(msg))
                  end
                end.join.html_safe
    
                contents = ''
                contents << content_tag(options[:header_tag] || :h2, header_message) unless header_message.blank?
                contents << content_tag(:p, message) unless message.blank?
                contents << content_tag(:ul, error_messages)
    
                content_tag(:div, contents.html_safe, html)
              end
            else
              ''
            end
      end
    
      ####################
      #
      # added to make the errors display in a single line per field
      #
      ####################
      def full_flat_messages(object)
        full_messages = []
    
        object.errors.each_key do |attr|
          msg_part=msg=''
          object.errors[attr].each do |message|
            next unless message
            if attr == "base"
              full_messages << message
            else
              msg=object.class.human_attribute_name(attr)
              msg_part+= I18n.t('activerecord.errors.format.separator', :default => ' ') + (msg_part=="" ? '': ' & ' ) + message
            end
          end
          full_messages << "#{msg} #{msg_part}" if msg!=""
        end
        full_messages
      end
    
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Rails app with some basic models. The website displays data retrieved
Relating to my last question here: Rails: Finding all associated objects to a parent
Rails is a great platform, but it just doesn't have the history that Perl
I've got a rails app that displays a list of items called modules. I'm
I am working on a Rails app that displays a sports schedule (in a
I have a select input on my page. this select input displays/hides fields in
I have trouble getting started with rails 3.0.0, because it just doesn't load the
Rails uses the concept of migrations to deal with model changes using the ActiveRecord
Rails comes with a handy session hash into which we can cram stuff to
Rails comes with RESTful resources out of the box, but do you use those

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.