FabioFratini 9 bulan lalu
induk
melakukan
89c6d64fab
2 mengubah file dengan 105 tambahan dan 24 penghapusan
  1. 37 17
      app/Http/Livewire/Member.php
  2. 68 7
      resources/views/livewire/member.blade.php

+ 37 - 17
app/Http/Livewire/Member.php

@@ -6,6 +6,7 @@ use Livewire\Component;
 use Livewire\Attributes\Url;
 use Livewire\WithFileUploads;
 use Livewire\WithPagination;
+use Illuminate\Support\Facades\Log;
 
 use DateTime;
 
@@ -134,7 +135,7 @@ class Member extends Component
     public $course_name, $course_level_id, $course_type_id, $course_frequency_id;
 
     // Certificates data
-    public $member_certificates = array(), $certificate_type, $certificate_filename_old, $certificate_filename, $certificate_expire_date, $certificate_status, $addCertificate, $updateCertificate, $certificateDataId;
+    public $member_certificates = array(), $certificate_type, $certificate_filename_old, $certificate_filename, $certificate_expire_date, $certificate_status, $addCertificate, $updateCertificate, $certificateDataId, $cardCertificateId;
 
     public $filterCard = [];
     public $filterCategory = [];
@@ -348,11 +349,11 @@ class Member extends Component
             // Carico gli abbonamenti e i mesi
             $period = \Carbon\CarbonPeriod::create($c->date_from, '1 month', $c->date_to);
             $this->course_months_list = [];
-            foreach ($period as $dt) 
+            foreach ($period as $dt)
             {
                 $this->course_months_list[] = $dt->format("m");
             }
-            
+
             $course_subscription_ids = [];
             $this->course_price_list = [];
             if ($c->prices != null)
@@ -860,7 +861,6 @@ class Member extends Component
             'email' => 'required',
             'phone' => 'required',
             'birth_nation_id' => 'required',
-            //'birth_place' => 'required',
             'address' => 'required',
             'zip_code' => 'required',
             'nation_id' => 'required',
@@ -905,12 +905,32 @@ class Member extends Component
             $existingMember = \App\Models\Member::where('fiscal_code', $this->fiscal_code)->first();
             if ($existingMember) {
                 $this->already_existing = true;
+                $this->emit('focus-error-field', 'fiscal_code');
+
                 return;
             }
         }
-        
-        $this->validate($rules);
-        
+
+        if ($this->under18) {
+            $rules['father_name'] = 'required_without:mother_name';
+            $rules['father_email'] = 'required_without:mother_email|email';
+            $rules['father_fiscal_code'] = 'required_without:mother_fiscal_code';
+            $rules['mother_name'] = 'required_without:father_name';
+            $rules['mother_email'] = 'required_without:father_email|email';
+            $rules['mother_fiscal_code'] = 'required_without:father_fiscal_code';
+        }
+
+        try {
+            $this->validate($rules);
+        } catch (\Illuminate\Validation\ValidationException $e) {
+            $this->isSaving = false;
+            $errorFields = array_keys($e->errors());
+            if (!empty($errorFields)) {
+                $this->emit('focus-error-field', $errorFields[0]);
+            }
+            Log::error('Validation failed', ['errors' => $e->errors()]);
+            return;
+        }
         try {
 
             $name = '';
@@ -925,7 +945,7 @@ class Member extends Component
 
             $mother_docs = implode("|", $this->mother_document_files);
 
-            
+
 
             $member = \App\Models\Member::create([
                 'first_name' => strtoupper($this->first_name),
@@ -972,7 +992,7 @@ class Member extends Component
                 'to_complete' => false,
                 'enabled' => $this->enabled
             ]);
-            
+
             session()->flash('success, Tesserato creato');
             updateMemberData($member->id);
             $this->resetFields();
@@ -1230,7 +1250,7 @@ class Member extends Component
             session()->flash('success', "Tesserato eliminato");
             $this->emit('reload');
         } catch (\Exception $e) {
-            session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
+            session()->flash('error', 'Errore (' . $e->getMessage() . ')');
         }
     }
 
@@ -1252,7 +1272,7 @@ class Member extends Component
                 \App\Models\Member::find($id)->delete();
             }
         } catch (\Exception $e) {
-            session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
+            session()->flash('error', 'Errore (' . $e->getMessage() . ')');
         }
         $this->multipleAction = '';
     }
@@ -1454,7 +1474,7 @@ class Member extends Component
             \App\Models\MemberCard::find($id)->delete();
             session()->flash('success', "Tesserato eliminato");
         } catch (\Exception $e) {
-            session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
+            session()->flash('error', 'Errore (' . $e->getMessage() . ')');
         }
     }
 
@@ -1501,7 +1521,7 @@ class Member extends Component
             $mc->months = json_encode($this->course_months);
             $mc->when = json_encode($this->course_when);
             $mc->save();
-            
+
             // Se il corso ha associato una categoria iscrivo anche al gruppo
             $c = \App\Models\Course::findOrFail($this->course_course_id);
             if ($c) {
@@ -1553,7 +1573,7 @@ class Member extends Component
                 $rate->note = '';
                 $rate->status = 0;
                 $rate->save();
-                
+
                 $start = date('Y-m-d', strtotime("+" . $r->months . " months", strtotime($start)));
 
                 if ($start > $this->course_date_to)
@@ -1678,7 +1698,7 @@ class Member extends Component
             \App\Models\MemberCourse::find($id)->delete();
             session()->flash('success', "Corso eliminato");
         } catch (\Exception $e) {
-            session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
+            session()->flash('error', 'Errore (' . $e->getMessage() . ')');
         }
     }
 
@@ -1812,7 +1832,7 @@ class Member extends Component
             \App\Models\MemberCertificate::find($id)->delete();
             session()->flash('success', "Tesserato eliminato");
         } catch (\Exception $e) {
-            session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
+            session()->flash('error', 'Errore (' . $e->getMessage() . ')');
         }
     }
 
@@ -1868,7 +1888,7 @@ class Member extends Component
             \App\Models\MemberCategory::find($id)->delete();
             session()->flash('success', "Associazione eliminata");
         } catch (\Exception $e) {
-            session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
+            session()->flash('error', 'Errore (' . $e->getMessage() . ')');
         }
     }
 

+ 68 - 7
resources/views/livewire/member.blade.php

@@ -1165,7 +1165,7 @@
                                                                 @endforeach
                                                             </td>
                                                             <td style="width:15%">
-                                                                {{date("d/m/Y", strtotime($member_course->date_from))}} <br> {{date("d/m/Y", strtotime($member_course->date_to))}}                                                                
+                                                                {{date("d/m/Y", strtotime($member_course->date_from))}} <br> {{date("d/m/Y", strtotime($member_course->date_to))}}
                                                             </td>
                                                             <td style="text-align:right;width:10%">
                                                                 <div class="row">
@@ -1186,7 +1186,7 @@
                                                                     <button type="button" class="btn" wire:click="showHideCourse({{ $member_course->id }})"><i class="fa-solid fa-chevron-down"></i></button>&nbsp;
                                                                 @endif
                                                             </td>
-                                                            
+
                                                         </tr>
                                                         @if(in_array($member_course->id, $showCourse))
                                                             @if(false)
@@ -1229,7 +1229,7 @@
                                                                     @endif
                                                                 </td>
                                                             </tr>
-                                                        
+
                                                         @endif
 
                                                     @endforeach
@@ -1301,9 +1301,9 @@
                                                                     </select>
                                                                 </div>
                                                             @endif
-                                                            
+
                                                         </div>
-                                                        
+
                                                     </div>
                                                     @if($course_course_id > 0)
                                                         <div class="row mt-4">
@@ -1322,9 +1322,9 @@
                                                                 <label for="course_price" class="form-label">Costo mensile</label>
                                                                 <input class="form-control" type="text" id="course_price" placeholder="Costo mensile" wire:model="course_price" onkeyup="onlyNumberAmount(this)">
                                                             </div>
-                                                            
+
                                                         </div>
-                                                    
+
                                                         @if(false)
                                                             <div class="row mt-4 course--duration">
                                                                 <div class="col-md-12">
@@ -1897,6 +1897,16 @@
         color: black;
         font-weight:normal;
     }
+
+    .persistent-invalid
+    {
+        border-color: #dc3545 !important;
+        padding-right: calc(1.5em + 0.75rem) !important;
+        background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12' width='12' height='12' fill='none' stroke='%23dc3545'%3e%3ccircle cx='6' cy='6' r='4.5'/%3e%3cpath stroke-linejoin='round' d='M5.8 3.6h.4L6 6.5z'/%3e%3ccircle cx='6' cy='8.2' r='.6' fill='%23dc3545' stroke='none'/%3e%3c/svg%3e") !important;
+        background-repeat: no-repeat !important;
+        background-position: right calc(0.375em + 0.1875rem) center !important;
+        background-size: calc(0.75em + 0.375rem) calc(0.75em + 0.375rem) !important;
+    }
     </style>
     <script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
     <script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-rc.0/dist/js/select2.min.js"></script>
@@ -2639,6 +2649,57 @@
         $(document).ready(function() {
             setupBackButtonHandlers();
         });
+        document.addEventListener('livewire:load', () => {
+    Livewire.on('focus-error-field', (fieldId) => {
+        console.log('Validation errors received:', fieldId);
+        let element;
+
+        if (typeof fieldId === 'string') {
+            element = document.getElementById(fieldId);
+        } else if (Array.isArray(fieldId) && fieldId.length > 0) {
+            const firstFieldId = fieldId[0];
+            element = document.getElementById(firstFieldId);
+            fieldId = firstFieldId; // Use the first fieldId for highlighting
+        }
 
+        if (element) {
+            element.focus();
+            element.scrollIntoView({
+                behavior: 'smooth',
+                block: 'center'
+            });
+
+            // Add the is-invalid class to highlight the field
+            element.classList.add('is-invalid');
+
+            // Also highlight the label
+            const label = document.querySelector(`label[for="${fieldId}"]`);
+            if (label) {
+                label.classList.add('text-danger');
+            }
+
+            // Prevent the class from being removed
+            const preserveInvalidClass = () => {
+                if (!element.classList.contains('is-invalid')) {
+                    element.classList.add('is-invalid');
+                }
+            };
+
+            // Set an interval to ensure the class stays applied
+            const intervalId = setInterval(preserveInvalidClass, 100);
+
+            // Stop preserving after user interaction
+            element.addEventListener('input', () => {
+                clearInterval(intervalId);
+                // Optional: Remove the highlighting when user edits the field
+                // element.classList.remove('is-invalid');
+                // if (label) label.classList.remove('text-danger');
+            });
+
+        } else {
+            console.warn('Element with id ' + fieldId + ' not found');
+        }
+    });
+});
     </script>
 @endpush