Converted invalid_chars validator to use locale

This commit is contained in:
J Guthrie 2018-11-05 01:55:25 +00:00
parent 3b68061e87
commit e091246ffc
5 changed files with 8 additions and 7 deletions

View file

@ -2,6 +2,6 @@ class InvalidCharsValidator < ActiveModel::EachValidator
INVALID_CHARS = "\x00-\x08\x0b-\x0c\x0e-\x1f\x7f\ufffe\uffff".freeze
def validate_each(record, attribute, value)
record.errors[attribute] << (options[:message] || "contains invalid chars") if value =~ /[#{INVALID_CHARS}]/
record.errors[attribute] << (options[:message] || I18n.t("validations.invalid_chars")) if value =~ /[#{INVALID_CHARS}]/
end
end

View file

@ -2,6 +2,6 @@ class InvalidUrlCharsValidator < ActiveModel::EachValidator
INVALID_URL_CHARS = "/;.,?%#".freeze
def validate_each(record, attribute, value)
record.errors[attribute] << (options[:message] || I18n.t("validations.invalid chars", :invalid_chars => INVALID_URL_CHARS)) if value =~ /[#{INVALID_URL_CHARS}]/
record.errors[attribute] << (options[:message] || I18n.t("validations.invalid_url_chars", :invalid_url_chars => INVALID_URL_CHARS)) if value =~ /[#{INVALID_URL_CHARS}]/
end
end

View file

@ -1,5 +1,5 @@
class LeadingWhitespaceValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
record.errors[attribute] << (options[:message] || I18n.t("validations.leading whitespace")) if value =~ /\A\s/
record.errors[attribute] << (options[:message] || I18n.t("validations.leading_whitespace")) if value =~ /\A\s/
end
end

View file

@ -1,5 +1,5 @@
class TrailingWhitespaceValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
record.errors[attribute] << (options[:message] || I18n.t("validations.trailing whitespace")) if value =~ /\s\z/
record.errors[attribute] << (options[:message] || I18n.t("validations.trailing_whitespace")) if value =~ /\s\z/
end
end

View file

@ -2563,6 +2563,7 @@ en:
flash: "Redaction destroyed."
error: "There was an error destroying this redaction."
validations:
leading whitespace: "has leading whitespace"
trailing whitespace: "has trailing whitespace"
invalid chars: "must not contain an invalid char: %{invalid_chars}"
leading_whitespace: "has leading whitespace"
trailing_whitespace: "has trailing whitespace"
invalid_chars: "contains invalid chars"
invalid_url_chars: "must not contain an invalid char: %{invalid_url_chars}"