]> git.openstreetmap.org Git - rails.git/commitdiff
Use .add method for adding errors
authorAndy Allan <git@gravitystorm.co.uk>
Wed, 17 Mar 2021 17:02:32 +0000 (17:02 +0000)
committerAndy Allan <git@gravitystorm.co.uk>
Wed, 17 Mar 2021 17:09:34 +0000 (17:09 +0000)
This avoids a deprecation warning on rails 6.1

app/validators/characters_validator.rb
app/validators/whitespace_validator.rb

index 837e3e6cc89628fe130b52b09816fc73341f54e2..13d08ca55e06e1079f9b44c39ba429e854a06916 100644 (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
index 3032c109eb03c1bae7bbc6fcc16b3546c055dd73..6453742c21fbadf0e08b77434f6a98d1e724f457 100644 (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