]> git.openstreetmap.org Git - rails.git/blob - app/validators/whitespace_validator.rb
Merge remote-tracking branch 'upstream/pull/2786'
[rails.git] / app / validators / whitespace_validator.rb
1 class WhitespaceValidator < ActiveModel::EachValidator
2   def validate_each(record, attribute, value)
3     unless options.fetch(:leading, true)
4       record.errors[attribute] << (options[:message] || I18n.t("validations.leading_whitespace")) if /\A\s/.match?(value)
5     end
6
7     unless options.fetch(:trailing, true)
8       record.errors[attribute] << (options[:message] || I18n.t("validations.trailing_whitespace")) if /\s\z/.match?(value)
9     end
10   end
11 end