KodeFor.Me
KodeFor.Me

Reputation: 13511

Laravel 5.1: Custom validatorwith custom error messages

I am new on Laravel, and I try to build an application, that in some point, I have a form, in which I have an array field set, to allow the end user to enter as much information as he like.

My form field is like that :

<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>

So, in the back end I have register a new service provider, in order to register all of my custom validation rules inside this class.

The service provider looks like that :

<?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;
            }
        );
    }
}

Also I have a custom request class to validate my form data and contains the following code:

<?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'

        ];
    }
}

So by using this code I can validate normaly my code, but unfortunatelly, the error message I get after the validation process is like the following one:

Errors occurred
    validation.campaign_rules

and this is not nice. As you already have seen, in the service provider, when I use the Validator::make I use the third parameter, in order to return the correct error messages.

So the question is, how can I send this custom error messages at the front of my application ?

Upvotes: 1

Views: 1731

Answers (1)

rzb
rzb

Reputation: 2153

Try overriding the messages() method in your FormRequest, like so:

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

Upvotes: 2

Related Questions