Laravel Request validation exists and nullable at the same time

Asked

Viewed 198 times

0

I created a request class and inject it into the store method of the controler. My Rules method within the class has the following key:

public function rules()
{
    return [
    //...
    'campo_id' => 'sometimes|nullable|exists:tabela,pk',
    //...
    ];
}

when the value is null and enters this validation, it falls into the exists validation and returns the corresponding message. My intention is to leave null itself. Any suggestions?

1 answer

0

You can use it that way

    public function rules()
{
    return [
    //...
    'campo_id' => 'nullable|exists:tabela,pk',
    //...
    ];
}
  • I see that nullable allows the field to be null, but exists forces you to have something. It didn’t work for me here. Vlw by force!

  • ta using in an api ?

  • No friend, I’m using it the way I put it in the post. No api

  • I am using this way with api and this run.

  • I found the problem, it was in a Istener. It really works!

  • So congratulations and that

  • Put as answered there to help =D

  • 1

    Vote and acceptance should not be given for passionate reasons. Vote as useful not useful. And it is accepted, preferably among the useful, the best answer.

Show 3 more comments

Browser other questions tagged

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