backpack-for-laravel 如何在 ajax 选择时更改字段属性
backpack-for-laravel How to change the field attribute upon ajax selection
我的 crud 中有一个字段
$this->crud->addField([
'name' => 'transaction_type',
'label' => 'Transaction Type',
'type' => 'select2',
'entity' => 'transactionType',
'attribute' => 'name',
'model' => 'App\Models\TransactionType',
'attributes' => [
'required' => true,
],
]);
$this->crud->addField([
'name' => 'developer_id',
'label' => 'Developer',
'type' => 'select2',
'entity' => 'developer',
'attribute' => 'name',
'model' => 'App\Models\Developer',
]);
我想将 developer_id 所需的属性设置为 true,因为默认情况下它是 false。我知道如何使用下面的代码使它为假
$this->crud->modifyField('developer_id',['attributes' => [
'required' => true,
]]);
现在的问题是,在transaction_type
中选择一个选项,如何让developer_id
变为'required' => true
?
到目前为止我得到的是将 'data_source' => url("admin/api/devrequirement")'
放在 transaction_type 的 addfield 中。所以它看起来像这样
$this->crud->addField([
'name' => 'transaction_type',
'label' => 'Transaction Type',
'type' => 'select2',
'entity' => 'transactionType',
'attribute' => 'name',
'data_source' => url("admin/api/devrequirement"), //i added it here
'model' => 'App\Models\TransactionType',
'attributes' => [
'required' => true,
],
]);
然后在我的自定义路线中放置一条路线:
Route::get('/api/devrequirement', 'SaleCrudController@setDevRequirement');
该函数内部是:
public function setDevRequirement(Request $request)
{
$form = collect($request->input('form'))->pluck('value', 'name');
$transaction_type = $form['transaction_type'];
if($transaction_type == 3) //if transaction type is equal to a specific value then required attribute of developer_id should be true
{
$this->crud->modifyField('developer_id',['attributes' => [
'required' => true,
]]);
}
}
但它什么也没做。它没有在我的开发人员字段上放置红色星号,也没有要求验证。我很难理解 laravel 背包以及如何使用它来操纵 ajax。
没关系,我只是制作自定义字段并在其中进行一些 js 操作
我的 crud 中有一个字段
$this->crud->addField([
'name' => 'transaction_type',
'label' => 'Transaction Type',
'type' => 'select2',
'entity' => 'transactionType',
'attribute' => 'name',
'model' => 'App\Models\TransactionType',
'attributes' => [
'required' => true,
],
]);
$this->crud->addField([
'name' => 'developer_id',
'label' => 'Developer',
'type' => 'select2',
'entity' => 'developer',
'attribute' => 'name',
'model' => 'App\Models\Developer',
]);
我想将 developer_id 所需的属性设置为 true,因为默认情况下它是 false。我知道如何使用下面的代码使它为假
$this->crud->modifyField('developer_id',['attributes' => [
'required' => true,
]]);
现在的问题是,在transaction_type
中选择一个选项,如何让developer_id
变为'required' => true
?
到目前为止我得到的是将 'data_source' => url("admin/api/devrequirement")'
放在 transaction_type 的 addfield 中。所以它看起来像这样
$this->crud->addField([
'name' => 'transaction_type',
'label' => 'Transaction Type',
'type' => 'select2',
'entity' => 'transactionType',
'attribute' => 'name',
'data_source' => url("admin/api/devrequirement"), //i added it here
'model' => 'App\Models\TransactionType',
'attributes' => [
'required' => true,
],
]);
然后在我的自定义路线中放置一条路线:
Route::get('/api/devrequirement', 'SaleCrudController@setDevRequirement');
该函数内部是:
public function setDevRequirement(Request $request)
{
$form = collect($request->input('form'))->pluck('value', 'name');
$transaction_type = $form['transaction_type'];
if($transaction_type == 3) //if transaction type is equal to a specific value then required attribute of developer_id should be true
{
$this->crud->modifyField('developer_id',['attributes' => [
'required' => true,
]]);
}
}
但它什么也没做。它没有在我的开发人员字段上放置红色星号,也没有要求验证。我很难理解 laravel 背包以及如何使用它来操纵 ajax。
没关系,我只是制作自定义字段并在其中进行一些 js 操作