Adding a conditional statement to an array of fields? - php

I need to include a conditional statement into the following array called $fields.
$fields = [
'program_id' => [
'type' => 'select',
'label' => 'Program',
'opts' => ["One", "Two", "Three"],
],
'name' => [
'label' => 'Job Name'
],
'start_date' => [
'class' => 'date-picker',
'label' => 'Job Starts' . $req,
'val' => $job->start_date ? dateToPicker($job->start_date) : null
],
'end_date' => [
'class' => 'date-picker',
'label' => 'Job Ends' . $req,
'val' => $job->end_date ? dateToPicker($job->end_date) : null
],
'section' => [
'type' => 'hidden',
'val' => 'details'
],
];
if (!$job->id && $program)
{
$fields['job_copy'] = [
'label' => 'Copy Job From',
'type' => 'select',
'textAsValue' => false,
'_comment' => 'Selecting a job here will copy all job information except the name.',
'opts' => array_replace([0 => '-- Select Job --'], $program->jobs()->lists('name', 'id')->all())
];
}
$fields[] = [
'type' => 'submit',
'label' => "Save",
'class' => 'btn btn-primary !important'
];
}
I need to move the conditional statement to the top so that it is the first thing displayed on the form. However, when I move it to the top it disappears. How can I integrate the conditional check into the top of the form as opposed to at the bottom where it currently displays?

$fields = [];
if (!$job->id && $program)
{
$fields['job_copy'] = [
'label' => 'Copy Job From',
'type' => 'select',
'textAsValue' => false,
'_comment' => 'Selecting a job here will copy all job information except the name.',
'opts' => array_replace([0 => '-- Select Job --'], $program->jobs()->lists('name', 'id')->all())
];
}
$fields2 = [
'program_id' => [
'type' => 'select',
'label' => 'Program',
'opts' => ["One", "Two", "Three"],
],
'name' => [
'label' => 'Job Name'
],
'start_date' => [
'class' => 'date-picker',
'label' => 'Job Starts' . $req,
'val' => $job->start_date ? dateToPicker($job->start_date) : null
],
'end_date' => [
'class' => 'date-picker',
'label' => 'Job Ends' . $req,
'val' => $job->end_date ? dateToPicker($job->end_date) : null
],
'section' => [
'type' => 'hidden',
'val' => 'details'
],
];
$fields = array_merge($fields,$fields2);
$fields[] = [
'type' => 'submit',
'label' => "Save",
'class' => 'btn btn-primary !important'
];

Related

how can i change fields from text to option box?

i want to change the type of car_engine from text to option but i do not even understand what this code means and in which language i shall write my modification.
i have tried no thing because i had no idea where should i start.
public static function front_fields( $fields ) {
$fields[] = [
'forms' => ['car', ],
'type' => 'fieldset',
'id' => 'car',
'legend' => esc_html__( 'Car', 'listing-manager' ),
'collapsible' => true,
'fields' => [
[
'id' => LISTING_MANAGER_LISTING_PREFIX . 'car_engine',
'type' => 'text',
'label' => esc_html__( 'Engine', 'listing-manager' ),
'required' => false,
],
Try to use this way (untested):
public static function front_fields( $fields ) {
$fields[] = [
'forms' => ['car', ],
'type' => 'fieldset',
'id' => 'car',
'legend' => esc_html__( 'Car', 'listing-manager' ),
'collapsible' => true,
'fields' => [
[
'id' => LISTING_MANAGER_LISTING_PREFIX . 'car_engine',
'type' => 'select',
'label' => esc_html__( 'Engine', 'listing-manager' ),
'required' => false,
'options' => [ //options may be values
'alabama' => 'Alabama',
'alaska' => 'Alaska'
],
'placeholder' => '',
],
]
);

Laravel backpack select_from_array

I am totally confused with select_from_array field in laravel backpack.
in my controller i am using a select_from_array field where in options i call a function,but when i run the code error is displayed. please help me with this.
Error : FatalErrorException in EventController.php line 106: syntax error, unexpected '$this' (T_VARIABLE)
controller.php
public $crud = array(
"model" => "App\Larapen\Models\Event",
"entity_name" => "event",
"entity_name_plural" => "events",
"route" => "admin/event",
"reorder" => true,
"reorder_label" => "name",
"reorder_max_level" => 2,
"details_row" => true,
// *****
// COLUMNS
// *****
"columns" => [
[
'name' => "id",
'label' => "ID"
],
],
"fields" => [
[
'name' => "event_name",
'label' => "Event name",
'type' => "text",
'placeholder' => "Event Name",
],
[
'name' => "event_topic",
'label' => "Event Topic",
'type' => "text",
'placeholder' => "Event Topic",
],
[
'name' => "event_type_id",
'label' => "Event Type",
'model' => "App\Larapen\Models\EventType",
'entity' => "eventType",
'attribute' => "name",
'type' => "select",
],
[
'name' => "about_event",
'label' => "About event",
'type' => "ckeditor",
'placeholder' => "About the Event",
],
[
'name' => "country_code",
'label' => "Country",
'type' => 'select_from_array',
'options' => $this->countries(),
'allows_null' => false,
],
],
);
public function countries()
{
..................
}
Please help me with this , why this happens? how to solve this issue?
Waiting for a response................
You can not use the pseudo-variable $this out of class method.
http://php.net/manual/en/language.oop5.properties.php
The pseudo-variable $this is available inside any class method when that method is called from within an object context. $this is a reference to the calling object
So if you want to set the attribute of crud's with $this, you can set it in the __construct function
public function __construct()
{
$this->crud['fields'][4] = $this->countries();
}
Or initialize it the __construct function
public $crud;
public function __construct()
{
$this->crud = array(
'model' => 'App\Larapen\Models\Event',
'entity_name' => 'event',
'entity_name_plural' => 'events',
'route' => 'admin/event',
'reorder' => true,
'reorder_label' => 'name',
'reorder_max_level' => 2,
'details_row' => true,
// *****
// COLUMNS
// *****
'columns' => [
[
'name' => 'id',
'label' => 'ID'
],
],
'fields' => [
[
'name' => 'event_name',
'label' => 'Event name',
'type' => 'text',
'placeholder' => 'Event Name',
],
[
'name' => 'event_topic',
'label' => 'Event Topic',
'type' => 'text',
'placeholder' => 'Event Topic',
],
[
'name' => 'event_type_id',
'label' => 'Event Type',
'model' => 'App\Larapen\Models\EventType',
'entity' => 'eventType',
'attribute' => 'name',
'type' => 'select',
],
[
'name' => 'about_event',
'label' => 'About event',
'type' => 'ckeditor',
'placeholder' => 'About the Event',
],
[
'name' => 'country_code',
'label' => 'Country',
'type' => 'select_from_array',
'options' => $this->countries(),
'allows_null' => false,
],
],
);
}

Preselecting an option from a select field in a fields array?

Observe the following $fields array, which is used as an input form in a blade:
$fields = [
'user_id' => [
'label' => 'Opportunity Owner' . $req,
'type' => 'select',
'class' => 'select2',
'opts' => User::whereTenantId(tenant()->id)->whereRole('Admin')->get()->lists('name', 'id')->all()
],
'name' => [
'label' => 'Opportunity Name' . $req,
],
'agent_id' => [
'label' => 'Agent',
'type' => 'select',
'class' => 'select2',
'textAsValue' => false,
'opts' => array_replace([0 => '-- Select Sales Rep --'],
User::whereTenantId(tenant()->id)->whereRole('Admin')->get()->lists('name', 'id')->all()),
],
'description' => [
'label' => 'Description',
'type' => 'textarea'
],
[
'type' => 'submit',
'label' => 'Save',
'class' => 'btn btn-primary !important'
]
];
In the 'agent_id' part, I'd like to pre-select a value if the user has a value preassigned. I know how to get the info from the user, but I am lost as to how to 'select' an option in the array within the 'agent_id' field. I need all options to show in the select, but I want to be able to have one 'selected' based on the agent_id number linked to the user. I tried the following:
'agent_id' => [
'label' => 'Agent',
'type' => 'select',
'class' => 'select2',
'textAsValue' => false,
'opts' => array_replace([0 => '-- Select Sales Rep --'],
User::whereTenantId(tenant()->id)->whereRole('Admin')->get()->lists('name', 'id')->all()),
'selected' => {{appropriate number here}}
],
But that did not work. How could I go about doing this?
Add selected value without any index.
Try this:
'agent_id' => [
'label' => 'Agent',
'type' => 'select',
'class' => 'select2',
'textAsValue' => false,
'opts' => array_replace([0 => '-- Select Sales Rep --'],
User::whereTenantId(tenant()->id)->whereRole('Admin')->get()->lists('name', 'id')->all()),
{{appropriate number here}}
],

Showing blank rows for filters in Yii2.0 with GridView

I have set up GridView to crfeate my table in Yii2.0 as follows:
<?= \yii\grid\GridView::widget([
'dataProvider' => $model->dataProvider,
'filterModel' => $model->searchModel,
'columns' => [
[
'label' => Yii::t( $cat, 'Id' ),
'value' => 'id',
],
[
'label' => Yii::t( $cat, 'Title' ),
'format' => 'raw',
'value' => function ( $data ) {
if ( $data['status_code'] != 5 )
{
return Html::a( $data['title'], '/signer/view/' . $data['id'] );
}
else
{
return $data['title'];
}
},
],
[
'label' => Yii::t( $cat, 'Description' ),
'value' => 'description',
],
[
'label' => Yii::t( $cat, 'Filename' ),
'value' => 'filename',
],
[
'label' => Yii::t( $cat, 'Status' ),
'value' => 'status',
'contentOptions' => function ( $data ) {
$statuses = [
1 => 'text-primary', # New
2 => 'text-warning', # Unsigned
3 => 'text-warning', # Partially signed
4 => 'text-success', # Signed
5 => 'text-danger', # Deleted
];
return [ 'class' => $statuses[$data['status_code']] ];
}
],
[
'label' => Yii::t( $cat, 'Created' ),
'value' => 'created',
],
//[ 'class' => 'yii\grid\ActionColumn' ],
],
]);
?>
I get all the correct data, but instead of filter inputs, I get empty rows.
Why is that? What am I missing?
PS: The search model itself works fine, meaning, when I add to the url ?title=asd it actually get the search results!
According to the documentation of the $filterModel property:
Note that in order to show an input field for filtering, a column must have its yii\grid\DataColumn::$attribute property set or have yii\grid\DataColumn::$filter set as the HTML code for the input field.
So you need to set yii\grid\DataColumn::$attribute property on your columns and in most of the cases this makes the value unnecessary:
<?= \yii\grid\GridView::widget([
'dataProvider' => $model->dataProvider,
'filterModel' => $model->searchModel,
'columns' => [
[
'label' => Yii::t( $cat, 'Id' ),
'attribute' => 'id',
],
[
'label' => Yii::t( $cat, 'Title' ),
'format' => 'raw',
'attribute' => 'title',
'value' => function ( $data ) {
if ( $data['status_code'] != 5 )
{
return Html::a( $data['title'], '/signer/view/' . $data['id'] );
}
else
{
return $data['title'];
}
},
],
[
'label' => Yii::t( $cat, 'Description' ),
'attribute' => 'description',
],
[
'label' => Yii::t( $cat, 'Filename' ),
'attribute' => 'filename',
],
[
'label' => Yii::t( $cat, 'Status' ),
'attribute' => 'status',
'contentOptions' => function ( $data ) {
$statuses = [
1 => 'text-primary', # New
2 => 'text-warning', # Unsigned
3 => 'text-warning', # Partially signed
4 => 'text-success', # Signed
5 => 'text-danger', # Deleted
];
return [ 'class' => $statuses[$data['status_code']] ];
}
],
[
'label' => Yii::t( $cat, 'Created' ),
'attribute' => 'created',
],
//[ 'class' => 'yii\grid\ActionColumn' ],
],
]);
?>
Another possible reason for the blank line: (not in posters exact case)
Missing/incorrect declaration of the public function rules() in the search model. In Yii 1 you could concatenate the string, in Yii2 they need to be actual array elements.
return [
[['authorId, title, publishFrom'], 'safe'], //WRONG
[['authorId', 'title', 'publishFrom'], 'safe'], //CORRECT
];

zend framework 2 with doctrine 2 $form->getData() in controller not returning all fields set using $form->setData()

I have set my form for validation using $form->setData().
After validation I am not receiving all of my properties back using $form->getData().
I am using following lines in controller
and somehow $form->getData() is not returning all fields anyone has any idea why?
if ($request->isPost())
{
$company = new Company();
$form->setInputFilter($company->getInputFilter());
$form->setData($request->getPost());
print_r($request->getPost()); // getPost shows all fields fine
if ($form->isvalid())
{
print_r($form->getData()); // is returning only select and text type fields which are in input filter. why?
}
}
my form is looks like this.
class Companyform extends Form
{
public function __construct()
{
parent::__construct('company');
$this->setAttribute ('method', 'post');
$this->setAttribute ('class', 'form-horizontal');
$this->add(array(
'name' => 'id',
'attributes' => array(
'type' => 'hidden'
),
));
$this->add ( array (
'name' => 'title',
'type' => 'Zend\Form\Element\Select',
'attributes' => array(
'id' => 'title',
'options' => array(
'mr' => 'Mr',
'miss' => 'Miss',
'mrs' => 'Mrs',
'dr' => 'Dr'
),
'value' => 'mr'
),
'options' => array(
'label' => 'Title'
)
));
$this->add(array(
'name' => 'fname',
'attributes' => array(
'id' => 'fname',
'type' => 'text',
'placeholder' => "First Name",
),
'options' => array(
'label' => 'First Name'
)
));
$this->add(array(
'name' => 'surname',
'attributes' => array(
'id' => 'surname',
'type' => 'text',
'placeholder' => "Surname Name",
),
'options' => array(
'label' => 'Surname Name'
)
));
$this->add(array(
'name' => 'companyName',
'attributes' => array(
'id' => 'companyName',
'type' => 'text',
'placeholder' => "Company Name",
),
'options' => array(
'label' => 'Company Name'
)
));
$this->add(array(
'name' => 'address1',
'attributes' => array(
'id' => 'address1',
'type' => 'text',
'placeholder' => "Address Line 1",
),
'options' => array(
'label' => 'Address'
)
));
$this->add(array(
'name' => 'address2',
'attributes' => array(
'id' => 'address2',
'type' => 'text',
'placeholder' => "Address Line 2",
),
'options' => array(
'label' => 'Address'
)
));
$this->add(array(
'name' => 'address3',
'attributes' => array(
'id' => 'address3',
'type' => 'text',
'placeholder' => "Address Line 3",
),
'options' => array(
'label' => 'Address'
)
));
$this->add(array(
'name' => 'btnsubmit',
'attributes' => array(
'id' => 'btnsubmit',
'type' => 'submit',
'value' => 'Add',
'class' => 'btn btn-primary'
),
));
}
}
and This is the input filter which I am using in entity company
public function setInputFilter(InputFilterInterface $inputFilter)
{
throw new \Exception("Not used");
}
public function getInputFilter()
{
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$factory = new InputFactory();
$inputFilter->add($factory->createInput(array(
'name' => 'companyName',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 2,
'max' => 255,
),
),
),
)));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
Every single Form-Element has to get validated! Even if the validator is empty like
$inputFilter->add($factory->createInput(array(
'name' => 'title'
)));
Only validated data get's passed from the form.

Categories