Merge leading and trailing whitespace validators
[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 value =~ /\A\s/
5     end
6
7     unless options.fetch(:trailing, true)
8       record.errors[attribute] << (options[:message] || I18n.t("validations.trailing_whitespace")) if value =~ /\s\z/
9     end
10   end
11 end