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.