Edit with a Potential Solution
After some digging, I looked at the Validator class and how it adds its error messages to see if it has any placeholders available.
In Illuminate\Validation\Validator function that, it seems to me, runs to validate the request, validate , which runs each of the rules in turn and adds error messages. The piece of code associated with adding the error message is the one at the end of the function:
$value = $this->getValue($attribute); $validatable = $this->isValidatable($rule, $attribute, $value); $method = "validate{$rule}"; if ($validatable && ! $this->$method($attribute, $value, $parameters, $this)) { $this->addFailure($attribute, $rule, $parameters); }
As you can see, it does not actually transmit the value of the field that was checked when it adds a failure, which in turn adds an error message.
I managed to achieve something to achieve what you need. If you add these two methods to your base Request class, which is usually located in App\Http\Requests\Request :
protected function formatErrors(Validator $validator) { $errors = parent::formatErrors($validator); foreach ($errors as $attribute => $eachError) { foreach ($eachError as $key => $error) { if (strpos($error, ':value') !== false) { $errors[$attribute][$key] = str_replace(':value', $this->getValueByAttribute($attribute), $error); } } } return $errors; } protected function getValueByAttribute($attribute) { if (($dotPosition = strpos($attribute, '.')) !== false) { $name = substr($attribute, 0, $dotPosition); $index = substr($attribute, $dotPosition + 1); return $this->get($name)[$index]; } return $this->get($attribute); }
Then, in your verification messages, you can put a substitute :value , which should be replaced with the actual value that was checked, for example:
public function messages() { return [ 'external_media.*.active_url' => 'The URL :value is not valid' ]; }
I noticed a couple of problems with your code:
- In your
messages function, you provided a message for active_url , which is the name of the rule, not the name of the field. This must be external_media . - You are not returning the
$messages variable from your messages function. You need to add return $messages; In the end.
Regarding your question, however, the class in which you write this code is an instance of Illuminate\Http\Request , and you should have access to the actual values ââprovided to this request when generating error messages. For example, you can do something like this:
public function messages() { return [ 'external_media' => 'The following URL is invalid: ' . $this->get('external_media') ]; } public function rules() { return [ 'external_media' => 'active_url' ]; }
Which will include the value indicated in external_media in the error message. Hope this helps.