@if (count($errors) > 0)
$error->has() for ANY errors in input array
I want to check, on my view, if there are any errors.
Normally:
$errors->has(name-of-field)
Or with an array:
$errors->has(array.index)
How do I check if there was an error for ANY of my input arrays?
I want so display an error if
$error->has('user.0') || $error->has('user.1') || ...
But I do not know how many users will be validated, I just want to display the error if user.any has an error
I tried:
$errors->has('user.*')
, but that did not work
Any ideas?
Here is what I am doing so that the view can be as clean as possible.
I am running a check after the validation to see if there exists an error on the members.. If there is, I add the error with the name of 'members'. This way, on my view side, I can keep it clean with $error->has('members') etc. I can run the regular expression here to check for members.
public function getValidatorInstance()
{
$validator = parent::getValidatorInstance();
$validator->after(function () use ($validator) {
if ($error = $this->getIndividualMemberError($validator)) {
$validator->errors()->add('members', $error);
}
});
return $validator;
}
private function getIndividualMemberError($validator)
{
$individualError = collect($validator->messages())->first(function ($key, $value) {
return preg_match('/members\./', $key);
});
return is_null($individualError) ? false : collect($individualError)->first();
}
If the MessagesBag->has() could take a regular expression, that would make this not needed
Please or to participate in this conversation.
