Populating nested dropdowns using backpack for Laravel - php

Has anyone here ever created a nested dropdown within the backpack's cruds?
I've got this crud controller which handles the 'Campaign' model -
$this->crud->addField([
'name' => 'industry_id',
'entity' => 'industry',
'type' => 'select2',
'attribute' => 'name',
'label' => "Industry",
]);
$this->crud->addField([
'name' => 'country_id',
'entity' => 'country',
'type' => 'select2',
'attribute' => 'country_name',
'label' => "Country",
]);
$this->crud->addField([
'name' => 'website_id',
'entity' => 'website',
'type' => 'select2',
'attribute' => 'name',
'label' => "Website",
]);
$this->crud->addField([
'name' => 'campaign_type_id',
'entity' => 'campaign_type',
'type' => 'select2',
'attribute' => 'name',
'label' => "Campaign Type",
]);
$this->crud->addField([
'name' => 'site_page_id',
'entity' => 'site_page',
'type' => 'select2',
'attribute' => 'name',
'label' => "Site Page",
]);
The 'website' entity depends on the industry field, and the 'site_page_id' depends on the cross between the 'website' entity and the 'campaign_type' entity.
When I'm creating a new campaign, I'm willing the dropdowns to be filled up dynamically according to the selected value of the dropdown above them.
I tried adding a method to the campaign's model as suggested in this pull and call it within the crud field's but it didn't work.
The only solution I can think of now is creating a custom create.blade.php file and call it with $this->crud->setCreateView('vendor.backpack.base.new_create'); from campaign's model setup() method.

You can use
$this->crud->with();
or
$this->crud->with(['website', 'site_page']);
It will get all the duplicate data in the relationships.
Let me know if this helped

Related

DoctrineModule\Form\Element\ObjectSelect - Selected values and auto submit on change

I have the following code of select form:
$this->add([
'name' => 'username',
'type' => 'DoctrineModule\Form\Element\ObjectSelect',
'options' => [
'object_manager' => $this->getObjectManager(),
'target_class' => 'Application\Entity\Player',
'property' => 'username',
'is_method' => true,
'empty_option' => '-- Please select Unsername --',
'find_method' => [
'name' => 'getUsername',
],
'label_generator' => function ($targetEntity) {
return $targetEntity->getId() ;
},
],
]);
My questions are:
Is it posible selected value to display as default?
(example: https://www.w3schools.com/tags/att_option_selected.asp)
Is it possible to do submit when changing position
Thank in advance for your help

zend framework 3 Select tag validation

I am getting the error:
The input was not found in the haystack
After form post. Please see the selection tag code lines below:
// Add "roles" field
$this->add([
'type' => 'select',
'name' => 'roles',
'attributes' => [
'multiple' => 'multiple',
'options'=>$this->role_desc,
'inarrayvalidator' => false,
'class'=>'form-control'
],
'options' => [
'label' => 'Role(s)',
],
]);
// Add input for "roles" field
$inputFilter->add([
'class' => ArrayInput::class,
'name' => 'roles',
'required' => true,
'haystack'=>$this->role_ids,
'filters' => [
['name' => 'ToInt'],
],
'validators' => [
['name'=>'GreaterThan', 'options'=>['min'=>1]],
['name'=>'InArray', 'options'=>['haystack'=>$this-
>role_ids]]
],
]);
The InArray seems to be validating well, lm just no sure what is bringing up the exception. Thank you in advance.
Actually , your issue is similar to link
To solve this, change your validators definition to:
'validators' => [
['name'=>'GreaterThan', 'options'=>['min'=>1]],
[
'name' => 'Explode',
'options' => array(
'validator' => [
'name'=>'InArray',
'options'=> [
'haystack'=>$this->role_ids
]
]
)
]
],
Unfortunately, I do not think there is a "cleaner" way to do this.
Alternately, Maybe you could use the MultiCheckbox.

How to create a custom action column in kartik gridview

I am trying to create a column that displays a glyphicon. The glyphicon will link to an url which allows the user to download a file. Any help would be massively appreciated. Current code is as follows:
GridView::widget([
'dataProvider' => $dataProvider,
'pager' => [
'class' => 'common\widgets\Pagination',
],
'columns' => [
['class' => 'yii\grid\SerialColumn'],
[
'label' => 'Date',
'attribute' => 'call_datetime',
'format' => 'date',
],
[
'label' => 'Time',
'attribute' => 'call_datetime',
'format' => 'time',
],
'call_from',
'call_to',
'duration',
'call_type',
'extension',
[
'label' => 'File',
'attribute' => 'fname',
'value' => 'callRecFiles.fname',
],
It is the last attribute 'fname' that the user will be downloading.
Change your fname field array to:
[
'label' => 'File',
'attribute' => 'fname',
'value' => function($model) {
//here create glyphicon with URL pointing to your action where you can download file, something like
return $model->callRecFiles ? Html::a('Download', ['myController/download-action', 'fname' => $model->callRecFiles->fname]) : null;
}
],
And prepare proper action to allow user to download file.

CodeIgniter Form information

I have a controller with a add() function and create() function. The add function posts to create.
The form is displayed using the form helper. In the add() function, I have an array to set the form input attributes that looks like the following:
$this->data['form'] = array(
'label_attributes' => array(
'class' => 'col-lg-2 control-label'
),
'media_name' => array(
'class' => 'form-control',
'id' => 'media_name',
'name' => 'media_name',
'value' => set_value('media_name')
),
'media_link' => array(
'class' => 'form-control',
'id' => 'media_link',
'name' => 'media_link',
'value' => set_value('media_link')
),
'media_width' => array(
'class' => 'form-control',
'id' => 'media_width',
'name' => 'media_width',
'size' => '4',
'maxlength' => '4',
'value' => ($this->form_validation->set_value('media_width')) ? $this->form_validation->set_value('media_width') : '640'
),
'media_height' => array(
'class' => 'form-control',
'id' => 'media_height',
'name' => 'media_height',
'value' => ($this->form_validation->set_value('media_height')) ? $this->form_validation->set_value('media_height') : '360'
),
'media_description' => array(
'class' => 'form-control',
'id' => 'media_desription',
'name' => 'media_desription',
'value' => $this->form_validation->set_value('media_desription')
)
);
When I post to the create() function, I lose access to the data['form'] values. Should all this information just be in the view or is it possible to put it in the model so I can load it whenever needed? When I tried to put it in the model, I had issues with the 'value' attributes even if I loaded the form_validation library in the model.
Because the controller class is renewed when a new request comes, so the problem with your code is that, $this->data is created when you visit the add function, while when you post to create function, the controller class is renewed again, there is no $this->data at all at this time.
If you want to pass data from one request to another request, you can pass the data via view or model.
Hope helps!

Yii CDetailView translate sub-property

<?php
$this->widget('zii.widgets.CDetailView', array(
'data' => $model,
'htmlOptions' => array('style' => 'width:425px; float:left; margin-right:20px; word-break:break-all', 'class' => 'detail-view table table-striped table-condensed'),
'attributes' => array(
'id',
'name',
'street',
'housenumber',
'zipcode',
'city',
array(
'label' => Yii::t('api', 'Country'),
'name' => Yii::t('api', 'country.name'),
),
),
));
?>
I want to translate the values for the attributes label and name in the array for the columns.
But it only translate the label and not the name.
Can someone tell me, what I am doing wrong?
name is the column name in the database, I think you want to adjust value, try this:
'label' => Yii::t('api', 'Country'),
'name' => 'country.name', //in reality you don't need this since you are setting the value
'value' => Yii::t('api', $model->country->name),

Categories