Last active
February 5, 2018 03:41
-
-
Save gomo/a344768998361fa2475db8c18db9608e to your computer and use it in GitHub Desktop.
Array length validator for rails
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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}." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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