disable encode html in activeform field dropdown yii2 - php

How can disable encode html in ActiveForm::Dropdown active form Yii2?
I want to create a select html tag that shows multilevel data so that children make fixed padding than its parents. So, I create an array like this:
$items = [
'Computer'
' Hardware'
' Software',
' Programming'
'&nbps; C#'
];
But space is removed and &nbps; encoded and both not worked. We can use pure html tag, but how can create it using Yii2::ActiveField?
Note that we can encode items before calling widget based on our conditions.
There is any idea?!

To retain the spaces,
echo $form->field($model, 'attribute')->dropDownList($data, [
'encodeSpaces' => true,
]);

Related

Inject Blade Array into script using HTML data attribute

I have a blade template, which has a script included in it. That script needs access to language strings, which are accessible in a #lang('intro') variable. I am wondering if there is way of injecting the whole array into the script via a html data attribute, and then retrieve it in the script using jquery.
So far I have the following:
../en/intro.php:
<?php
return [
'step1' => 'Welcome',
'step2' => 'Step 2',
];
../de/intro.php
<?php
return [
'step1' => 'Willkommen',
'step2' => 'Schritt 2',
];
In the blade template I am injecting each string:
<script data-id="intro-script"
data-introStep1="#lang('intro.step1')"
data-introStep2="#lang('intro.step2')"
src="{{ mix('js/intro.js') }}">
And retrieve it using jQuery in the script intro.js:
document.querySelector('script[data-id="intro-script"]').getAttribute('data-introStep1');
This works so far, but isn't great for many more strings. I am wondering if its possible to combine data-introStep1 and data-introStep2 in a single data attribute which contains the whole #lang('intro') array, not just a single string per attribute.
Your translation file can be packed as JSON encoded string like so:
<script data-id="intro-script"
data-intro='#json(__('intro'))'
src="{{ mix('js/intro.js') }}"
></script>
Then, retrieve using Javascript.
const intro = JSON.parse(
document.querySelector('script[data-id="intro-script"]').dataset.intro
);

PHP - Array of termins as form option (LIMIT array element usage)

Let me just explain you my problem. I am working on a php form template which could be useful for more events in our company. In this form I collect few things about our employees but that is out of question.
My problem is that in form I have HTML SELECT element, where are some OPTIONs inside. I have this option in PHP array, for better imagination for example termins for Blood donation. I need to handle that only one employee can sign in for specific termin.
Can I somehow limit "the usage of array element"? I mean, when someone has already signed in for termin X, just don't allow another employee to pick this termin.
Thanks.
<-- EDIT -->
here is the code:
This is my config.php (the file, where all variables are configured
$termins = array(
'8:00' => '',
'8:30' => '',
'9:00' => '',
'9:30' => '',
'10:00' => '',
'10:30' => '',
'11:00' => ''
);
and here is index.php where I fill the options into select element
<?php
foreach($termins as $key => $value) {
echo '<option value="myvalue">' . $key . ' </option>';
}
?>
I think that "Disable" is the way how to solve this. When the option is already picked, i just have to change the OPTION to "Disabled". But this comes to my second question: How to change the property of html OPTION element to Disabled and keep this change? Should I use php sessions?
First of all, that HTML SELECT elements are in database? If yes, doing a LEFT JOIN between form's registered users table and HTML SELECT options table you will have elements that do not match between those two tables.

yii2 DetailView template/layout without values

I have a little problem in yii2 framework.
I have DetailView widget
<?= DetailView::widget([
'model' => $table_1,
'attributes' => [
'year',
'table_zs_field_1',
'table_zs_field_2',
'table_zs_field_3',
'table_zs_field_4',
'table_zs_field_5',
'table_zs_field_6',
'table_zs_field_7',
'table_zs_field_8',
'table_zs_field_9',
'table_zs_field_10',
'table_zs_field_11',
'table_zs_field_12',
'table_zs_field_13',
'table_zs_field_14',
'table_zs_field_15',
'table_zs_field_16',
'table_zs_field_17',
'table_zs_field_18',
'table_zs_field_19',
],
]) ?>
If i write this to code I'll see a DetailView widget with names of fields(get from model) and values.
Problem: I want to hide values and show only names of fields from model and in next time hide names and show only values. Anybody know ?
Change the $template property of the Detailview.
The Default is
$template = '<tr><th>{label}</th><td>{value}</td></tr>'
Adding
'template'=>'<tr><th>{label}</th></tr>' ,
to the config array of your DetailView should show only the names of the fields.
Adding
'template'=>'<tr><td>{value}</td></tr>',
should show only the value.
See the corresponding section in the Documentation of DetailView.

Generate select without optgroup on dropDownList in Yii

I am using dropDownListRow of bootstrap Yii.
<?php echo $form->dropDownListRow($model, 'roles', array('' => "--Select Roles--", 'Trekking Agency' => CHtml::listData(Roles::model()->findAll(), 'idRole', 'name'))); ?>
It renders my select within the optgroup. I do not want the options in optgroup. What is the way to remove optgroup from the select in Yii?
It looks like you are using ListData within an array and that array has another element, optgroups will be created if you have an array within array structure. For creating empty options use empty htmlOptions attribute as below (see here for details)
<?php echo $form->dropDownListRow($model, 'roles',
CHtml::listData(Roles::model()->findAll(), 'idRole', 'name'),
array('empty'=>'--Select Roles---')); ?>
If you use Yii 2.0++
And items = array(arra(item01=>1), array(item02=>2))
you need this:
use yii\helpers\ArrayHelper;
<?= Html::dropDownList('Name', 'SelectedItem', ArrayHelper::getColumn($listItems, 'key_name')); ?>

Yii Framework: Horizontal activeCheckboxList?

I'm trying to use an activeCheckboxlist in a form in my Yii site. When generating the checkboxlist, Yii automatically puts a < br > between the checkboxes.
Is there any way to avoid/override this except with CSS?
Yes, you can do that by give 'separator' param in htmlOptions
echo CHtml::activeCheckboxList($model, $attribute, $dataArr,
array(
//..
'separator'=>'|', //new separator. html allowed also
//'template'=>'<span class="myItem">{label} {input}</span>', // use template to customize each item
//..
));
http://www.yiiframework.com/doc/api/1.1/CHtml#activeCheckBoxList-detail

Categories