1
Good afternoon, I need to validate a Dropdown so that if it exists the values 1, 2, 3 the user can not enter other values other than those contained in it.
I managed to do in Alunoupdaterequest the following:
public function rules()
{
return [
'classe_id' => 'required|in:' . $this->aluno->classe_id,
In my Alunocontroller.php is that way:
public function update(AlunoUpdateRequest $request, Aluno $aluno)
{
$aluno->update($request->all());
Session::flash('notice', 'Atualizado Com Sucesso !');
return Redirect::to('/aluno');
}
The only problem is that I’m not being able to use the same form in the creation of the Alunocreaterequest:
public function rules()
{
return [
'classe_id' => 'required|in:' . $this->aluno->classe_id,
The code is the same. How to solve ?
UPDATING
Guys, I managed to solve using the function "exists": https://laravel.com/docs/5.2/validation#Rule-exists
You could put more information, usually the creation of a Request Validator is sufficient!
– user46523
Do these values come from a table? What are they? You have to give more information to help us
– Miguel
They come from a database table. @Miguel
– Samuel Corrêa Dos Santos
@João, the Request validator has already been created.
– Samuel Corrêa Dos Santos
Exactly, it’s already been created. You can:
classe_id => 'required|exists:classes,id'
... Checks the existence of the item in the table classes, column id, in this case. I think it’s any like that you want– Miguel
So Miguel, the function that Laravel himself provides is "in"; what you said is valid, but I believe that there has to be some way to do this in Request without the need to create a function got ? @Miguel
– Samuel Corrêa Dos Santos
Can’t. The closest you can is to do this check in middleware. Can’t do it in the request
– Miguel
I just got it. I used the "exists" validation of Laravel
– Samuel Corrêa Dos Santos