jonassiewertsen / statamic-livewire

A Laravel Livewire integration for Statamics antlers engine
91 stars 16 forks source link

Make errors work in Antlers #13

Closed aerni closed 2 years ago

aerni commented 3 years ago

There is currently no way to get validation errors with Antlers. With Blade you can easily do the following:

@if ($errors->any())     
    <p>There were $errors->count() errors</p> 
    <ul>
        @foreach ($errors->all() as $error)
            <li>{{ $error }}</li>
        @endforeach
    </ul>
@endif

I'm currently using a custom tag to get around this. But I was wondering if you are open to a PR that adds the functionality to this package. It could work like this:

{{ if {livewire:errors} }}
    <p>There were {{ livewire:error_count }} errors</p> 
    <ul>
        {{ livewire:errors }}
            <li>{{ error }}</li>
        {{ livewire:errors }}
    </ul>
{{ /if }}
jonassiewertsen commented 3 years ago

I get your point and now this pain point myself 😊

I am not sure if the livewire integration is the right place to fetch errors from the error bag, as this belongs to Antlers and is not related to Livewire only. Does this make sense or am I missing something?

aerni commented 3 years ago

I was contemplating the same. I think it goes into the same direction like Erin's idea here: https://github.com/statamic/ideas/issues/75

The only issue with Erin's implementation is, that Livewire's error bag can't be accessed with the session(). At least in my testing, it didn't work. I had to get the error bag directly from the Livewire instance. And if that's the case, I think getting the error from the Livewire instance would be in line with this addon's purpose of making Livewire accessible with Antlers.

Here's my implementation with a custom tag: https://github.com/aerni/statamic-livewire-forms/blob/main/src/Tags/LivewireForms.php

How have you been handling errors in Antlers?

jonassiewertsen commented 3 years ago

Here is an very old PR from myself: https://github.com/statamic/cms/pull/2333

aerni commented 3 years ago

Do you suggest rolling with my own error tag until this idea is implemented? Or are you open for a PR?

jonassiewertsen commented 3 years ago

I did a little tinkering and would love to include an error-tag. So yes, a PR would be appreciated a lot.

What would you think about the following syntax: {{ errors }} to get the complete bag {{ error:name }} to fetch a specific error.

What I am not sure about, as I haven't looked into it: The best way to get the errors into the view. I would think that the livewire errors already are there, but that we only need to pass them to the view.

aerni commented 3 years ago

Awesome! I like the idea to get the whole bag or specific error.

Unfortunately, the {{ errors }} tag name won't work. I'm using this tag name in my Statamic Livewire Forms addon and it's causing issues with password protected pages. Check this issue: https://github.com/statamic/cms/issues/4004

Long story short; we have to settle on a different tag name. Or maybe you see something I don't?

jonassiewertsen commented 3 years ago

Hmmm ... I see.

We can think about this a little longer, but what about {{ error:all }} for now?

aerni commented 3 years ago

Personally, I'd probably try to scope the tag name to this addon to not cause any potential name conflicts. It's not as pretty but more bulletproof.

Either {{ livewire:errors }} or {{ livewire_errors }}

You mentioned: {{ error:name }} to fetch a specific error. What's the use case of fetching a specific error? The following already works in Antlers:

{{ if error }}
    {{ error }}
{{ /if }}
jonassiewertsen commented 2 years ago

Might this release solve the issue? I haven't checked it yet.

https://github.com/statamic/cms/releases/tag/v3.2.23

aerni commented 2 years ago

I just tested the Statamic PR and it solves this issue.