Overriding Default Error Msg for custom Validation

i have a custom validator for a model, and I want to override the
formatting of error msg that is being outputed to the view. By default,
the code below gives me an error msg of “Website Please make sure the
following Website(s) have valid URLs”, so how do I remove the word
exclude the attr_name that is on the beginning of the sentence?

def validates_url_of(*attr_names)
configuration = { :on => :save }
configuration.update(attr_names.extract_options!)

validates_each(attr_names,configuration) do |record, attr_name,

value|
begin
uri = URI.parse(value)
raise URI::InvalidURIError unless [URI::HTTP,
URI::HTTPS].include? uri.class
rescue URI::InvalidURIError
record.errors.add(attr_name, “Please ensure the following
#{attr_name.to_s.capitalize}(s) have valid URLs”)
end
end
end

I figured out what’s missing now. I was using
<% @person.full_messages.each do |msg| %> to output the error msg
before.

Using <% @person.errors.each do |attr,msg| %> solves my problem by
giving me just the msg w/o the attribute name

Reginald T. wrote in post #1005968:

i have a custom validator for a model, and I want to override the
formatting of error msg that is being outputed to the view. By default,
the code below gives me an error msg of “Website Please make sure the
following Website(s) have valid URLs”, so how do I remove the word
exclude the attr_name that is on the beginning of the sentence?

def validates_url_of(*attr_names)
configuration = { :on => :save }
configuration.update(attr_names.extract_options!)

validates_each(attr_names,configuration) do |record, attr_name,

value|
begin
uri = URI.parse(value)
raise URI::InvalidURIError unless [URI::HTTP,
URI::HTTPS].include? uri.class
rescue URI::InvalidURIError
record.errors.add(attr_name, “Please ensure the following
#{attr_name.to_s.capitalize}(s) have valid URLs”)
end
end
end

i meant i was using <% @person.errors.full_messages.each do |msg|
%> before

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs