Skip to content

Instantly share code, notes, and snippets.

@ngugijames
Created August 16, 2017 08:52
Show Gist options
  • Save ngugijames/d3535e5ca4edbbbc14a7f8a4f7547803 to your computer and use it in GitHub Desktop.
Save ngugijames/d3535e5ca4edbbbc14a7f8a4f7547803 to your computer and use it in GitHub Desktop.
showing :value in laravel validation errors
//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