自定义验证在Laravel 5中返回“Method [validateAlphaSpace]不存在”

时间:2015-07-20 12:54:08

标签: php validation 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中注册了提供者......任何人都可以看到有什么问题吗?

1 个答案:

答案 0 :(得分:1)

自定义验证程序类中的方法必须以&#34; validate&#34;为前缀。在您的情况下,请尝试将alpha_space方法重命名为validateAlphaSpace