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 1 year has passed since last update.

独自のバリデーションが追加できなかった理由

Posted at

独自のバリデーション追加ができない

「You need to supply at least one validation」

のエラーが発生していた。

調べたら、カスタムしたメソッドは単数形にしないといけないみたい。

「validates」を「validate」に変更したらできた。

大まかなバリデーションは複数形で、プライベートで細かく追加したら単数形になる感じ?

訂正して成功したコード↓

contact.rb
class Contact < ApplicationRecord
    validates :name, presence: true

    validate :email_format
    #↑をvalidatesにしていた

    private

    def email_format
        return if /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i.match?(email)
        
        errors.add(:email, "の形式が正しくありません")
    end


end
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?