LoginSignup
2
0

More than 3 years have passed since last update.

Laravel バリデーションを実装したら403エラーが帰ってきた

Last updated at Posted at 2021-04-25

目的

  • Laravelで新規のRequestクラスを作成してバリデーションを実装したら403エラーが返されたので何が間違えていたかメモ的にまとめる

原因

  • Requestクラスの中のauthorize()メソッドの戻り値がfalseのままだった。

    Request.php
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return false;
    }
    
  • 「Determine if the user is authorized to make this request.」と記載されており、翻訳すると「ユーザーがこの要求を行うことを許可されているかどうかを判別します。」と説明されている。

  • ユーザーからPostされたデータをバリデートしたい時は当該メソッドの戻り値をtrueに設定する必要がある。

    Request.php
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }
    

教えていただいた内容

  • @nunulk様にコメントで教えていただきました。
  • ユーザーによる要求の許可の必要がない場合authorize()メソッドを削除してしまっても良いそうです!
2
0
1

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
2
0