E
E
Evgeny Romashkan2018-06-07 18:01:13
Laravel
Evgeny Romashkan, 2018-06-07 18:01:13

Why doesn't laravel see my validation error message?

Here is such a class:

spoiler
<?php

namespace App\Rules;

use App\Inspections\Spam;
use Illuminate\Contracts\Validation\Rule;

class SpamFree implements Rule
{
    /**
     * Create a new rule instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        try{
            return ! resolve(Spam::class)->detect($value);
        } catch(\Exception $e) {
            return false;
        }
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return 'Custom msg.';
    }
}


Added a line to AppServiceProvider boot():
\Validator::extend('spamfree', 'App\Rules\[email protected]');

And used like this:
this->validate(request(), ['body' => 'required|spamfree']);

However, instead of an error message, this is displayed: "validation.spamfree".
And if you add it to resources/lang/en , everything works. What am I doing wrong, why is the message not being output from the message() method? 'spamfree' => 'Custom msg',

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
JhaoDa, 2018-06-07
@EvgeniiR

Have you tried using the rule as suggested in the documentation?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question