mardi 25 octobre 2022

Bug on checkboxes in laravel livewire

I'm having a doble problems with my checkboxes on livewire component.

I'm trying to save in my form's table two checkboxes. This checkboxes take their value in an other table named "Garde_type". Checkbox "home" save in my form's table the ID 1, checkbox "visit" save ID 2. Here all is simple...but

Laravel livewire ask me property rules to save my data. Ok ! But when i put in rules my 2 checkboxes : in my form the checkbox can't be checked..when i check them they check/uncheck immediately = first problem. Second problem : no matter check/uncheck...everytime i submit my form, the 2 checkboxes are consider "checked" and then saved in my DB.

Here you can take a look to my code, i tried many things but i have no idea any more.

This is the livewire component "controler" :

class VilleSelect extends Component {     

public $visit;
public $home;
public $user_id;

protected function rules() {

 return [     
    
   'visit' => 'nullable',
   'home' => 'nullable',
];
    }


public function submit() {

   $annonces=annonces::create([
        'home' => $this->home->id,
        'visit' => $this->visit->id,
    ]);
    
    $annonces->save();
       
}

This is the checkboxes :

 <div class="mt-4 space-y-4">
     <div class="flex items-start">
          <div class="flex h-5 items-center">
            <x-jet-input wire:model="home" value="" type="checkbox"/>
          </div>
          <div class="ml-3 text-sm">
            <x-jet-label for="home" value=""/>
          </div>
      </div>
      <div class="flex items-start">
          <div class="flex h-5 items-center">
            <x-jet-input wire:model='visit' value="" type="checkbox"/>
          </div>
                
          <div class="ml-3 text-sm">
            <x-jet-label for="visit" value=""/>
          </div>
       </div>               
  </div>



Aucun commentaire:

Enregistrer un commentaire