Yii2 - How To Add a CSS class to an ActiveForm Field - yii2

This is the activeform field. I would like to know how to add a css class to it.
<?= $form->field($model, 'url')->label(false); ?>

You can add it with
<?= $form->field($model, 'url')->textInput(['maxlength' => 255, 'class' => 'your class'])->label(false); ?>
As a rule you can pass html elements when telling activeField what type it should be. The default is textInput so that is why your code works, but if you want to change the input then you have to explicitly tell it the type of input.
http://www.yiiframework.com/doc-2.0/guide-input-forms.html

<?= $form->field($addWithdrawRequest, 'amount', ['options' => ['tag' => false]])->textInput(['class' => 'form-control col-lg-6 amount','required'=>true,'style' => 'background-color: #fff !important;','placeholder'=>"Enter Amount To Withdraw"])->label(false)?>

Related

Create dependent dropdown in Yii2

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');
}
});

Yii2 disabled textInput ActiveForm

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]) ?>

Yii2 form submit button must be clicked two times for action. How to prevent this?

yii2 submit button needs to be clicked two times in form
I have a problem where I need to check more than one submit buttons in the controller. It works but I need to click submit buttons two times.
In controller :
switch(\Yii::$app->request->post('submit')) {
case 'submit_1' :
//my code
break;
case 'submit_2' :
//my code
In view
<?= Html::submitButton('NEXT', ['name' => 'submit', 'value' => 'submit_2','class'=>'btn btn-primary pull-right']) ?>
<?= Html::submitButton('PREVIOUS', ['name' => 'submit', 'value' => 'submit_3','id'=>'next_summary', 'class' => 'btn btn-primary pull-right']) ?>
There is an issue with using jquery reserved words as your attribute id or attribute names.
Search for "Be careful when naming form elements such as submit buttons" at
https://github.com/yiisoft/yii2/blob/master/docs/guide/input-forms.md
Search "Additional Notes" at https://api.jquery.com/submit/
Changing your submit names will fix your click twice problem:
<?= Html::submitButton('NEXT', ['name' => 'submit_next', 'value' => 'submit_2','class'=>'btn btn-primary pull-right']) ?>
<?= Html::submitButton('PREVIOUS', ['name' => 'submit_prev', 'value' => 'submit_3','id'=>'next_summary', 'class' => 'btn btn-primary pull-right']) ?>
try to change name of button as array
<?= Html::submitButton('NEXT', ['name' => 'submit[]', 'value' => 'submit_2','class'=>'btn btn-primary pull-right']) ?>
<?= Html::submitButton('PREVIOUS', ['name' => 'submit[]', 'value' => 'submit_3','id'=>'next_summary', 'class' => 'btn btn-primary pull-right']) ?>
and in your controller :
$submittedType = \Yii::$app->request->post('submit');
switch($submittedType[0]) {
//your code
}
To avoid double click disable validate on submit:
$form = ActiveForm::begin([
'validateOnSubmit' => false,
]);

How to add custom class to label in Yii2 form field?

I want to add a custom class to label tag in this code in Yii2, but I don't know how.
<?= $form->field($model, 'name',[
'template' => "{label}\n<div class='col-md-6'>{input}</div>\n{hint}\n{error}"])->textInput(['maxlength' => true])?>
Try:
<?= $form->field($model, 'name', [
'template' => "{label}\n<div class='col-md-6'>{input}</div>\n{hint}\n{error}",
'labelOptions' => [ 'class' => 'your_custom_class_name' ]
])->textInput(['maxlength' => true])?>
For more details refer to this link.
UPDATE:
For more options use \yii\bootstrap\ActiveField (link) instead of \yii\widgets\ActiveField
there is a simple way and it worked for me
<?= $form->field($model, 'title')->textInput(['class'=>'form-control'])->label('Your Label',['class'=>'label-class']) ?>

ActiveForm without model yii2

I want to create ActiveForm without model for just in case something. I did try with dynamicModel but i got some error :
use yii\base\DynamicModel;
$model = DynamicModel::validateData(compact('KOMENTAR'), [
[['KOMENTAR'], 'string', 'max' => 128],
]);
This is the form i want to create
<br>
<?php $form = ActiveForm::begin([
'method' => 'post',
]); ?>
<?= $form->field($model, 'KOMENTAR')->textarea(['rows' => 6])->label(false) ?>
<div class="form-group">
<?= Html::submitButton('POST', ['class' => 'btn btn-primary']) ?>
</div>
This is the error
Getting unknown property: yii\base\DynamicModel::KOMENTAR
Normally ActiveItems are used to work with a model, but Yii2 have a helper class called Html to use the same items like classic HTML.
Use beginForm() method from Html. And try something like that:
use yii\helpers\Html;
<?= Html::beginForm(['/controller/view', 'id' => $model->id], 'POST'); ?>
<?= Html::textarea('KOMENTAR', '', ['rows' => 6])->label(false); ?>
<div class="form-group">
<?= Html::submitButton('POST', ['class' => 'btn btn-primary']); ?>
</div>
<?= Html::endForm(); ?>
You can read more about this helper in the documentation.
Since you are using compact('KOMENTAR'), you should have a $KOMENTAR variable.
Read more about compact : http://php.net/manual/fr/function.compact.php
Or you should simply create your model like this :
$model = new \yii\base\DynamicModel(['KOMENTAR']);
$model->addRule(['KOMENTAR'], 'string', ['max' => 128]);
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
// do what you want
}