LoginSignup
8
6

More than 5 years have passed since last update.

elixir on phoenix changesetでのValidates

Last updated at Posted at 2016-09-28

Validates

validate_required(必須チェック)

@spec validate_required(t, list | atom, Keyword.t) :: t

Examples

validate_required(changeset, :title)
validate_required(changeset, [:title, :body])

Options

# `:message` - エラーメッセージ、デフォルトは "can't be blank"

validate_required(changeset, [:title, :body], [message: "入力してよ!"])

validate_format(形式チェック)

@spec validate_format(t, atom, Regex.t, Keyword.t) :: t

Examples

validate_format(changeset, :email, ~r/@/)

Options

# `:message` - エラーメッセージ、デフォルトは "has invalid format"

validate_required(changeset, [:title, :body], [message: "フォーマットが違うよ"])

validate_inclusion(有効性のチェック)

@spec validate_inclusion(t, atom, Enum.t, Keyword.t) :: t

Examples

validate_inclusion(changeset, :gender, ["man", "woman", "other", "prefer not to say"])
validate_inclusion(changeset, :age, 0..99)

Options

# `:message` - エラーメッセージ、デフォルトは to "is invalid"

validate_inclusion(changeset, :age, 0..99, [message: "無効な値だよ"])

validate_subset(有効性のチェック:enum型)

@spec validate_subset(t, atom, Enum.t, Keyword.t) :: t

Examples

validate_subset(changeset, :pets, ["cat", "dog", "parrot"])
validate_subset(changeset, :lottery_numbers, 0..99)

Options

#  `:message` - エラーメッセージ、デフォルトは "has an invalid entry"

validate_inclusion(changeset, :lottery_numbers, 0..99, [message: "無効な値だよ"])

validate_exclusion(使用できるか)

@spec validate_exclusion(t, atom, Enum.t, Keyword.t) :: t

Examples

validate_exclusion(changeset, :name, ~w(admin テスト))

Options

#  `:message` - エラーメッセージ、デフォルトは to "is reserved"

validate_exclusion(changeset, ~w(テスト superadmin), [message: "それは使えません!!"])

上記の場合だとnameに「テスト」か「superadmin」のどちらかを入力するとエラーになります。

validate_length(桁数のチェック)

@spec validate_length(t, atom, Keyword.t) :: t

Examples

validate_length(changeset, :title, [min: 3])
validate_length(changeset, :title, [max: 100])
validate_length(changeset, :title, [min: 3, max: 100])
validate_length(changeset, :code, [is: 9])

Options

# `:is` - 桁数
# `:min` - 最小桁数
# `:max` - 最大桁数

#  `:message` - エラーメッセージ、デフォルトは 
# * "should be %{count} character(s)"
# * "should be at least %{count} character(s)"
# * "should be at most %{count} character(s)"

validate_length(changeset, :name, [min: 2, message: "%{count}桁以上で入力してね!"])
validate_length(changeset, :name, [max: 3, message: "%{count}桁以下で入力してね!"])
validate_length(changeset, :name, [is: 2, message: "%{count}桁で入力してね!"])

オプションでminとmaxを指定した時のメッセージの切り替え方がよくわからない。。
なので個別でやるしかないのだろうか。。

validate_number(桁数の数字)

@spec validate_number(t, atom, Keyword.t) :: t | no_return

Examples

validate_number(changeset, :count, less_than: 3)
validate_number(changeset, :pi, greater_than: 3, less_than: 4)
validate_number(changeset, :the_answer_to_life_the_universe_and_everything, equal_to: 42)

Options

# `:less_than` - <
# `:greater_than` - >
# `:less_than_or_equal_to` - <=
# `:greater_than_or_equal_to` -  >=
# `:equal_to` - ==

#  `:message` - エラーメッセージ、デフォルトは 
# * "must be less than %{number}"
# * "must be greater than %{number}"
# * "must be less than or equal to %{number}"
# * "must be greater than or equal to %{number}"
# * "must be equal to %{number}"

validate_length(changeset, :age, [equal_to: 2, message: "%{number}桁以上で入力してね!"])

validate_confirmation(確認用)

@spec validate_confirmation(t, atom, Keyword.t) :: t

Examples

validate_confirmation(changeset, :password)

Options

# `:required`  - boolean
#  `:message` - エラーメッセージ、デフォルトは "does not match"

validate_confirmation(:password, [message: "一致しないよ"])
#この場合だとpasswordとpassword_confirmationが一致するかチェックする

validate_change

@spec validate_change(t, atom, (atom, term -> [error])) :: t

自分でチェックを実装したいときはvalidate_changeを使う

Examples

validate_change changeset, :name, fn :name, name  ->
  if name == "elixir" do
    [name: "elixirはだめです"]
  else
    []
  end
end

参考URL
https://hexdocs.pm/ecto/Ecto.Changeset.html

エラーメッセージの日本語化

日本語のpoファイルを用意してconfigに設定すればOKみたいです。

config :sample, Sample.Gettext, default_locale: "ja"

参考URL
「Phoenix」フレームワークで、エラーメッセージの日本語化

リンク

1.elixir on phoenix セットアップ
2.elixir on phoenix Mix Tasksについて
3.elixir on phoenix ectoを使っていろいろなSELECT文
4.elixir on phoenix ルーティングの書き方についてまとめてみた
5.elixir on phoenix changesetでのValidates

8
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
6