how to disabled textInput in ActiveForm ?
i try like this but can't
<?= $form->field($model, 'title_series')->textInput(['class' => 'form-control class-content-title_series', 'placeholder' => 'Title', 'disabled' => 'disabled'])->label(false) ?>
'disabled' => 'disabled or 'disabled' => true
both of them can't too
I don't really know yii2/ActiveForm, but I believe you need to do it this way:
<?= $form->field($model, 'title_series')->textInput(['class' => 'form-control class-content-title_series', 'placeholder' => 'Title', 'disabled' => true])->label(false) ?>
To ensure that the field values are send on submit, use
echo $form->field($model, 'name')->textInput([
'readonly' => true,
]);
This solution won in 50 different tries.
I think issue was with your label thing
<?= $form->field($model, 'title_series')->textInput(['class' => 'form-control class-content-title_series', 'placeholder' => 'Title', 'disabled' => 'true'])->label(''); ?>
This is working, keep the disabled in same array than 'value'
<?= $form->field($model, 'type')->textInput(['value' => $type,'disabled' => true]) ?>
Related
These are two active-form fields in Yii2.
<?= $form->field($model, 'navigation_type')->dropdownList(['Module'=>'Module','Screen'=>'Screen']) ?>
<?= $form->field($model, 'showInUrl')->dropdownList([0=>'No',1=>'Yes']) ?>
When I click Screen, the second field should be changed to Yes. When I click Module, it should be changed to No. I have to save only 0 or 1 to the db.
How can I do this?
Well, you need to bind the change event to the first drop-down using javascript/jquery like below. add the script on top of your view and provide the id to both the dropdowns.
$this->registerScript("
$('#navigation_type').on('change',function(){
if($(this).val() == 'Module'){
$('#showInUrl').val(0);
}else{
$('#showInUrl').val(1);
}
});",\yii\web\View::POS_END);
<?= $form->field($model, 'navigation_type')->dropdownList(['Module'=>'Module','Screen'=>'Screen'],['id'=>'navigation_type']) ?>
<?= $form->field($model, 'showInUrl')->dropdownList([0=>'No',1=>'Yes'],['id'=>'showInUrl' ])?>
Apart from above solution, you should look into DepDropDown by kartik which reduces your efforts to
a maximum and you just need to integrate and it works great.
Using Kartik/select Dropdown You Can Code like These :
<div class="navigation-form">
<?= $form->field($model, 'navigation_type')
->widget(kartik\select2\Select2::className(), [
'data' => ['Module'=>'Module','Screen'=>'Screen'],
'options' => ['multiple' => false],
'pluginOptions' => [
'placeholder' => 'Select Module',
],
])
?>
<?= $form->field($model, 'showInUrl')->widget(kartik\select2\Select2::className(),[
'data' => [0=>'No',1=>'Yes'],
'options' => ['multiple' => false],
'pluginOptions' => [
'placeholder' => 'ShoW URL',
],
]) ?>
</div>
<script type="text/javascript">
$(document).ready(function(){
$('#navigation-navigation_type').on("change",function(e){
var sel_val = $(this).select2("val");
if(sel_val=='Screen'){
$('[name="navigation[showInUrl]"]').val('Yes').trigger('change');
}
if(sel_val=='Module'){
$('[name="navigation[showInUrl]"]').val('No').trigger('change');
}
});
I have a form field "dob[]" with array input like
<?php $form = ActiveForm::begin();
for($i=0;$i<= 3;$i++):
echo $form->field($model, 'dob[]')->widget(DatePicker::classname(), [
'options' => ['placeholder' => 'Date Of Birth'],
'type' => DatePicker::TYPE_INPUT,
'pluginOptions' => [
'format' => 'mm/dd/yyyy',
'autoclose' => true,
]
]);
endfor; ?>
<div class="form-group">
<?= Html::button('Submit', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
Datepicker working only on first "dob" field but rest of the field having only button format of datepicker but calendar not working.
This is because the javascript cannot determine the correct input fields, after the first one. Take a look in your source code. All widgets have properly the same id and/or name. You have to setup a unique ID for each of the generated widgets.
By the way, it is always a good approach to name form data accordingly.
That is documented at the demo page.
The following should work:
<?php
$form = ActiveForm::begin();
for ($i=0; $i < 3; $i++) {
echo $form->field($model, 'date_end')->widget(DatePicker::classname(), [
'options' => [
'placeholder' => 'Date Of Birth',
'name' => 'DOB' .$i,
'id' => 'DOB-ID' . $i,
],
'type' => DatePicker::TYPE_INPUT,
'pluginOptions' => [
'format' => 'mm/dd/yyyy',
'autoclose' => true,
],
]);
}
?>
<div class="form-group">
<?= Html::button('Submit', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
I have a simple form:
<?php $form = ActiveForm::begin([
'id' => 'answer-form',
'action' => Yii::$app->getUrlManager()->createUrl('test'),
'enableClientValidation' => false,
]); ?>
<?= $form->field($user_answer, 'user_text')->textInput(['value' => $text])->label('Text') ?>
<?php ActiveForm::end(); ?>
I want to show input with red color (div with class "has-error" by default)- like somebody added wrong data to the input. How can i do it?
Try this
<?= $form->field($user_answer, 'user_text', [ 'options' => [ 'class' => 'has-error'])->textInput(['value' => $text])->label('Text') ?>
getting an error cant upload the extension kartik select2....
need some ideaaa
using yii want to install the select 2 extension in the branch table...
<div class="branches-form">
<?php $form = ActiveForm::begin(); ?>
<?=$form->field($model,'companies_company_id')->widget(Select2::classname(), [
'data' =>ArrayHelper::map(Companies::find()->all(),'company_id','company_name'),['prompt'=>'Select Company'] ,
'language' => 'en',
'options' => ['placeholder' => 'Select a state ...'],
'pluginOptions'=> [
'allowClear' => true
],
]); ?>
<?= $form->field($model, 'branch_name')->textInput(['maxlength' => 100]) ?>
<?= $form->field($model, 'branch_address')->textInput(['maxlength' => 255]) ?>
<?= $form->field($model, 'branch_status')->dropDownList([ 'active' => 'Active', 'inactive' => 'Inactive', '' => '', ], ['prompt' => 'Status']) ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
Properly read this documentation http://demos.krajee.com/widget-details/select2
Try this solution:
Also include use kartik\select2\Select2; at the top of your file.
<?= Select2::widget([
'attribute' => 'companies_company_id',
'model' => $model,
'data' =>ArrayHelper::map(Companies::find()->all(),'company_id','company_name'),['prompt'=>'Select Company'] ,
'language' => 'en',
'options' => ['placeholder' => 'Select a state ...'],
'pluginOptions'=> [
'allowClear' => true
],
]) ?>
My application have to have multiple language, so I decided to separate each language by using tab (Yii2 gui), but how can I render the form in side the 'content' key?
<?php
$language_tab=[];
$increment=0;
$content="I love you";
foreach($language as $obj){
$language_tab[$increment] = array('label' => $obj->name ,'content' => $content);
$increment++;
}
echo Tabs::widget([
'items' => $language_tab,
'options' => ['tag' => 'div'],
'itemOptions' => ['tag' => 'div'],
'headerOptions' => ['class' => 'my-class'],
'clientOptions' => ['collapsible' => false],
]);
?>
<div class="status-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'date_created')->textInput() ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
I just wanna change from $content to the form below.
Please help!!!
You may create separate view for the form and render it:
...
'content' => $this->render('_language_form', ['language' => $obj, 'model' => $model]),
...