LaravelCollective / html

HTML and Form Builders for the Laravel Framework
MIT License
3.99k stars 783 forks source link

for the AJAX #740

Open tszyuloveyou opened 1 year ago

tszyuloveyou commented 1 year ago

if create and edit include xxx.form, than xxx.create using open method and xxx.edit using model method when I need to add parameters to FormData and this two page share one js the js may be like this:

if( document.getElementById( "method" ) ) {
    formData.append( "_method", "PUT" );
}

the hidden input of _method have no ID

So, I suggest Collective\FormBuilder::getAppendage() from:

if (in_array($method, $this->spoofedMethods)) {
    $appendage .= $this->hidden('_method', $method);
}

change to like:

if (in_array($method, $this->spoofedMethods)) {
    $appendage .= $this->hidden('_method', $method,['id'=>'method']);
}

or other id name

themuddfamily commented 1 year ago

if create and edit include xxx.form, than xxx.create using open method and xxx.edit using model method when I need to add parameters to FormData and this two page share one js the js may be like this:

if( document.getElementById( "method" ) ) {
    formData.append( "_method", "PUT" );
}

the hidden input of _method have no ID

So, I suggest Collective\FormBuilder::getAppendage() from:

if (in_array($method, $this->spoofedMethods)) {
    $appendage .= $this->hidden('_method', $method);
}

change to like:

if (in_array($method, $this->spoofedMethods)) {
    $appendage .= $this->hidden('_method', $method,['id'=>'method']);
}

or other id name

Unfortunately this can't be done because ids need to be unique per page and there is likely more than 1 form.

In my fork I have added "method_attributes" as an option you can use on Form::open / Form::model. https://github.com/LaravelLux/html

tszyuloveyou commented 1 year ago

如果創建和編輯包包括xxx.form,比xxx.create使用open方法和xxx.edit使用模型方法,當我需要向FormData添加參數並與這兩個頁面共享時一個js時,js可能是這樣的:

if( document.getElementById( "method" ) ) {
    formData.append( "_method", "PUT" );
}

_method 的隱藏輸入沒有ID 所以,我建議 Collective\FormBuilder::getAppendage() 來自:

if (in_array($method, $this->spoofedMethods)) {
    $appendage .= $this->hidden('_method', $method);
}

改成喜歡:

if (in_array($method, $this->spoofedMethods)) {
    $appendage .= $this->hidden('_method', $method,['id'=>'method']);
}

或者其他ID名稱

不幸的是,這無法完成,因為每個頁面的ID必須是唯一的,而且可能有不止一種形式。

在我的fork中,我添加了“method_attributes”作為您可以在Form::open / Form::model上使用的選項。https://github.com/LaravelLux/html

Oh I miss multiple form option, so sorry if the "method_attributes" is for other attributes that is good idea but need user to add attributes to find the form method that may not be good idea if id is unique that we can add dataset using ajax must define form element to preventDefault, and than I believe that most of the people will define

const form = document.getElementById( "{form id}" );
const action = form.action;

so, if set data-method to form attribute may be good idea on https://github.com/LaravelCollective/html/blob/6.x/src/FormBuilder.php#L138 add: $attributes['data-method'] = $method;

user just need to define const method = form.dataset.method;