【发布时间】:2015-10-15 04:04:50
【问题描述】:
这是我的验证服务提供商。
<?php namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Services\Validation\CustomValidation;
class ValidationServiceProvider extends ServiceProvider
{
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
// We don't have to register anything here so we keep this empty!
}
/**
* Boot the service provider.
*/
public function boot()
{
// Need to override the default validator with our own validator
// We can do that by using the resolver function
$this->app->validator->resolver(function ($translator, $data, $rules, $messages) {
// This class will hold all our custom validations
return new CustomValidation($translator, $data, $rules, $messages);
});
}
}
这是扩展 Laravel 验证器类的自定义验证类。我对应该在哪里为自定义规则编写错误消息感到困惑。我可以找到它的任何文档。
<?php namespace App\Services\Validation;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Validator;
class CustomValidation extends Validator
{
/**
* $attribute Input name
* $value Input value
* $parameters Table, field1
*/
public function validateCustomRule($attribute, $value, $parameters)
{
// Logic to be written later
return false;
}
}
【问题讨论】:
-
您是否尝试过使用相同的约定将它们添加到验证语言文件中?
'custom_rule' => 'Some message' -
@nathanmac,解决了我的问题! :) 你能回答吗,这样我就可以接受了。
标签: laravel