Displaying yii2 checkboxlist vertically - php

I have an active form which currently displays checkboxlists horizontally but I would like it to display them vertically. I have set the form layout to vertical but it still displays them horizontally:
This is what I have tried:
//generates an array of permissions
$options = Permission::value_list(
Permission::findWhere()->select('name')->andWhere(['not', ['name' => $name]])->all(),
['name']
);
This is the form
<?php $form = ActiveForm::begin(['layout' => 'vertical']); ?>
<?= $form->field($model, 'item_children')
->checkboxList($options)->label(sprintf("Available %s", $assigning))
->hint("Which type of authorization item are you creating") ?>
What do I need to add: currently they are displayed in this way.
I would like the displayed vertically.

You could use the separator option mentioned here: http://www.yiiframework.com/doc-2.0/yii-helpers-basehtml.html#activeCheckboxList()-detail
Your call would then look like this for example:
<?php $form = ActiveForm::begin(['layout' => 'vertical']); ?>
<?= $form->field($model, 'item_children')
->checkboxList($options, ['separator'=>'<br/>'])
->label(sprintf("Available %s", $assigning))
->hint("Which type of authorization item are you creating") ?>
Or whatever you want to use as the separator in your specific case.
Hope that helps...

The label to use Note that this will NOT be encoded.
<?php $form = ActiveForm::begin(['layout' => 'vertical']); ?>
<?= $form->field($model, 'country')
->inline(true)
->checkboxList($options)->label(sprintf("Available %s", $assigning))
->hint("Which type of authorization item are you creating") ?>

Related

how to get data from Input Form Cakephp 3

I am very new to this Cakephp framework, trying to follow and understand how to upload files using cakephp. I am using upload plugin provided by josediazgonzalez. In the view file, using formhelper i have:
<?= $this->Form->create($user, ['type' => 'file']) ?>
<fieldset>
<legend><?= __('Add User') ?></legend>
<?php
echo $this->Form->control('name');
echo $this->Form->control('username');
echo $this->Form->control('password');
echo $this->Form->control('role');
echo $this->Form->input('photo', ['type' => 'file']);
echo $this->Form->control('dir');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
I want to print again the value that i submitted in my controller, what should i write? Something like:
$this->request->data;
$this->request->data is an array of data passed to your scirpt. You can use it to get field that you are interested in with, eg:
$data = $this->request->data;
$someVariable = $data["name"];
Or, you can access any field directly, by using data() accessor:
$someVariable = $this->request->data("name");
From this point, you can do anything you want with this variable.
One more thing - as $this->request->data and $this->request->data() are currently in deprecated state and will be removed in next version, I suggest to use $this->request->getData() instead. Usage is similar:
$this->request->getData(); //will return array of data passed
$this->request->getData("field_name"); //access to specific field

Take data from another field in yii2 form

I'm making a form with yii2, now I have two field:
<?php echo $form->field($model, 'Protocol')->textInput(['maxlength' => true])->dropDownList(
array("rtsp://"=>"rtsp","rsmt://"=>"rsmt","http://"=>"http"), // Flat array ('id'=>'label')
['prompt'=>'Select'] // options
); ?>
<?php echo $form->field($model, 'url')->textInput(['maxlength' => true]); ?>
They look like this:
How can I take the selection from Protocol's drop down list and automatically add it to the below URL field? Like this: I type the http:// in the field manually, is there anyway I can make it automatic?
Add onchange event in your 'protocol' dropdownList. show below code
<?= $form->field($model, 'Protocol')->dropdownList(["rtsp://"=>"rtsp","rsmt://"=>"rsmt","http://"=>"http"], [
'onchange'=>'$( "#'.Html::getInputId($model, 'url').'").val($(this).val());'
]) ?>
<?= $form->field($model, 'url')->textInput(['maxlength' => true]);
?>

Is it possible to redirect using dropdown onchange using value as parameter?

Using Yii2 I have the following code which displays a dropdown with my list of categories.
I was wondering is it possible so that on change the action executed is localhost:8888/article/category?id=1 rather that what it is atm (localhost:8888/article/category).
Does this require AJAX and/or JS or can it be done just using php?(id prefer php)
<?php $form = ActiveForm::begin(['id' => 'category-form', 'action' =>
'/advanced/article/category',]); ?>
<?= $form->field($model, 'category')->dropDownList($model->categoryList,[
'prompt'=>'Select Category to view',
'onchange'=>'this.form.submit()'
]) ?>
<?php ActiveForm::end(); ?>
You dont need ajax for this. js will do it.
<?= $form->field($model, 'category')->dropDownList($model->categoryList,[
'prompt'=>'Select Category to view',
'id'=>'cat-id',
]) ?>
And js to redirect when selected
$this->registerJs(
'$(document).ready(function(){
$("#cat-id").change(function(){
var e = document.getElementById("cai-id");
var strSel = e.options[e.selectedIndex].value;
window.location.href="'.Yii::$app->urlManager->createUrl('category?id=').'" + strSel;
});
});', View::POS_READY);
If you want use onchange attribute without writing any additional javascript, you need correct your code a bit more to get it working.
1) Change you form method to get (by default it's post):
<?php $form = ActiveForm::begin([
'id' => 'category-form',
'action' => '/advanced/article/category',
'method' => 'get', // Add this to your code
]); ?>
2) Explicitly set name of the category field in dropDownList options to avoid wrapping in formName():
<?= $form->field($model, 'category')->dropDownList($model->categoryList, [
'prompt'=>'Select Category to view',
'onchange'=>'this.form.submit()',
'name' => 'category', // Add this to your code
]) ?>
sorry you can make a onchange example but this time with a radiolist in yii2
field($model, 'estadoLaboral')->radioList(array('T'=>'Actualmente trabajo aquí','B'=>'Ya no trabajo aquí')) ?>
using javascript

Basic Hidden field in yii

I'm trying to place data in hidden text in yii, but I don't know how.
I need a similar code to a regular php syntax:
<input type="hidden" name="field_name" value="a"/>
It's supposed to be a field with static value of a. I just need it to go with my $_POST variables for error checking.
Is it possible to avoid modifying the models and controllers just to put the field in?I cant use gii cause I only have snippets of code with me.Sorry as well as I have little understanding of yii so I have no clue if what I'm saying about the last 2 sentences is correct.
in views
hidden field with model and form:
<?php echo $form->hiddenField($model, 'name'); ?>
or without model
<?php echo CHtml::hiddenField('name' , 'value', array('id' => 'hiddenInput')); ?>
Yii hidden input :
<?php echo $form->hiddenField($model,'fieldName',array('value'=>'foo bar')); ?>
In Yii2 this has changed too:
<?= Html::activeHiddenInput($model, 'name') ;?>
References:
http://www.yiiframework.com/forum/index.php/topic/49225-activeform-how-do-you-call-label-input-and-errors-individually/
https://github.com/yiisoft/yii2/issues/735
if data from database and value or size field:
echo $form->hiddenField($experience,'job_title',array('size'=>'50','value'=>$experience_data['job_title'])); ?>
Yii 1
<?php echo $form->hiddenField($model, 'name'); ?>
Yii2
<?= Html::activeHiddenInput($model, 'attribute', ['value' => 'Some Value']) ?>
Also, worth noting for Yii2, the array parameter works different to a normal form field.
E.G. A normal input would look more like this.
<?= $form->field($model, 'attribute', ['inputOptions' => ['placeholder' => 'Some Placeholder', 'value' => 'Some Input Value']]) ?>
Hope this helps.
for yii2 you can try this
<?= $form->field($model, 'user_type',['inputOptions' => ['value' => '2']])->hiddenInput()->label(false) ?>
It worked for me
Alternatively,
echo CHtml::activeHiddenField($model,"[$i]id", array("value" => $model->id));
This would set hidden field value as the id from model. The [$i] is useful for multiple record update.
Here are two ways to do that...
without model
echo CHtml::hiddenField('name' , 'value', array('id' => 'name'));
with model
echo $form->hiddenField($model, 'name');

Select or Dropdown list from CActiveRecord in Yii

I have table types and i want to build selectbox with all values from this table
In my controller i wrote this code
$allRegistrationTypes = RegistrationType::model()->findAll();
$this->render('index', array('allRegistrationTypes' => $allRegistrationTypes))
How build selectbox in view file ?
Well then its pretty simple all you need to do is first create List Data like
CHtml::ListData(allRegistrationTypes,'value you want to pass when item is selected','value you have to display');
for ex
typeList = CHtml::ListData(allRegistrationTypes,'id','type');
now remember both id and type are fields in table
now all you have to do is if you are using form then
<?php echo $form->dropDownList($model, 'type_id', $typeList, array('empty'=>'Select a tyoe')); ?>
and if you need multiple you can pass multiple => multiple in the array as htmlOptions
You would use CHtml::dropDownList, or activeDropDownList if there is a "parent" model and you want to leverage its validation rules.
If you want to make the <select> element multiple-selection-capable, pass in 'multiple' => 'multiple' and 'size' => X as part of the $htmlOptions parameter.
Simplest Method to get "Select Box" in YII Framework:
<div class="row">
<?php
echo $form->labelEx($model,'county');
$data = CHtml::listData(County::model()->findAll(), 'id', 'county');
$htmlOptions = array('size' => '1', 'prompt'=>'-- select county --', );
echo $form->listBox($model,'county', $data, $htmlOptions);
echo $form->error($model,'county');
?>
</div>
Good Luck..

Categories