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
– novic
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 ...
– novic
@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?– David Santos
I was using the https://github.com/filp/whoops library and somehow this changed the default behavior of Laravel.
– David Santos