Skip to content

Instantly share code, notes, and snippets.

@gomo
Last active February 5, 2018 03:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gomo/a344768998361fa2475db8c18db9608e to your computer and use it in GitHub Desktop.
Save gomo/a344768998361fa2475db8c18db9608e to your computer and use it in GitHub Desktop.
Array length validator for rails
# app/validators/array_length_validator.rb
class ArrayLengthValidator < ActiveModel::Validations::LengthValidator
def initialize(options)
ActiveModel::Validations::LengthValidator::MESSAGES.each do |key, value|
options[value] = I18n.t("errors.messages.array_#{value}")
end
super(options)
end
end
# config/locale/en.yml
en:
errors:
format: "%{message}"
messages:
array_too_long:
one: "Do not select %{attribute}."
other: "You can select up to %{count} %{attribute}."
array_too_short:
one: "Select %{attribute}."
other: "Select at least %{count} %{attribute}."
array_wrong_length: "Select %{count} %{attribute}."
# config/locale/ja.yml
ja:
errors:
format: "%{message}"
messages:
array_too_long:
one: "%{attribute}は選択しないでください。"
other: "%{attribute}は、最高%{count}個まで選択可能です。"
array_too_short:
one: "%{attribute}を選択してください。"
other: "%{attribute}は最低%{count}個以上選択してください。"
array_wrong_length: "%{attribute}は%{count}個選択してください。"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment