0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

7桁の数字入力に制限するバリデーションnumericality

Posted at

railsガイド

https://railsguides.jp/active_record_validations.html

参考に行いました

class Player < ApplicationRecord
  validates :points, numericality: true
  validates :games_played, numericality: { only_integer: true }
end
オプション 働き 表示メッセージ
greater_than 指定された値よりも大きくなければならない 「must be greater than %{count}」
greater_than_or_equal_to 指定された値と等しいか、それよりも大きくなければならない 「must be greater than or equal to %{count}」
equal_to 指定された値と等しくなければならない 「must be equal to %{count}」
less_than 指定された値よりも小さくなければならない 「must be less than %{count}」
less_than_or_equal_to 指定された値と等しいか、それよりも小さくなければならない 「must be less than or equal to %{count}」
other_than 渡した値以外の値でなければならない 「must be other than %{count}」
odd trueの場合は奇数でなければない 「must be odd」
even trueの場合は偶数でなければない 「must be even」です。

自アプリに転用

models/item.rb
validates :price, numericality:{only_integer: true, greater_than_or_equal_to: 0, less_than_or_equal_to: 9999999 }
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?