如何在Laravel 5.3中创建自定义验证规则

时间:2017-01-20 23:42:01

标签: laravel laravel-5.3 laravel-validation

我正在尝试在laravel 5.3中创建自定义验证规则。在 AppServiceProvider 中,我像这样扩展了Validator

Validator::extend('image64',function ($attribute,$value,$parameters,$validator){
        $type = explode(',',$value)[0];
        if (stripos($type, "image/jpeg") !== false || stripos($type, "image/jpg") !== false || stripos($type, "image/jpeg") !== false) {
            return true;
        }
        return false;
    });

并在 validation.php

'custom' => [
    'base64' => 'The :attribute must be an jpeg/jpg/png',
    'attribute-name' => [
        'rule-name' => 'custom-message',
    ],
],

我正在我的控制器中使用它。

$validator = Validator::make($request->all(), [
            'name' => 'required|max:255|unique:industries',
            'image' => 'required|base64'
        ]);

但我收到错误

"Method [validateBase64] does not exist."

0 个答案:

没有答案