I have a form to create a Product. The product has two images. The first image is required. When I create a Product I set the validation rule required for the first image. I dont know what to do when editing the product. How can I validate it? Because if the request hasnt the file maybe it was already uploaded when the Product was created so it shouldnt return errors. How should I handle this case? Im looking for the cleanest and laravel way.
1
votes
You would have your controller make a call to check if the product already has an image, then conditionally display an error. If you post the code you are currently using, I/we can suggest how to modify it to incorporate this change
- tam5
Make separate rules for creating and editing the product. You can see an example of it here
- Doom5
1 Answers
1
votes
While editing product, you can remove rule on base of product id
In your own Requests class
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules() {
$rules = [
'title' => 'required',
'description' => 'required',
];
if (!($this->request->get('product_id'))) {
$rules['product_image'] = 'required';
}
return $rules;
}