0

I have a Laravel 10, Livewire 3, Alpine JS application. On one of the blade files, I use the alpine x-for to loop through and add an input for each entry. I need to validate each input on change. I'm using a custom validation rule and calling $fail(...); if there's an error. the problem is that i cannot figure out how to display the validation error inside the x-for template under the input that failed validation. it displays after the looped inputs if i put the span element outside of the template but then i can't use the index variable from the x-for.

blade file:

<template x-for="(value, index) in some_array" :key="value_" + index>
    <div>
        <!-- input -->

        <span x-show="$errors.has('...' + index)" x-text="$errors.first('...' + index)">
   </div>
</template>
// span works if i put it here

i've tried using $wire.errors, @error, @if (checking for the error bag), and wire:show also but nothing is working inside the template. is there a way to get this to work or do i need to use @foreach instead of x-for? thanks.

This is what's in the error bag when i try adding a duplicate url in the third input spot and it fails validation:

{"some_array.2":["This URL has already been reported. Please contact support for further questions."]}

this is what $errors->all() looks like in the blade file:

array (
  0 => 'This URL has already been reported. Please contact support for further questions.',
)
2
  • Hi welcome! Can you to make it easier for us edit your post with a debug of $errors? This to make sure the format is correct. Also check the (value, index) in some_array, this should be errors instead of some_array. Commented Mar 25 at 0:30
  • @N.Hamelink some_array is the array that's values are bound to the inputs. i'm not trying to loop through the $errors array if that's what you mean by "this should be errors instead of some_array". i wan't to display the errors underneath the appropriate input. Commented Mar 25 at 15:52

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.