【问题标题】:Laravel error messages for extended custom validator class扩展自定义验证器类的 Laravel 错误消息
【发布时间】: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' =&gt; 'Some message'
  • @nathanmac,解决了我的问题! :) 你能回答吗,这样我就可以接受了。

标签: laravel


【解决方案1】:

您是否尝试过使用相同的约定将它们添加到验证语言文件中? 'custom_rule' => '一些信息'

【讨论】:

    猜你喜欢
    • 2015-07-13
    • 1970-01-01
    • 2018-04-04
    • 2015-10-02
    • 2017-05-30
    • 1970-01-01
    • 2020-02-26
    • 2021-02-24
    • 2011-10-12
    相关资源
    最近更新 更多