Laravel Errorbag Not working inside Blade Component

Viewed 1488

I have a Laravel Livewire component where I have included a Blade Component as well like so

<div>
    <div class="card-header">
        <div>{{ $quiz->name }}</div>
        <x-create-question-modal :state="$modalQuestionState" :question="$question"/>
    </div>

    @foreach($quiz->questions as $question)
    <div class="card-body">
        <div class="card-title">
            {{ $question->question }}
        </div>
        @foreach($question->choices as $choice)
            <div class="card-text">
                {{ $choice->name }}
            </div>
        @endforeach
    </div>
        <hr>
    @endforeach
</div>

This is my Blade Component

<div>
    <div>
        <div wire:click="modalCreateQuestionToggle">Create New Quiz</div>
    </div>

    {{--  Create Quiz Modal --}}
    <div class="modal @if($state) d-block @endif" tabindex="-1" role="dialog">
        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">Create New Quiz</h5>
                    <button wire:click="modalCreateQuestionToggle" type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <form wire:submit.prevent="store">
                    <div class="modal-body">
                        <input type="text" wire:model="question">
                        @error('question') <span class="error alert-danger">{{ $message }}</span> @enderror
                    </div>
                    <div class="modal-footer">
                        <button type="submit" class="btn btn-primary">Create</button>
                    </div>
                </form>

            </div>
        </div>
    </div>

</div>

This is the store method which I have in Livewire Class,

    public function store() {
        //
        $this->validate([
            'question' => 'required|min:5',
        ]);

        Questions::create([
            'question' => $this->question,
            'quiz_id' => $this->quiz->id
        ]);
        $this->modalQuestionState = !$this->modalQuestionState;
        $this->question = null;
    }

When the store is called, the validation fails, but for some reason it is not rendering in my blade component.

This is what the output of livewire's network

errorBag: {question: ["The question must be at least 5 characters."]}
question: ["The question must be at least 5 characters."]
1 Answers

just put this portion on your livewire component instead the full modal. I had the same issue, solved by doing this.

It should solve your issue. let me know.

      <div>
           <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">Create New Quiz</h5>
                    <button wire:click="modalCreateQuestionToggle" type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <form wire:submit.prevent="store">
                    <div class="modal-body">
                        <input type="text" wire:model="question">
                        @error('question') <span class="error alert-danger">{{ $message }}</span> @enderror
                    </div>
                    <div class="modal-footer">
                        <button type="submit" class="btn btn-primary">Create</button>
                    </div>
                </form>

            </div>
      </div>

This is my modal in liveware component:

https://ibb.co/KFWtFzP

This is my blade:

https://ibb.co/v156XVT

Explanation: This is because when your component refreshes , livewire provides you refreshed view with its changes. in Refreshed component the modal is not triggered to be open anymore. So you are not getting the changes. So when you are using modal, make sure you using modal contents in your livewire component, so that the open modal remain open , the content inside it will get refreshed then. Hope I made it clear to you. Let me know if you aren't clear enough.

Related