GodziLaravel's avatar

Error message when validate null value !

Hello ,

I created this custom validation :

    public function boot()
    {
        Validator::extend('image64', function ($attribute, $value, $parameters, $validator) {
            $type = explode('/', explode(':', substr($value, 0, strpos($value, ';')))[1])[1];
            if (in_array($type, $parameters)) {
                return true;
            }
            return false;
        });

        Validator::replacer('image64', function($message, $attribute, $rule, $parameters) {
            return str_replace(':values',join(",",$parameters),$message);
        });
    }

I'm using it like :

        $rules = [

            'avatar' => 'image64:jpeg,jpg,png'

        ];

it works but when the avatar value is null (when user send the form without uploading image ) it returns me :

line: 28
message: "Undefined offset: 1"

liene 28 :

$type = explode('/', explode(':', substr($value, 0, strpos($value, ';')))[1])[1];
0 likes
1 reply
petrit's avatar
petrit
Best Answer
Level 20

You may use nullable rule. But I think you can validate your request using integrated rules on Laravel.

'avatar' => 'nullable|image|mimes:jpeg,jpg,png'
1 like

Please or to participate in this conversation.