0
votes

I've created a request for my update method called CandidateProfileUpdateRequest.php:

public function authorize()
{
    return true;
}

/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    return [
        'photo' => ['mimes:jpeg,png,jpg,gif,bmp', 'max:4096'],
        'video_one' => ['mimes:mp4,mov,ogg,qt', 'max:30720'],
        'video_two' => ['mimes:mp4,mov,ogg,qt', 'max:30720'],
        'video_three' => ['mimes:mp4,mov,ogg,qt', 'max:30720'],
        'resume' => ['mimes:doc,docx,pdf', 'max:4096'],
        'job_title' => ['required'],
    ];
}

public function messages()
{
    return [
        'photo.max' => 'The photo may not be greater than 4MB.',
        'video_one.max' => 'The video may not be greater than 30MB.',
        'video_two.max' => 'The video may not be greater than 30MB.',
        'video_three.max' => 'The video may not be greater than 30MB.',
        'resume.max' => 'The resume may not be greater than 4MB.',
    ];
}

For these 4 fields that aren't required photo, video_one, video_two, video_three, I only want to apply these rules, if a file is being uploaded in either of these form fields.

So for example if video_two is empty i.e. the User isn't uploading anything here, and clicks Update, it shouldn't return any rules for video_two. Is this possible?

2
have you tried using the nullable rule?lagbox

2 Answers

0
votes

Check out the sometimes rule.

In some situations, you may wish to run validation checks against a field only if that field is present in the data being validated. To quickly accomplish this, add the sometimes rule to your rule list:

/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    return [
        'photo' => ['mimes:jpeg,png,jpg,gif,bmp', 'max:4096'],
        'video_one' => ['mimes:mp4,mov,ogg,qt', 'max:30720'],
        'video_two' => ['sometimes', 'mimes:mp4,mov,ogg,qt', 'max:30720'],
//                      ^^^^^^^^^^^
        'video_three' => ['mimes:mp4,mov,ogg,qt', 'max:30720'],
        'resume' => ['mimes:doc,docx,pdf', 'max:4096'],
        'job_title' => ['required'],
    ];
}
0
votes

The sometimes rule didn't work. Thank you to lagbox, the nullable rule worked!