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]);
?>
Related
I want to display the lecturer username in another text input field form when the user selects the lecturer name in the drop-down list above.
this is my code for drop-down list lecturer name
<?= $form->field($model, 'LecturerName')->dropDownList(
ArrayHelper::map(User::findAll(['category' => 'lecturer']),'fullname','fullname'),
['prompt'=>'Select Lecturer']
) ?>
this is my code for lecturer username
<?= $form->field($model, 'LecUsername')->textInput(['maxlength' => true]); ?>
I want to get and display the LecUsername based on the selected drop-down list above. the LecUsername is from the database.
As you are using the dropdown you need to bind the change event for the dropdown to insert the value from the dropdown to the input text. And to make sure that you are inserting the LecUsername for the LecturerName you need to have the LecUsername as the value for the dropdown, means the drop-down data should have the username field as the index, currently you are using the fullname as the index and the value so change the code according to the field name you have for the username, i assume it is username for the example.
ArrayHelper::map(User::findAll(['category' => 'lecturer']),'username','fullname')
so your dropdown code will look like
<?= $form->field($model, 'LecturerName')->dropDownList(
ArrayHelper::map(User::findAll(['category' => 'lecturer']),'username','fullname'),
['prompt'=>'Select Lecturer']
) ?>
then add the below on top of your view file where you have the form
$ddId=Html::getInputId($model, 'LecturerName');
$inputId=Html::getInputId($model, 'LecUsername');
$js = <<< JS
$(document).ready(function(){
$("#{$ddId}").on('change',function(e){
$("#{$inputId}").val($(this).val());
});
});
JS;
$this->registerJs($js, \yii\web\View::POS_READY);
A better way for dropdowns is to use the library Kartik-v\select2 and use the event pluginEvents option to specify the event select2:select, your code should look like below in that case.
// Usage with ActiveForm and model
echo $form->field($model, 'LecturerName')->widget(Select2::classname(), [
'data' => ArrayHelper::map(User::findAll(['category' => 'lecturer']),'username','fullname'),
'options' => ['placeholder' => 'Select Lecturer'],
'pluginOptions' => [
'allowClear' => true
],
'pluginEvents'=>[
"select2:select" => 'function() { $("#{$ddId}").on('change',function(e){
$("#{$inputId}").val($(this).val());
}); }',
]
]);
Have One Page 2 Gridview Table . Pagination Does not work properly ,
Here is the Code :
// table 1
<?php Pjax::begin(['id'=>'table_1']); ?>
<?= GridView::widget([
//
]); ?>
<?php Pjax::end(); ?>
// Table 2
<?php Pjax::begin(['id'=>'table_2']); ?>
<?= GridView::widget([
//
]); ?>
<?php Pjax::end(); ?>
It is already explained in the guide and here I quote.
You can use more than one GridView on a single page but some additional configuration is needed so that they do not interfere with each other. When using multiple instances of GridView you have to configure different parameter names for the generated sort and pagination links so that each GridView has its own individual sorting and pagination. You do so by setting the sortParam and pageParam of the dataProvider's sort and pagination instances.
In your case:
use yii\grid\GridView;
$tbl1Provider->pagination->pageParam = 'tbl1_page';
$tbl2Provider->pagination->pageParam = 'tbl2_page';
echo GridView::widget([
'dataProvider' => $tbl1Provider,
]);
echo GridView::widget([
'dataProvider' => $tbl2Provider,
]);
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") ?>
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
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');