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.

【Laravel】バリデーションルールの作成

Last updated at Posted at 2020-07-16

この記事は以下の書籍を参考にして執筆しました。

バリデーションルールの作成

php artisan make:rule Myrule

app/Rules/Myrule.phpに作られる。

app/Rules/Myrule.php
namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class Myrule implements Rule
{
    public function __construct()
    {
        //
    }
    public function passes($attribute, $value)
    {
        //
    }
    public function message()
    {
        return 'The validation error message.';
    }
}

出典:PHPフレームワークLaravel入門 第2版

メソッド 説明
passes ルールの通過条件
message 問題発生時のメッセージを返す

passesの引数は以下2つ

  • $attribute(ルール属性をまとめたもの)
  • value(チェックする値)

passesの戻り値はこちら

戻り値 説明
true 問題ない
false エラー

Myrulesの使用

HelloRequestクラスのruleメソッドを修正

'age'=>['numeric'z,new Myrules(5)]

出典:PHPフレームワークLaravel入門 第2版

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?