最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

laravel - display livewire validation errors in alpine js x-for template - Stack Overflow

programmeradmin6浏览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.',
)
发布评论

评论列表(0)

  1. 暂无评论