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 Convert Date String to Datetime Object
- Laravel Validation Different Value Example
- Jquery Redirect to URL After Specific Time Example
- User Roles and Permissions in Laravel Example
- How to Get Value of Selected Option in Vue JS?
- Laravel Change Password OLD Password Validation Example
- Vue JS Get String Length Example
- How to Active and Inactive Status in Laravel?