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 5 years have passed since last update.

バリデーションのformatヘルパー の使い方

0
Last updated at Posted at 2021-04-09

目次

①バリデーションヘルパーとは
②formatの使い方

①バリデーションヘルパーとは

Active Recordには、クラス定義の内側で直接使える定義済みのバリデーションヘルパーが多数用意されている。ヘルパーは、共通のバリデーションルールを提供をし、バリデーションが失敗するたびに、オブジェクトのerrorsコレクションにエラーメッセージが追加され、そのメッセージは、バリデーションが行われる属性に関連付けられる。

②formatの使い方

formatヘルパーは、withオプションで与えられた正規表現と属性の値がマッチするかどうかのテストによる検証を行う。

Userモデル
class User < ApplicationRecord
validates :email, presence: true, length: { maximum: 100 },
                    format: { with:/\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i , message: "は英文字のみが使えます", }

ちなみに「message:」はエラーが起きたい際にエラーメッセージとして表示されるものである。

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?