34
24

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 5 years have passed since last update.

FormRequestのバリデーションエラー時のリダイレクト先をカスタマイズする

Posted at

LaravelにはFormRequestというクラスがあり、これを継承したクラスにルールを指定しておくと、うまいことバリデーションをしてくれます。

StoreBlogPostRequest.php
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StoreBlogPostRequest extends FormRequest
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'title' => 'required|unique:posts|max:255',
            'body' => 'required',
        ];
    }
}

通常、バリデーションに引っかかると、リクエストを送ってきた前のURLにリダイレクトされますが、ここをカスタマイズすることが出来ます。

FormRequestクラスを読んでみると、getRedirectUrlという関数がありそこで呼ばれているプロパティを設定する、もしくはその関数自体をオーバーライドすることでリダイレクト先を変更することが出来ることがわかります。

FormRequest.php
<?php

namespace Illuminate\Foundation\Http;

use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Validation\ValidationException;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Validation\ValidatesWhenResolvedTrait;
use Illuminate\Contracts\Validation\ValidatesWhenResolved;
use Illuminate\Contracts\Validation\Factory as ValidationFactory;

class FormRequest extends Request implements ValidatesWhenResolved
{


... 省略 ...


    /**
     * Get the URL to redirect to on a validation error.
     *
     * @return string
     */
    protected function getRedirectUrl()
    {
        $url = $this->redirector->getUrlGenerator();

        if ($this->redirect) {
            return $url->to($this->redirect);
        } elseif ($this->redirectRoute) {
            return $url->route($this->redirectRoute);
        } elseif ($this->redirectAction) {
            return $url->action($this->redirectAction);
        }

        return $url->previous();
    }


... 省略 ...


}

よって、以下のようにすればリダイレクト先をカスタマイズできます。

StoreBlogPostRequest.php
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StoreBlogPostRequest extends FormRequest
{
    protected $redirect = 'リダイレクトさせたいパス';
    protected $redirectRoute = 'リダイレクトさせたいroute';
    protected $redirectAction = 'リダイレクトさせたいAction';

    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'title' => 'required|unique:posts|max:255',
            'body' => 'required',
        ];
    }

    protected function getRedirectUrl()
    {
        return 'リダイレクトさせたいURL';
    }

}

34
24
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
34
24

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?