Matt Pierce
Matt Pierce

Reputation: 805

Laravel Custom Error Message Not Returning Correctly

I have created a new custom rule using a service provider like so

public function boot()
{
    $this->app['validator']->extend('googleUrl', function($attribute, $value, $parameters, $messages)
    {
        $url = $value;
        $google_haystack = array('https://www.google.com', 'https://google.com');

            // Check the user's input against each array value

            foreach ($google_haystack as $google_haystack)
            {
                if (strpos($url, $google_haystack) !== FALSE)
                {
                    return TRUE;
                }
                return FALSE;
            }   
    });
}

The rule works as it should, but when the error message displays, it just displays as "validation.google_url". So, in my validation.php file I have it defined, but it still just returns the previous error message, not my custom message.

/*
|--------------------------------------------------------------------------
| Custom Validation Language Lines
|--------------------------------------------------------------------------
|
| Here you may specify custom validation messages for attributes using the
| convention "attribute.rule" to name the lines. This makes it quick to
| specify a specific custom language line for a given attribute rule.
|
*/

'custom' => [
    'attribute-name' => [
        'rule-name' => 'custom-message',
    ],
    'validation.google_url' => [
        'googleUrl' => 'You must enter a valid Google URL.',
    ],
],

Upvotes: 0

Views: 81

Answers (1)

thefallen
thefallen

Reputation: 9749

The message should be placed in the first level of the array, not within the custom array, which is only for attribute-specific error messages.

Upvotes: 1

Related Questions