Validation "in" Laravel 5

Asked

Viewed 107 times

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!

  • Do these values come from a table? What are they? You have to give more information to help us

  • They come from a database table. @Miguel

  • @João, the Request validator has already been created.

  • 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

  • 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

  • Can’t. The closest you can is to do this check in middleware. Can’t do it in the request

  • I just got it. I used the "exists" validation of Laravel

Show 3 more comments
No answers

Browser other questions tagged

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