phplaravellaravel-validation

Get the length of an array using closure validation


What I am trying to do is to get the length of an array inside closure Laravel validation.

Here is what I have tried:

        'attachment' => 'max:10',
        'attachment.*' => 'max:10240|mimes:jpeg,jpg,pdf',

        'deletedAttachments' => ['nullable', 'array', 'max:10', function($attribute, $value, $fail) {
            $fail(count($this->input('attachment')));
        }],

Unfortunately, This did not work and I am getting the following error:

Illuminate\Translation\PotentiallyTranslatedString::__toString(): Return value must be of type string, null returned


Solution

  • as I remember, Laravel validation should give the data type first, maybe you could try:

    [
    'attachment' => 'array|max:10',
    'attachment.*' => 'file|mimes:jpeg,jpg,pdf|max:10240',
    //...other codes
    ]