自定义验证返回“方法[validateAlphaSpace]不存在”;在Laravel 5


Custom Validation returns "Method [validateAlphaSpace] does not exist" in Laravel 5

我正在尝试注册新的自定义验证,但得到这个错误:

方法[validateAlphaSpace]不存在。(Laravel 5)

下面是我的代码:

CustomValidator.php

<?php namespace App;
use Illuminate'Validation'Validator;
class CustomValidator extends Validator {
    public function alpha_space($attribute, $value, $parameters)
    {
        return preg_match('/^[a-zA-Z0-9's]+$/u', $value);
    }
}

ValidatorServicePorvider.php

<?php namespace App'Providers;
use Illuminate'Support'ServiceProvider;
use Illuminate'Support'Facades'Validator;
use App'CustomValidator;
class ValidatorServicePorvider extends ServiceProvider {
 /**
  * Bootstrap any necessary services.
  *
  * @return void
  */
 public function boot()
 {
     Validator::resolver(function($translator, $data, $rules, $messages)
                        {
                            return new CustomValidator($translator, $data, $rules, $messages);
                        });
 }
 /**
  * Register the service provider.
  *
  * @return void
  */
 public function register()
 {
 }
}

我也注册提供商在app.php…有人能看出是什么问题吗?

方法中的自定义验证器类必须以"validate"作为前缀。在您的情况下,尝试将alpha_space方法重命名为validateAlphaSpace