Repository: jeroennoten/Laravel-AdminLTE (language: php)

Issue:
Error message Not showing in componet
I implement Input component and i try to validate the value. validation working fine but not showing error messages ..

```
<x-adminlte-input name="stu_sur_name" id="stu_sur_name" label="Sur Name"  igroup-size="sm" placeholder="Dimul Gomisge">
    <x-slot name="prependSlot">
      <div class="input-group-text">
        <i class="fas fa-user text-lightblue"></i>
      </div>
</x-slot>
</x-adminlte-input>   
```

Can any one help this




Discussion / hints:
Please, share the code or logic you are using on the controller to validate the input...
> e

protected $rules = [
        'stu_first_name'  => 'required',    
        'stu_middel_name'  => 'required',    
    ];

    public function updated($rules)
    {
        $this->validateOnly($rules);
    }
    
    public function createAdmission()
    {
        
        $validatedData = $this->validate();
        Student::Create([
            'stu_roll_number' => $this->stu_roll_number,
            'stu_admission_number' => $this->stu_admission_number,
            'stu_admission_date' => $this->stu_admission_date,
            'stu_admission_grade' => $this->stu_admission_grade,
            'stu_title' => $this->stu_title,
            'stu_ge_name' => $this->stu_ge_name,
            'stu_first_name' => $this->stu_first_name,
            'stu_middel_name' => $this->stu_middel_name,
            'stu_sur_name' => $this->stu_sur_name,
            'stu_dob' => $this->stu_dob,
            'stu_gender' => $this->stu_gender,
            'stu_mother_tongue' => $this->stu_mother_tongue,
            'stu_religion' => $this->stu_religion,
            'stu_nationality' => $this->stu_nationality,
            'stu_description' => $this->stu_description,
            
           
            ]);
            $this->reset();
            $this->dispatchBrowserEvent('alert', ['message' => 'Admission Created Successfully!']);
            
            
    }
@tmgomas Not sure what type of validation are you using, I have tested with the one documented [here](https://laravel.com/docs/8.x/validation#quick-writing-the-validation-logic) and all works fine. Be sure to trace the **session's error bag** and check that error messages appears, for example:

```blade
@php
    logger('ERRORS: ' . session()->get('errors'));
@endphp

.
.
.

<x-adminlte-input name="stu_sur_name" id="stu_sur_name" label="Sur Name"  igroup-size="sm" placeholder="Dimul Gomisge">
    <x-slot name="prependSlot">
      <div class="input-group-text">
        <i class="fas fa-user text-lightblue"></i>
      </div>
    </x-slot>
</x-adminlte-input>  
```

Then, after submitting the form, open the `storage/logs/laravel.log` file and search for that trace. Then share it here, you need to see something like next:

```sh
[2021-08-27 02:08:08] local.DEBUG: ERRORS: {"stu_sur_name":["The stu_sur_name field is required."], ...}
```
its Working with Normal Blade .. I am using Livewire Validation ... Its not working with liveiwre 

Its not working with the Components
@tmgomas you need to see the error message on the **Laravel error's bag**. I have explained previosuly how you can trace it. So, please share what you see on the trace for the Laravel  error's bag.
@Shidersz  

Yes no issue with Error Bag and its not working with error Key .. i try this method and its working 

```
<x-adminlte-input name="company_name" class="{{ $errors->has('company_name') ? 'is-invalid' : '' }}" wire:model.defer="company_name" label="Company Name ( School Name )" placeholder="placeholder" fgroup-class="col-md-12 col-lg-12"/>
               
<x-adminlte-input name="company_s_name" error-key="company_s_name" wire:model.defer="company_s_name" label="Short Name ( KPS )" placeholder="placeholder" fgroup-class="col-md-12 col-lg-6"/>

``` 

Error Key is not working
Ok, please, share what you see on the error's bag after submitting an invalid value for both inputs. I need to see your error's bag format in order to track any issue. Take into account that these components where not intended to work with **Livewire** and there may be some sort of conflict with it...
@tmgomas have you found a solution for this? Share the indicated trace otherwise...
Closed, no feedback!
facing the same issue with adminlte and laravel livewire, both latest versions. The $errors variable exists in the livewire blade and validation errors are there. it was working with previous livewire version and dev-next branch of adminlte, stopped working now. can solve it manually using @error directive, just thought to report in case it could be fixed

Attaching logs

```sh
info ($errors);
local.INFO: {"record.descr":["The descr field is required."],"record.descren":["The descren field is required."]}

info(\Session::all());
local.INFO: array (
  '_token' => 'DFRTYEREwsdsaWxQZmM4WXsHL6CqtNM9D5r1qn',
  'url' => 
  array (
  ),
  '_previous' => 
  array (
    'url' => 'http://myapp.dev/categories/create',
  ),
  '_flash' => 
  array (
    'old' => 
    array (
    ),
    'new' => 
    array (
    ),
  ),
)  

logger('ERRORS: ' . session()->get('errors'));
[2021-10-19 19:39:34] local.DEBUG: ERRORS: (nothing here)  
```

the blade of the livewire component is

```blade
<x-adminlte-card theme="light" theme-mode="full" icon="fas fa-lg fa-plus" maximizable>
    <form wire:submit.prevent="submit" autocomplete="off">
        <x-adminlte-input name="record.descr" placeholder="Descr" wire:model.lazy="record.descr"/>
        <x-adminlte-input name="record.descren" placeholder="DescrEn" wire:model.lazy="record.descren"/>
        <x-adminlte-button :type="'submit'" class="d-flex ml-auto" theme="dark" label="Σθβμιτ" icon="fas fa-sign-in"/>
    </form>
</x-adminlte-card>
```

thank you
@asartz Thanks for sharing info. I will check this when get some time...
Check if your config is using livewire first of all
any one got any answer ?
Hi @tmgomas from what I can guess, it appears that **Livewire** do not flash errors into the session when your validation fails, since **Livewire** do not make a full page reload on these cases. See next links for more info:

- https://stackoverflow.com/questions/63986147/how-in-laravel-livewire-set-flash-message-with-validation-erros
- https://github.com/livewire/livewire/issues/621

This conflicts with our current code since we put the error display logic inside the component class, and it appears that validation errors are only available on the blade files for **Livewire**. Please, understand this package wasn't focused on supporting **Livewire**. For the moment, try the next fix temporarily:

Open the file `vendor/jeroennoten/laravel-adminlte/resources/views/components/form/input-group-component.blade.php` and edit next lines:

```blade
{{-- Error feedback --}}
@if($isInvalid() && ! isset($disableFeedback))
    <span class="invalid-feedback d-block" role="alert">
        <strong>{{ $errors->first($errorKey) }}</strong>
    </span>
@endif
```

by replacing by next ones:

```blade
@if($errors->has($errorKey) && ! isset($disableFeedback))
    <span class="invalid-feedback d-block" role="alert">
        <strong>{{ $errors->first($errorKey) }}</strong>
    </span>
@endif
```

If this solution works anyway, please tell us and we going to try to address this problem..
> Hi @tmgomas from what I can guess, it appears that **Livewire** do not flash errors into the session when your validation fails, since **Livewire** do not make a full page reload on these cases. See next links for more info:
> 
> * https://stackoverflow.com/questions/63986147/how-in-laravel-livewire-set-flash-message-with-validation-erros
> * [Validation errors are not flashed to session livewire/livewire#621](https://github.com/livewire/livewire/issues/621)
> 
> This conflicts with our current code since we put the error display logic inside the component class, and it appears that validation errors are only available on the blade files for **Livewire**. Please, understand this package wasn't focused on supporting **Livewire**. For the moment, try the next fix temporarily:
> 
> Open the file `vendor/jeroennoten/laravel-adminlte/resources/views/components/form/input-group-component.blade.php` and edit next lines:
> 
> ```
> {{-- Error feedback --}}
> @if($isInvalid() && ! isset($disableFeedback))
>     <span class="invalid-feedback d-block" role="alert">
>         <strong>{{ $errors->first($errorKey) }}</strong>
>     </span>
> @endif
> ```
> 
> by replacing by next ones:
> 
> ```
> @if($errors->has($errorKey) && ! isset($disableFeedback))
>     <span class="invalid-feedback d-block" role="alert">
>         <strong>{{ $errors->first($errorKey) }}</strong>
>     </span>
> @endif
> ```
> 
> If this solution works anyway, please tell us and we going to try to address this problem..

Thanks its Working 
Sry for the delay 

Fix patch:
diff --git a/resources/views/components/form/input-group-component.blade.php b/resources/views/components/form/input-group-component.blade.php
index c9de3687..a1413a80 100644
--- a/resources/views/components/form/input-group-component.blade.php
+++ b/resources/views/components/form/input-group-component.blade.php
@@ -1,3 +1,11 @@
+{{-- Setup the internal errors bag for the component --}}
+
+@php
+    $setErrorsBag($errors);
+@endphp
+
+{{-- Setup the input group component structure --}}
+
 <div class="{{ $makeFormGroupClass() }}">
 
     {{-- Input label --}}
diff --git a/src/View/Components/Form/InputGroupComponent.php b/src/View/Components/Form/InputGroupComponent.php
index 59deb650..27157f48 100644
--- a/src/View/Components/Form/InputGroupComponent.php
+++ b/src/View/Components/Form/InputGroupComponent.php
@@ -6,6 +6,14 @@
 
 class InputGroupComponent extends Component
 {
+    /**
+     * Holds an instance of the Laravel errors bag. This will be mainly used to
+     * detect when the input group has associated errors.
+     *
+     * @var \Illuminate\Support\MessageBag;
+     */
+    protected $errorsBag;
+
     /**
      * The id attribute for the underlying input group item. The input group
      * item may be an "input", a "select", a "textarea", etc.
@@ -99,6 +107,10 @@ public function __construct(
         // Setup the lookup key for validation errors.
 
         $this->errorKey = $errorKey ?? $this->makeErrorKey();
+
+        // Initialize the internal errors bag holder variable.
+
+        $this->errorsBag = null;
     }
 
     /**
@@ -166,13 +178,26 @@ public function makeItemClass()
     public function isInvalid()
     {
         // Get the errors bag from session. The errors bag will be an instance
-        // of the Illuminate\Support\MessageBag class.
+        // of the Illuminate\Support\MessageBag class. First we will check if
+        // the errors bag is available within this instance, otherwise we will
+        // try to get it from the session.
 
-        $errors = session()->get('errors');
+        $errors = $this->errorsBag ?? session()->get('errors');
 
         // Check if exists any error related to the configured error key.
 
-        return ! empty($errors) && ! empty($errors->first($this->errorKey));
+        return ! empty($errors) && $errors->has($this->errorKey);
+    }
+
+    /**
+     * Setup the internal errors bag.
+     *
+     * @param  \Illuminate\Support\MessageBag  $errorsBag
+     * @return void
+     */
+    public function setErrorsBag($errorsBag)
+    {
+        $this->errorsBag = $errorsBag;
     }
 
     /**


Test patch:
diff --git a/tests/Components/FormComponentsTest.php b/tests/Components/FormComponentsTest.php
index eecb26fd..7123718a 100644
--- a/tests/Components/FormComponentsTest.php
+++ b/tests/Components/FormComponentsTest.php
@@ -79,7 +79,7 @@ public function testAllComponentsRender()
     |--------------------------------------------------------------------------
     */
 
-    public function testInputGroupComponent()
+    public function testInvalidInputGroupComponentBySessionErrorsBag()
     {
         $component = new Components\Form\InputGroupComponent(
             'name', null, null, 'lg', null, 'fgroup-class', 'igroup-class'
@@ -101,6 +101,35 @@ public function testInputGroupComponent()
         $this->assertStringContainsString('is-invalid', $iClass);
     }
 
+    public function testInvalidInputGroupComponentByErrorsBagSetup()
+    {
+        // Note we configure a specific error key called 'nameErrorKey'.
+
+        $component = new Components\Form\InputGroupComponent(
+            'name', null, null, 'sm', null, 'fgroup-class',
+            'igroup-class', null, 'nameErrorKey'
+        );
+
+        // Setup an internal errors bag for the component.
+
+        $msgBag = new MessageBag();
+        $msgBag->add('nameErrorKey', 'error');
+        $component->setErrorsBag($msgBag);
+
+        // Test the component.
+
+        $iGroupClass = $component->makeInputGroupClass();
+        $fGroupClass = $component->makeFormGroupClass();
+        $iClass = $component->makeItemClass();
+
+        $this->assertStringContainsString('input-group', $iGroupClass);
+        $this->assertStringContainsString('input-group-sm', $iGroupClass);
+        $this->assertStringContainsString('igroup-class', $iGroupClass);
+        $this->assertStringContainsString('adminlte-invalid-igroup', $iGroupClass);
+        $this->assertStringContainsString('form-group', $fGroupClass);
+        $this->assertStringContainsString('fgroup-class', $fGroupClass);
+    }
+
     /*
     |--------------------------------------------------------------------------
     | Date range component tests.