1
我想註冊新的自定義驗證,但得到這個錯誤:自定義驗證返回「方法[validateAlphaSpace]不存在」在Laravel 5
Method [validateAlphaSpace] does not exist. [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中註冊提供者...任何人都可以看到什麼是問題?
錯誤已經告訴你是怎麼回事, 你必須改變「alpha_space」到「validateAlphaSpace」的方法名稱 那是laravel方式來添加自定義的驗證 –
你可能想看看[自定義驗證規則](http://laravel.com/docs/4.2/validation#custom-validation-rules) –