In the update function, you would have to check the $input['hero_image']
and $input['picture']
variables if they are different from ''
. If they are == ''
(equal to), then you need to change your validator or at least its rules dynamically to exclude the 'hero_image' rule. I have this in my code at one point and have different $rules
defined in my model for if the input field is present and for when it's submitted empty (= unchanged).
I just found another, way easier and cleaner way to do it:
Laravel provides the ->sometimes()
validator modifier (link). After you define your validator, do this:
$validation->sometimes('hero_image', 'required', function($input)
{
return !empty($input->hero_image); // If the submitted field is empty, return FALSE to NOT include this validation rule
});
$validation->sometimes('picture', 'required', function($input)
{
return !empty($input->hero_image);
});
// THEN, call the $validator->passes() function
if($validation->passes())
{
...
You also need to remove the 'hero_image'
rule from your Post
model.
// Eat. Sleep. Code. Repeat.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community