Laravel Validation required if other field empty example

August 13, 2018 | Category : Laravel 5.6 Laravel 5.5 Laravel 5 Laravel PHP

We know laravel provide several in-built laravel validation like required_if, required_without etc. If you need to add validation rules like required if other field is empty in laravel then you can do it using required_if or required_without.

I am going to explain you, If you give profile image uploading in your user profile page then if already added profile image then you do not need to upload every time image, So at that time you can add validation required_if or required_without.

So, you can use this way:

'image' => 'required_if:image_old,'

--OR--

'image' => 'required_without:image_old,'

You can see below validation method example and use this way as i added method example:

required_if:

public function googleFormPost(Request $request)

{

$request->validate([

'name' => 'required',

'image' => 'required_if:image_old,'

]);

dd("Done!");

}

required_without:

public function googleFormPost(Request $request)

{

$request->validate([

'name' => 'required',

'image' => 'required_without:image_old,'

]);

dd("Done!");

}

I hope you found your best....