Skip to content

Instantly share code, notes, and snippets.

@lezhnev74
Last active March 9, 2021 03:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lezhnev74/84fef07a43e79bfc490d4cff8d17c05e to your computer and use it in GitHub Desktop.
Save lezhnev74/84fef07a43e79bfc490d4cff8d17c05e to your computer and use it in GitHub Desktop.
Replace custom placeholder in Validation message (Laravel)
<?php
// When I create a validation rule, I can set my one replacer which helps replace different special placeholders
// For example if validator message is "Problem with :some", then my replacer will handle it
Validator::extend('testrule', function($attribute, $value, $parameters, $validator) {
$validator->addReplacer('testrule', function($message, $attribute, $rule, $parameters){
return str_replace(":some", "whatever", $message);
});
return $value == "test";
});
@UnKulMunki
Copy link

UnKulMunki commented Mar 22, 2019

What about passing the value to the message. Instead of
return str_replace(":some", "whatever", $message);
I want to do something like:
return str_replace(":some", $someValue , $message);

Any ideas?
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment