Custom Request does not redirect Laravel 5.4

Asked

Viewed 154 times

0

I am implementing a system and realized the creation of a request through the command:
php artisan make:request PreventivaRequest
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 post to the action store it returns to me an Exception:

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


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

  • Related: https://stackoverflow.com/questions/34610845/laravel-validator-throws-an-exception-instead-of-redirecting-back

  • Another factor, if you create a Request with a name and the class with another Name, always try to create everything with the same name ...

  • @Virgilionovic, I’m sorry, but I couldn’t find a match on the link you reported. Really if I were doing the validations on the controller itself I could use the Redirect::back(), but I am using Formrequest, another detail that you quoted me about the name of the request and the class, which would influence?

  • I was using the https://github.com/filp/whoops library and somehow this changed the default behavior of Laravel.

1 answer

0


One detail I hadn’t mentioned (because I didn’t think it would be related) was that I was using the Whoops by the Laravel 5.4.

Somehow Whoops changed the default return behavior of Laravel when using a Custom Request.

To resolve the situation and continue using Whoops I changed the render() method of the file app/Exceptions/Handler.php 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);
}

This way Laravel returned to work correctly, returning and redirecting in case of not validating the form.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.