Colin (and Sami)

Indeed,  your link is relevant as I use the 
http://rubyforge.org/projects/custom-err-msg/
plugin to over-ride the error messages.  I notice that it hasn't
changed since early January this year so is it a 2.3.4 update to
Active Record that is causing some sort of compatability problem?  The
plugin code is below.  Any ideas? The model which is doing the
validation is below too.

module ActiveRecord
  class Errors

    # Redefine the ActiveRecord::Errors::full_messages method:
    #  Returns all the full error messages in an array. 'Base'
messages are handled as usual.
    #  Non-base messages are prefixed with the attribute name as usual
UNLESS
    # (1) they begin with '^' in which case the attribute name is
omitted.
    #     E.g. validates_acceptance_of :accepted_terms, :message =>
'^Please accept the terms of service'
    # (2) the message is a proc, in which case the proc is invoked on
the model object.
    #     E.g. validates_presence_of :assessment_answer_option_id,
    #     :message => Proc.new { |aa| "#{aa.label} (#{aa.group_label})
is required" }
    #     which gives an error message like:
    #     Rate (Accuracy) is required
    def full_messages
      full_messages = []

      @errors.each_key do |attr|
        @errors[attr].each do |msg|
          next if msg.nil?

          if attr == "base"
            full_messages << msg
          elsif msg =~ /^\^/
            full_messages << msg[1..-1]
          elsif msg.is_a? Proc
            full_messages << msg.call(@base)
          else
            full_messages << @base.class.human_attribute_name(attr) +
" " + msg
          end
        end
      end

      return full_messages
    end
  end
end

class Enquiry < ActiveRecord::Base

  validates_presence_of :name, :email, :subject, :message
  validate :validate_email

  def name
    firstname && lastname ? [firstname, lastname].join(' ') : ''
  end

  def name=(n)
    split = n.split(' ', 2)
    self.firstname = split.first
    self.lastname = split.last
  end

  protected

  def validate_email
    address = EmailVeracity::Address.new(email)
    errors.add(:email, "^We suspect that the email address #{email} is
incorrect.") unless address.valid?
  end

end

Beer o'clock in the UK.

O.

--

You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.


Reply via email to