I have this custom validator:

class EmailFormatValidator < ActiveModel::EachValidator
def validate_each(object, attribute, value)
unless value =~ /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
object.errors[attribute] << (options[:message] || 'is not valid')
end
end
end

I'm using it in my user.rb model this way:

validates :email, presence: true, email_format: true

When email is invalid, this is the error shown:

Email is not valid

How can I customize the *entire string*?

I have tried this way in the custom validator:

class EmailFormatValidator < ActiveModel::EachValidator
def validate_each(object, attribute, value)
unless value =~ /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
object.errors[attribute] = "Maybe your email is malformed."
end
end
end

But it is always prefixing the field name in the error:

Email Maybe your email is malformed.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/J4whtT5qXdQJ.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

  • Peter Vandenabeele at Feb 9, 2012 at 12:17 pm

    On Wed, Feb 8, 2012 at 5:50 PM, David M wrote:

    I have this custom validator:

    class EmailFormatValidator < ActiveModel::EachValidator
    def validate_each(object, attribute, value)
    unless value =~ /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
    object.errors[attribute] << (options[:message] || 'is not valid')
    end
    end
    end

    I'm using it in my user.rb model this way:

    validates :email, presence: true, email_format: true

    When email is invalid, this is the error shown:

    Email is not valid

    How can I customize the *entire string*?

    I have tried this way in the custom validator:

    class EmailFormatValidator < ActiveModel::EachValidator
    def validate_each(object, attribute, value)
    unless value =~ /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
    object.errors[attribute] = "Maybe your email is malformed."
    end
    end
    end

    But it is always prefixing the field name in the error:

    Email Maybe your email is malformed.
    Which exact code are you using to "show" the error message?

    Internally, the error message is only remembered as "is not valid" or
    "can't be blank" (without the attribute name, as you want to have),
    but certain forms of showing the error (related to the "full_message"
    function of ActiveModel errors.rb) join it by default with the attribute
    name:

    E.g.

    $ rails c
    Loading development environment (Rails 3.2.1)
    1.9.3p0 :001 > o = Order.new
    1.9.3p0 :002 > o.valid?
    => false
    1.9.3p0 :004 > o.errors[:name]
    => ["can't be blank"]
    1.9.3p0 :005 > o.errors.full_messages
    => ["Name can't be blank" ...]
    1.9.3p0 :007 > o.errors.full_messages[0]
    => "Name can't be blank"

    So, if you simply used user.errors[attribute], you would get the
    error message without the attribute prepended.

    If you want to have the list of all of them, code like this might help

    1.9.3p0 :011 > o.errors.map{|attr, message| message}.join('<br/>')
    => "can't be blank<br/>can't be blank<br/> ... "


    Using I18n can resolve the problem in two ways:

    ../config/locales$ cat errors.yml
    en:

    errors:
    # The default format to use in full error messages.
    # format: "%{attribute} %{message}"
    format: "%{message}"

    activerecord:
    errors:
    models:
    order:
    attributes:
    name:
    blank: Please fill in the name for this order


    $ rails c
    Loading development environment (Rails 3.2.1)
    1.9.3p0 :001 > o = Order.new
    1.9.3p0 :002 > o.valid?
    => false
    1.9.3p0 :003 > o.errors.full_messages
    => ["Please fill in the name for this order", "can't be blank", ...]

    I have changed 2 things here:

    1) the error message itself for the Order#name attribute for a :blank
    condition is customized.

    2) the default "errors.format" that is used to form the "full_message"
    string is changed to only have the error message (not prepended with
    the attribute).

    Source code can be found here:

    https://github.com/rails/rails/blob/master/activemodel/lib/active_model/locale/en.yml#L4
    https://github.com/rails/rails/blob/master/activemodel/lib/active_model/errors.rb#L272

    HTH,

    Peter


    *** Available for a new project ***

    Peter Vandenabeele
    http://twitter.com/peter_v
    http://rails.vandenabeele.com
    http://coderwall.com/peter_v

    --
    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 rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
  • Dave Aronson at Feb 10, 2012 at 2:45 am

    On Wed, Feb 8, 2012 at 11:50, David M wrote:

    class EmailFormatValidator < ActiveModel::EachValidator
    def validate_each(object, attribute, value)
    unless value =~ /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
    object.errors[attribute] << (options[:message] || 'is not valid')
    end
    end
    end ...
    When email is invalid, this is the error shown:

    Email is not valid

    How can I customize the entire string?

    I have tried this way in the custom validator: ....
    object.errors[attribute] = "Maybe your email is malformed."
    Note the difference in the technique you're using to alter
    object.errors[attribute] in each case. Try mimicking the original way
    more closely. Let us know if that works.

    -Dave

    --
    Dave Aronson:  Available Cleared Ruby on Rails Freelancer
    (NoVa/DC/Remote) -- see www.DaveAronson.com, and blogs at
    www.Codosaur.us, www.Dare2XL.com, www.RecruitingRants.com

    --
    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 rubyonrails-talk@googlegroups.com.
    To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
    For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedFeb 9, '12 at 11:22a
activeFeb 10, '12 at 2:45a
posts3
users3
websiterubyonrails.org
irc#RubyOnRails

People

Translate

site design / logo © 2021 Grokbase