Puneet Sharma
Puneet Sharma

Reputation: 103

Laravel Form Submit should only work with submit button in livewire

First I create a form and wire it to function But it have two button and one button for form submit and other one for reset everything. But if any button press it will submit after run resetcategory() function but after it run submitcategory() i can't get i use some workaround with if($this->name) but i don't want to use that. Please Help?

My php livewire file is

public function resetcategory()
{
    $this->reset();
}

public function submitcategory()
{
     if ($this->name)
    {
    Category::create([
        'name' => $this->name,
        'parent' => $this->parent,
    ]);
    $this->reset();
    }
}

my blade file is---

 <form wire:submit.prevent="submitcategory">
      <div class="flex items-center justify-end px-4 py-3 bg-gray-50 text-right sm:px-6 shadow sm:rounded-bl-md sm:rounded-br-md">
        <!-- here is input content-->
        <button class="inline-flex items-center px-4 py-2 bg-gray-800 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-gray-700 active:bg-gray-900 focus:outline-none focus:border-gray-900 focus:ring focus:ring-gray-300 disabled:opacity-25 transition mr-2 ml-2" wire:click="resetcategory()">Reset</button>
    
        <button type="submit" class="inline-flex items-center px-4 py-2 bg-gray-800 border border-transparent rounded-md font-semibold text-xs text-white uppercase tracking-widest hover:bg-gray-700 active:bg-gray-900 focus:outline-none focus:border-gray-900 focus:ring focus:ring-gray-300 disabled:opacity-25 transition">Send</button>
      </div>
    </form>

Upvotes: 3

Views: 6216

Answers (1)

Peppermintology
Peppermintology

Reputation: 10210

This is because you have not specified a type on your Reset button. The default type for a button (if a type is not provided) is submit. So your reset button is actually submitting the form too.

Add type="button" on your Reset button to prevent it submitting the form.

<button type="button" wire:click="resetcategory()">Reset</button>

Upvotes: 12

Related Questions