Custom Request does not redirect Laravel 5.4

0

I'm implementing a system and I've created a request through the command:
php artisan make:request PreventivaRequest Home I put my rules in Request:

<?php

namespace App\Http\Controllers\Tecnologia\Preventivas\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StorePreventiva extends FormRequest
{
    /**
     * Redirect route when errors occur.
     *
     * @var string
     */
    protected $redirectRoute = 'preventiva.novo';

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

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'user_id' => 'required',
            'nome_computador' => 'required',
            'ip' => 'required',
            'so_id' => 'required'
        ];
    }
}

And I used the request in my controller:

<?php

namespace App\Http\Controllers\Tecnologia\Preventivas;

use Aplicredi\Repositories\Sistema\Usuarios\IUserRepository;
use Aplicredi\Repositories\Tecnologia\Preventiva\IPreventivaRepository;
use Aplicredi\Repositories\Tecnologia\Preventiva\IPreventivaSoRepository;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Tecnologia\Preventivas\Requests\StorePreventiva;
use App\Http\Controllers\Tecnologia\Preventivas\Requests\UpdatePreventiva;
use Illuminate\Support\Facades\Auth;

/**
 * Class PreventivasController
 * @package Http\Controllers\Tecnologia\Preventivas
 */
class PreventivasController extends Controller
{
    /**
     * @var IPreventivaRepository
     */
    protected $preventivaRepository;

    /**
     * @var IPreventivaSoRepository
     */
    protected $preventivaSoRepository;

    /**
     * @var IUserRepository
     */
    protected $userRepository;

    /**
     * PreventivasController constructor.
     * @param IPreventivaRepository $preventivaRepository
     * @param IPreventivaSoRepository $preventivaSoRepository
     */
    public function __construct(
        IPreventivaRepository $preventivaRepository,
        IPreventivaSoRepository $preventivaSoRepository,
        IUserRepository $userRepository
    )
    {
        $this->preventivaRepository = $preventivaRepository;
        $this->preventivaSoRepository = $preventivaSoRepository;
        $this->userRepository = $userRepository;
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function novo()
    {
        $user = $this->userRepository->find(Auth::user()->id);

        return view('Tecnologia.Preventivas.novo', compact('user'));
    }

    /**
     * @param StorePreventiva $request
     */
    public function store(StorePreventiva $request)
    {

    }

    /**
     * @param UpdatePreventiva $request
     */
    public function update(UpdatePreventiva $request)
    {

    }
}

But when I do the post to the action store it returns me an Exception:

Illuminate \ Validation \ ValidationException
The given data failed to pass validation.


And it does not redirect to the form so I can display validation errors for it.

    
asked by anonymous 21.09.2017 / 16:44

1 answer

0

One detail I had not mentioned (even though I did not think it would be related) was that I was using the Exceptions from Whoops next to Laravel 5.4.

Somehow, Whoops changed Laravel's default return behavior when using a Custom Request.

To resolve the situation and continue to use Whoops I changed the render () method of the app/Exceptions/Handler.php file as follows:

public function render($request, Exception $exception)
{
    if (!$exception instanceof ValidationException) {
        if (config('app.debug') && config('app.env') == 'local') {
            return $this->renderExceptionWithWhoops($request, $exception);
        }
    }

    return parent::render($request, $exception);
}

In this way Laravel returned to work correctly, returning and redirecting in case of non-validation of the form.

    
21.09.2017 / 19:45