Use .add method for adding errors

This avoids a deprecation warning on rails 6.1
This commit is contained in:
Andy Allan 2021-03-17 17:02:32 +00:00
parent a2ddcda911
commit 6f70c13062
2 changed files with 4 additions and 4 deletions

View file

@ -3,7 +3,7 @@ class CharactersValidator < ActiveModel::EachValidator
INVALID_URL_CHARS = "/;.,?%#".freeze
def validate_each(record, attribute, value)
record.errors[attribute] << (options[:message] || I18n.t("validations.invalid_characters")) if /[#{INVALID_CHARS}]/o.match?(value)
record.errors[attribute] << (options[:message] || I18n.t("validations.url_characters", :characters => INVALID_URL_CHARS)) if options[:url_safe] && /[#{INVALID_URL_CHARS}]/o.match?(value)
record.errors.add(attribute, options[:message] || I18n.t("validations.invalid_characters")) if /[#{INVALID_CHARS}]/o.match?(value)
record.errors.add(attribute, options[:message] || I18n.t("validations.url_characters", :characters => INVALID_URL_CHARS)) if options[:url_safe] && /[#{INVALID_URL_CHARS}]/o.match?(value)
end
end

View file

@ -1,6 +1,6 @@
class WhitespaceValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
record.errors[attribute] << (options[:message] || I18n.t("validations.leading_whitespace")) if !options.fetch(:leading, true) && /\A\s/.match?(value)
record.errors[attribute] << (options[:message] || I18n.t("validations.trailing_whitespace")) if !options.fetch(:trailing, true) && /\s\z/.match?(value)
record.errors.add(attribute, options[:message] || I18n.t("validations.leading_whitespace")) if !options.fetch(:leading, true) && /\A\s/.match?(value)
record.errors.add(attribute, options[:message] || I18n.t("validations.trailing_whitespace")) if !options.fetch(:trailing, true) && /\s\z/.match?(value)
end
end