0
0

More than 3 years have passed since last update.

laravel 任意のフィールドの値が指定したものと一致していない時に除外するバリデーションルールを記載する方法

Posted at

目的

  • laravelで任意のフィールドの値が指定したものと一致していない時に当該フィールドのバリデートをスキップする方法をまとめる

方法

  • フィールドcheckの値に「true」という文字列以外が格納されたとき以外に、フィールドinput_strの値のバリデーションをスキップしたい時は下記のように記載する。
  • input_strの値のバリデーションルールは「必須」と「文字列であること」とする。
  • 下記はリクエストファイルのrules()メソッドの内容のみ抜粋して記載する。

    HogeRequest.php
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'check' => 'required',
            'input_str' => 'exclude_unless:check,true|required|string',
        ];
    }
    
  • 任意のフィールドの値が指定したものと一致していない時のみ除外してほしいバリデーションルールの頭にexclude_unless:フィールド名,指定された値を記載する。

参考文献

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