I have a field in my 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',
]);
I want to make the attribute required for developer_id to true since it is false by default. I know how to make it false by using this code below
$this->crud->modifyField('developer_id',['attributes' => [
'required' => true,
]]);
The question now is, how do I make developer_id
to 'required' => true
upon selecting an option in transaction_type
?
What I've got so far is to put 'data_source' => url("admin/api/devrequirement")'
inside the addfield of transaction_type. so it would look like this
$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,
],
]);
and then put a route inside my custom routes:
Route::get('/api/devrequirement', 'SaleCrudController@setDevRequirement');
and inside that function is:
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,
]]);
}
}
But it doesnt do anything. it didnt put red asterisk on my Developer field nor asking for validation. I'm having a hard time about laravel backpack and how to manipulate ajax with it.
public function setDevAndProjectRequired(Request $request) { $this->crud->modifyField('developer_id',['attributes' => [ 'required' => true, ]]); echo "being fired"; }
– rochiey