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....
Do you like below Tutorials ?
- PHP Laravel 5.5 - import export data into excel and csv using maatwebsite
- Laravel 7.x and 6.x create custom facade example from scratch
- Node JS - Get Real time online users counter using socket io example
- PHP Laravel 7.x and 6.x - Bootstrap Admin Theme Integration example from scratch
- MySQL Query - How to select records of today to next Sunday ?
- Laravel 7.x and 6.x - username custom validation example
- Laravel 5.5 - Import Extract zip file using chumper/zipper package
- PHP Laravel Set Custom Validation Error Messages Example