Created
August 16, 2017 08:52
-
-
Save ngugijames/d3535e5ca4edbbbc14a7f8a4f7547803 to your computer and use it in GitHub Desktop.
showing :value in laravel validation errors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//in controller | |
$validator = Validator::make(['one','two','three'], [ | |
'*' => 'exists:table,name', | |
], [ | |
'exists' => '`:value` does not exist.', | |
]); | |
//in service provider boot | |
$this->app->validator->resolver(function($translator, $data, $rules, $messages, $attributes) | |
{ | |
return new CustomValidator($translator, $data, $rules, $messages, $attributes); | |
}); | |
//CustomValidator | |
use Illuminate\Validation\Validator; | |
class CustomValidator extends Validator | |
{ | |
/** | |
* Replace all place-holders for the view_exists rule. | |
* | |
* @param string $message | |
* @param string $attribute | |
* @param string $rule | |
* @param array $parameters | |
* @return string | |
*/ | |
protected function replaceExists($message, $attribute, $rule, $parameters) | |
{ | |
return str_replace(':value', $this->getValue($attribute), $message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment