LoginSignup
8
6

More than 5 years have passed since last update.

パスワードリマインダーにカスタムバリデーションを設定する。

Posted at

はじめに

Laravel標準のパスワードリマインダーのパスワードのバリデーションは6文字以上かどうか。
Validationクラスを使って簡単に8文字以上の半角英数記号のみっていうバリデーションをつけたかった。

方針

LaravelにはValidationクラスという便利なものがあるので大いに使っていきまっしょい

コード

そんなわけでコード。例として8文字以上の半角英数記号のみのバリデーションをつける
もとにしたコードは以下。バリデーションチェックするロジックのみ抜き出す。
https://github.com/CodepadME/laravel-tricks/blob/master/app/Controllers/RemindersController.php

ReminderController.php

    /**
     * Handle a POST request to reset a user's password.
     *
     * @return Response
     */
    public function postReset()
    {
        $credentials = Input::only(
            'email', 'password', 'password_confirmation', 'token'
        );
        $pwRule = [
                'password'              => 'required|min:8|regex:/^[!-~]+$/',
                'password_confirmation' => 'required|min:8|same:password|regex:/^[!-~]+$/',
];

        //カスタムバリデーションルールを登録する
        Password::validator(function($credentials){
            list($password, $confirm) = [$credentials['password'], $credentials['password_confirmation']];
            $validation = Validator::make(['password' => $password, 'password_confirmation' => $confirm], $pwRule);

            return $validation->passes();
        });

        //以下続き
    }

終わりに

いやーLaravelって便(ry

8
6
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
8
6