Rails 4: remove attribute name from error message in custom validator

Thibaud Clement

In my Rails 4 app, I implemented a custom validator named LinkValidator on my Post model:

class LinkValidator < ActiveModel::Validator

  def validate(record)
    if record.format == "Link"
      if extract_link(record.copy).blank?
        record.errors[:copy] << 'Please make sure the copy of this post includes a link.'
      end
    end
  end

end

Everything works fine, except that currently, the message displayed is:

1 error prohibited this post from being saved:
Copy Please make sure the copy of this post includes a link.

How can remove the word "copy" from the above message?

I tried record.errors << '...' instead of record.errors[:copy] << '...' in the validator, but then the validation no longer works.

Any idea?

BroiSatse

Unfortunately currently the format of full_messages for errors is controlled with a single I18n key errors.format, hence any change to it will have a global consequences.

Common option is to attach error to the base rather than to the attribute, as full messages for base errors do not include attribute human name. Personally I don't like this solution for number of reason, main being that if the validation error is caused by a field A, it should be attach to field A. It just makes sense. Period.

There is no good fix for this problem though. Dirty solution is to use monkey patching. Place this code in a new file in your config/initializers folder:

module ActiveModel
  class Errors
    def full_message(attribute, message)
      return message if attribute == :base
      attr_name = attribute.to_s.tr('.', '_').humanize
      attr_name = @base.class.human_attribute_name(attribute, :default => attr_name)
      klass = @base.class
      I18n.t(:"#{klass.i18n_scope}.error_format.#{klass.model_name.i18n_key}.#{attribute}", {
                                 :default   => [:"errors.format", "%{attribute} %{message}"],
                                 :attribute => attr_name,
                                 :message   => message
                             })
    end
  end
end

This preserves the behaviour of full_messages (as per rails 4.0), however it allows you to override the format of full_message for particular model attribute. So you can just add this bit somewhere in your translations:

activerecord:
  error_format:
    post:
      copy: "%{message}"

I honestly dislike the fact that there is no clean way to do this, that probably deserves a new gem.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Rails 4 - Remove attribute name from error message for associated model presence

From Dev

Rails 4 - Remove attribute name from error message for associated model presence

From Dev

custom validator on a couple of attribute in Rails

From Dev

Customize Rails Validation Message (Do not show attribute name) Rails 4

From Dev

Customize Rails Validation Message (Do not show attribute name) Rails 4

From Dev

Zend\Form\Form not displayin error message from custom validator

From Dev

Zend\Form\Form not displayin error message from custom validator

From Dev

Rails 4 custom validator clarifiaction

From Dev

rails 4: custom error pages for 404, 500 and where is the default 500 error message coming from?

From Dev

Validations with custom message and no attribute name

From Dev

jQuery validator custom error message position for rule

From Dev

Custom string in rails 4 active records error message

From Dev

Rails 4, Adding custom layout, got error message

From Dev

Remove prepended attribute name from full error messages for errors[:base]

From Dev

JQGrid remove cell value from custom error message in inline editing

From Dev

Gmaps4Rails v2: how to remove marker using a custom attribute?

From Dev

Custom Validation Attribute error message not showing with ValidationMessageFor

From Dev

How to change validation error message dynamically in extended (custom) validator class

From Dev

Properly implementing the Parsley.js Custom Remote Validator in Rails 4

From Dev

Custom validator to prevent overlapping appointments in Rails 4 app?

From Dev

Custom validator to prevent overlapping appointments in Rails 4 app?

From Dev

Rails custom validation error message not working

From Dev

Rails 4.2 ActionController:BadRequest custom error message

From Dev

Remove custom attribute from sales/quote in Magento

From Dev

How to remove attribute from object (Rails & Ajax)

From Dev

Rails remove time from datetime attribute and group

From Dev

Rails 4: How to display error message using a prop name in f.label

From Dev

grails custom validator: force no message

From Dev

Angular 4 Custom Validator ngIf not displaying error <span>

Related Related

  1. 1

    Rails 4 - Remove attribute name from error message for associated model presence

  2. 2

    Rails 4 - Remove attribute name from error message for associated model presence

  3. 3

    custom validator on a couple of attribute in Rails

  4. 4

    Customize Rails Validation Message (Do not show attribute name) Rails 4

  5. 5

    Customize Rails Validation Message (Do not show attribute name) Rails 4

  6. 6

    Zend\Form\Form not displayin error message from custom validator

  7. 7

    Zend\Form\Form not displayin error message from custom validator

  8. 8

    Rails 4 custom validator clarifiaction

  9. 9

    rails 4: custom error pages for 404, 500 and where is the default 500 error message coming from?

  10. 10

    Validations with custom message and no attribute name

  11. 11

    jQuery validator custom error message position for rule

  12. 12

    Custom string in rails 4 active records error message

  13. 13

    Rails 4, Adding custom layout, got error message

  14. 14

    Remove prepended attribute name from full error messages for errors[:base]

  15. 15

    JQGrid remove cell value from custom error message in inline editing

  16. 16

    Gmaps4Rails v2: how to remove marker using a custom attribute?

  17. 17

    Custom Validation Attribute error message not showing with ValidationMessageFor

  18. 18

    How to change validation error message dynamically in extended (custom) validator class

  19. 19

    Properly implementing the Parsley.js Custom Remote Validator in Rails 4

  20. 20

    Custom validator to prevent overlapping appointments in Rails 4 app?

  21. 21

    Custom validator to prevent overlapping appointments in Rails 4 app?

  22. 22

    Rails custom validation error message not working

  23. 23

    Rails 4.2 ActionController:BadRequest custom error message

  24. 24

    Remove custom attribute from sales/quote in Magento

  25. 25

    How to remove attribute from object (Rails & Ajax)

  26. 26

    Rails remove time from datetime attribute and group

  27. 27

    Rails 4: How to display error message using a prop name in f.label

  28. 28

    grails custom validator: force no message

  29. 29

    Angular 4 Custom Validator ngIf not displaying error <span>

HotTag

Archive