How to pass variables from validation rules to error message in laravel?

时间:2016-04-12 00:55:38

标签: php validation laravel laravel-5.2

Here is the custom validation I made to validate a string. My question is how to pass a variable (i.e. missing_tags) into the error message? Thanks.

Validator::extend('tags', function($attribute, $value, $parameters, $validator) {
        $tags = explode(',', $value);
        $missing_tags = array()

        foreach ($tags as $tag)
        {
            if (!Tag::exist($tag))
            {
                $missing_tags[] = $tag;
            }
        }

        return count($missing_tags) == 0;
    });

    Validator::replacer('tags', function($message, $attribute, $rule, $parameters)
    {
        return str_replace('missing_tags', $missing_tags, $message);
    });

0 个答案:

没有答案