Support the ongoing development of Laravel.io →
Input Forms Validation
Last updated 2 years ago.
0

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).

Last updated 2 years ago.
0

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.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

mdunbavan mdunbavan Joined 31 Jan 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.