Laravel 5.1:自定义验证器,带有自定义错误消息

时间:2015-07-14 06:48:33

标签: php validation laravel laravel-5

我是Laravel的新手,我尝试构建一个应用程序,在某种程度上,我有一个表单,我有一个数组字段集,允许最终用户输入他喜欢的尽可能多的信息。

我的表单字段就是这样:

<div class="rule">
    <div class="target">

        <select name="rule[0][0][target]" id="target_NSBYS" class="form-control">
            <option value="url">URL</option>
            <option value="referrer">Referrer</option>
        </select>
    </div>
    <div class="condition">
        <select name="rule[0][0][condition]" id="condition_NSBYS" class="form-control">
            <option value="equals">Equals</option>
            <option value="contains">Contains</option>
            <option value="starts_with">Starts With</option>
            <option value="ends_with">Ends With</option>
            <option value="regex">Matches RegExp</option>
            <option value="not_equal">Doesn't Equal</option>
            <option value="not_contains">Does not contain</option>
            <option value="not_starts_with">Does not start with</option>
            <option value="not_ends_with">Does not end with</option>
            <option value="not_regex">Does not match RegExp</option>
        </select>
    </div>
    <div class="rule">
        <input type="text" id="rule_NSBYS" name="rule[0][0][rule]" placeholder="Rule value" value="" class="form-control">
    </div>
    <div class="actions">
        <a href="javascript:void(0)" class="removeRule btn btn-sm btn-default">-</a>
        <a href="javascript:void(0)" class="addRule btn btn-sm btn-default">+</a>
    </div>
</div>

所以,在后端我注册了一个新的服务提供者,以便在这个类中注册我的所有自定义验证规则。

服务提供商看起来像这样:

<?php
namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Validator;

class CustomValidationRules extends ServiceProvider
{
    public function boot()
    {
        Validator::extend(
            'campaign_rules',
            function ( $attribute, $value, $parameters ) {

                $valid = true;

                foreach ( $value as $group_id => $group_fields ) {

                    foreach ( $group_fields as $field_id => $field_set ) {
                        $result = Validator::make(
                            $field_set,
                            array(
                                'target'    => 'required|in:url,referrer',
                                'condition' => 'required|in:equals,contains,starts_with,ends_with,regex,not_equal,not_contains,not_starts_with,not_ends_with,not_regex',
                                'rule'      => 'required'
                            ),
                            array(
                                'target.required' => sprintf(
                                    'The target field in the %d rules group, in the %d rule it is required',
                                    $group_id,
                                    $field_id
                                ),
                                'target.in' => sprintf(
                                    'The target field in the %d rules group, in the %d rule contains a wrong value',
                                    $group_id,
                                    $field_id
                                ),
                                'condition.required' => sprintf(
                                    'The conditional field in the %d rules group, in the %d rule it is required',
                                    $group_id,
                                    $field_id
                                ),
                                'condition.in' => sprintf(
                                    'The conditional field in the %d rules group, in the %d rule contains a wrong value',
                                    $group_id,
                                    $field_id
                                ),
                                'rule.required' => sprintf(
                                    'The rule field in the %d rules group, in the %d rule can\'t be empty',
                                    $group_id,
                                    $field_id
                                )
                            )
                        );

                        if ( $result->fails() ) {
                            $valid = false;

                            break 1;
                            break 2;
                        }
                    }
                }

                return $valid;
            }
        );
    }
}

此外,我还有一个自定义请求类来验证我的表单数据,并包含以下代码:

<?php

namespace App\Http\Requests;

use App\Eb\Helpers;
use App\Http\Requests\Request;

class CampaignValidation extends Request
{
    public function authorize()
    {
        if ( Helpers::is_super_admin() || Helpers::is_client() ) {
            return true;
        }

        return false;
    }

    public function rules()
    {
        return [

            'name'             => 'required|max:255',
            'status'           => 'required|in:active,paused,archived',
            'publication_date' => 'required|date_format:Y-m-d H:i',
            'expiration_date'  => 'required|date_format:Y-m-d H:i',
            'mode'             => 'required|in:normal,aggressive',
            'rule'             => 'campaign_rules'

        ];
    }
}

因此,通过使用此代码,我可以正常验证我的代码,但不幸的是,验证过程后得到的错误消息如下所示:

Errors occurred
    validation.campaign_rules

这并不好。正如您已经看到的,在服务提供者中,当我使用Validator :: make时,我使用第三个参数,以便返回正确的错误消息。

所以问题是,如何在我的应用程序前面发送此自定义错误消息?

1 个答案:

答案 0 :(得分:2)

尝试覆盖FormRequest中的messages()方法,如下所示:

public function messages()
{
    return [
        'validation.campaign_rules' => 'My custom validation message',
    ];
}