$errors->first('subject') will give you the first error message from the array of error messages for subject.
How to display errors inside a Laravel Livewire child component?
I created an autocomplete Livewire Component:
<livewire:form.subject-autocomplete class="mt-2" id="subject" name="subject" label="Subject" key="subject" :error="$errors->get('subject')" limit="1"/>
My parent component validates the subject and has an error (what gives me the var_dump($errors->get('subject')) inside of the blade (parent) template:
array(1) { [0]=> string(30) "The subject field is required." }
Inside of the child component the error property does not change at all.
It this intentionally? How can I set the error message of my child form components?
I would be really grateful for any help
The component is quite big :D.
But I solved it this way:
I fired an event on dehydrate on the child component:
public function dehydrate()
{
$error = $this->getErrorBag()->first('selected');
if (!$error) {
return;
}
$this->emitUp($this->name . 'HasError', $error);
}
Then I can catch it inside my parent like this:
protected $listeners = [
'subjectHasError',
];
public function subjectHasError(string $error)
{
$this->addError('subject', $error);
}
This way the components can 'talk' to each other ;).
Thank you for your help!
Please or to participate in this conversation.