【问题标题】:Laravel Livewire, form submit: do something first then submit the formLaravel Livewire,表单提交:先做点事,然后提交表单
【发布时间】:2021-09-25 12:40:00
【问题描述】:

我有一个 livewire 表单,我打算将其提交到外部 url。但是,在提交之前,我想以编程方式添加一些用户无法编辑的隐藏输入,然后最终提交表单:

 <form action="some-external-url" wire:submit.prevent="processForm" method="post">
                <x-inputs.text-input wire:model="amount" name="amount" />
                <x-inputs.button title="Submit" />
            </form>

类似于这个 jQuery 代码的东西:

$('form').submit( function(ev){
         ev.preventDefault();
         //fetch and add some additional fields to the form
         // finally submit the form
         $(this).unbind('submit').submit()

  });

我怎样才能最好地使用 livewire 来实现这一点。请注意,我不打算使用 guzzle 提交此表单。

【问题讨论】:

  • 为什么不从您的processForm 方法向外部网址发送一个帖子请求?
  • 请教我怎么做!!
  • 你可以使用Http::asForm()-&gt;post('external-url.com', ['amount' =&gt; $this-&gt;amount]);laravel.com/docs/8.x/http-client#request-data

标签: laravel laravel-8 laravel-livewire


【解决方案1】:

如果你想在你的组件中设置属性,你需要,然后在你的 mount 方法中,你初始化这些属性的值。 见forms in livewire

class ComponentName extends Component
{
   public $hidden_val;
   


  public function mount() 
  {
     $this->hidden_val = "my_hidden_val";
  }
}

然后用 livewire 传递它

<input type="hidden" wire:model="hidden_val">

但我也认为 @ClémentBaconnier 并建议使用 laravel 在控制器中提供的 Http Client 或 livewire 组件中的事件将表单数据传递给外部链接。

Http::asForm()->post('some-external-url', ['form_data' => /*your form data*/]);

关注here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-18
    • 1970-01-01
    • 2016-07-30
    相关资源
    最近更新 更多