Home > Software engineering >  Custom errorBag for different cards in one livewire form
Custom errorBag for different cards in one livewire form

Time:06-10

My livewire form has four cards in it, and users can choose which cards they would like to answer, and it can be more than one card. So I did a condition that checks which cards are selected and only validates data and creates database entry based on the cards selected.

CardFormComponent.php:

public function submit()
    {
        $user = auth()->user();
               
        if($this->showDiv1){
            $salary_data = $this->validate([
                'emp_name' => 'required',
                'emp_length' => 'required',
                'emp_monthly_gross_salary' => 'required',
                'monthly_allowance' => 'required',
            ])->validateWithBag('salary_error');
        }
        
        if($this->showDiv2){
            $gig_data = $this->validate([
                'role' => 'required',
                'est_earnings' => 'required',
            ])->validateWithBag('gig_error');
        }
            
        if($this->showDiv3){
            $self_data = $this->validate([
                 'business_type' => 'required',
                 'other_type' => 'required',
                 'date_com_trade' => 'required',
                 'no_of_emp' => 'required',
                 'business_registration_number' => 'required',
                 'office_ph_no' => 'required',
                 'share_of_bus' => 'required',
                 'last_2_year_profit' => 'required'
            ])->validateWithBag('self_error');
         }
                
         if($this->showDiv4){
             $investment_data = $this->validate([
                  'inv_name' => 'required',
                  'yearly_earning' => 'required',
             ])->validateWithBag('invest_error');
         }
         
         $user->user_current_step = Constant::CURR_STEP_COMMITMENT;
         $user->save();                    
                    
         alert('success','Your income are saved.');
                    
         return redirect()->route('borrower.landing');
                    
    }

So this code works for saving the user input from all the cards, however, for example when I chose two cards, and submit an empty form, error messages only appear in the first card. When tested individually, error messages appear for all the cards, so I am sure that its not the problem with the display of the messages. My assumption is that once the validation fails in the first card, it straight away return to the view before going through the other loops. Is there any way to prevent the controller returning to the view before it goes through all the validation?

Additional note, a senior suggested me to use error bags for each cards, thus explaining the validateWithBag('name) in the code, but from my research it seems like I can only pass one error bags to the view? And it also seems like validateWithBag() does automatic redirection.

Is there any way from my code or any alternative for me to get all the error messages displayed according to the cards selected?

CodePudding user response:

I got this answer from my facebook posting for this question and I thought I should share it here.

Answer:

Yes, the Laravel validate() method will stop execution of the code and return the errors. You could try running a validate() only once at the end of the if statements, inside each if statement, you should add the rules that represent your Livewire models.

Hope this answers your question and helps you out. The screencap on how to write the code

  • Related