I a new yii2 developer. I want to use widgets instead of activeForms. However, i do not know how to do it. Please, give me hint how to use widgets.
In my file, I have following code:
<div class="">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'title_ru')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'description_ru')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'content_ru')->textarea(['rows' => 6]) ?>
<?= $form->field($model, 'type')->textInput() ?>
<?= $form->field($model, 'region')->textInput() ?>
<?= $form->field($model, 'category')->textInput() ?>
<?= $form->field($model, 'status')->textInput() ?>
<?= $form->field($model, 'image')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'title_en')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'title_uz')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'title_uzk')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'description_en')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'description_uz')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'description_uzk')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'content_en')->textarea(['rows' => 6]) ?>
<?= $form->field($model, 'content_uz')->textarea(['rows' => 6]) ?>
<?= $form->field($model, 'content_uzk')->textarea(['rows' => 6]) ?>
<?php ActiveForm::end(); ?>
</div>
//learning widgets
Actually ActiveForm is a widget you can see for yourself by use keyword in top of your php view file
it will be something like
<?php
use yii\widgets\ActiveForm;
but if you want to use other widgets make it like this (use ->widget()) instead of textInput or textarea
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'company_id')->widget(Select2::classname(), [
'data' => ArrayHelper::map(Companies::find()->all(), 'company_id', 'company_name'),
'language' => 'en',
'options' => ['placeholder' => 'Select a category ...'],
'pluginOptions' => [
'allowClear' => true
],
]); ?>
make sure you put your own data.
You can find a lot of widgets here http://www.yiiframework.com/extension/yii2-widgets/
<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
$form = ActiveForm::begin(['id' => 'login-form']);
ActiveForm::end();
?>
If i run this code the following error will occur.
Fatal error: Class 'yii\bootstrap\ActiveForm' not found in
E:\wamp\www\yii2-paypal-master\backend\views\site\login.php on line 6
Kindly help me to fix this.
Thanks
Replace
use yii\bootstrap\ActiveForm;
with
use yii\widgets\ActiveForm;
Form:
<?php $form = ActiveForm::begin([
'options'=>['enctype'=>'multipart/form-data'],
]);
?>
<?= $form->errorSummary($model) ?>
<?= $form->field($model, 'title')->textInput(['maxlength' => true]) ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
First, check the file assets/AppAsset.php if your Class App Asset is like mine, using Boostrap 3 or 4 or o:
public $depends = [
'yii\web\YiiAsset',
'yii\bootstrap4\BootstrapAsset',
];
}
You need to change it to use yii\bootstrap\ActiveForm; change this to instead use yii\bootstrap4\ActiveForm; it worked for me.
What is the mapping of textarea in yii2 ?
How to write this in yii2 format?
<textarea name="downloadSourceCode" id="downloadSourceCode"></textarea>
What is an alternative or way to define textarea in yii2?
You can use Active Forms to create fields like textarea for example
<?php $form = ActiveForm::begin(['id' => 'downloadSourceCode']); ?>
<?= $form->field($model, 'description')->textarea(['rows' => '6']) ?>
<?= Html::submitButton('Submit') ?>
<?php ActiveForm::end(); ?>
In the previouse example you are creating a form with a textarea inside, you can give it a name and pass the model from the controller to show the existing content of the model if you are editing it, if you are creating a new model, you will need to create a new object and then pass it to the view.
Text area code in yii2 could be created in many ways It depends on what you need exactly
Situation 1 You have a Model
say the text area connected to that model in an Active form
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'election_description')->textArea() ?>
<?php ActiveForm::end(); ?>
the Code generated will be
<div class="form-group field-election-election_description">
<label class="control-label" for="election-election_description">Description</label>
<textarea id="election-election_description" class="form-control" name="Election[election_description]"></textarea>
<div class="help-block"></div>
</div>
As you can see label and error block is generated along with the textarea code by default since this could be useful in practical scenarios .So What i have written above will be interpreted as
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'election_description',['template'=> "{label}\n{input}\n{hint}\n{error}"])->textArea() ?>
<?php ActiveForm::end(); ?>
Change or remove the label by just doing
<?= $form->field($model, 'election_description')->textArea()->label(false) ?>
<?= $form->field($model, 'election_description')->textArea()->label("Some Label") ?>
Or more advanced customization could be done by modifying the template ,
"{label}\n{input}\n{hint}\n{error}"
is the default template .However template is customizable, If you just want the text area only override the code generation template for text area as
"{input}"
thus
<?= $form->field($model, 'election_description',['template'=> "{input}"])->textArea() ?>
the Code generated will be
<div class="form-group field-election-election_description">
<textarea id="election-election_description" class="form-control" name="Election[election_description]"></textarea>
</div>
The Div wrapping the text filed could be removed by modifying the template of the active form or by using another function activeTextInput
<?= Html::activeTextInput($model, 'election_description'); ?>
the Code generated will be
<textarea id="election-election_description" name="Election[election_description]"></textarea>
Situation 2 You don't have a Model
If we don't have a model and just want to create the exact code as asked best way will be to use Html::textarea
follow this format
textarea ( $name, $value = '', $options = [] )
Refer this example
<?php use yii\helpers\Html;?>
<?= Html::textArea('downloadSourceCode',"",['id'=>'downloadSourceCode']); ?>
Which will generate a code
<textarea id="downloadSourceCode" name="downloadSourceCode"></textarea>
Hope This helps
Refer these links for more info
http://www.yiiframework.com/doc-2.0/yii-widgets-activefield.html#textarea()-detail
http://www.yiiframework.com/doc-2.0/yii-helpers-basehtml.html#textarea()-detail
http://www.yiiframework.com/doc-2.0/yii-helpers-basehtml.html#activeTextarea()-detail
You can do this like:
<?php $form = ActiveForm::begin(['id' => 'my-form']); ?>
<?= $form->field($model, 'field_name')->textArea(['rows' => '6']) ?>
<?= Html::submitButton('Submit') ?>
<?php ActiveForm::end(); ?>
Use Textarea in template
<?= $form->field($model, 'columnName',
['template' => '
{label}
<div class="input-group">
<span class="input-group-addon">
<i class="fa fa-newspaper-o"></i>
</span>
{input}
</div>
{error}{hint}'])->textarea(['rows' => 6])->hint('Max 255 characters.'); ?>
You can use the below code
<?= $form->field($model, 'desc')->textarea(); ?>
OR
<?= $form->field($model, 'desc')->textarea()->label('Description'); ?>
OR
<?= $form->field($model, 'desc')->textarea(array('rows'=>2,'cols'=>5)); ?>
For more details about form field.
If you map with model then following code should be OK for you:
<?= $form->field($model, 'downloadSourceCode')->textarea() ?>
<?= $form->field($model, 'field_name')->textArea(['maxlength' => 300, 'rows' => 6, 'cols' => 50,'placeholder'=>'Enter Message Here.....']) ?>
You can run following command on console
php composer.phar require --prefer-dist yiidoc/yii2-redactor "*"
or
"yiidoc/yii2-redactor": "*"
for instaling Redactor see https://github.com/yiidoc/yii2-redactor
Than you can check following line in codes
<?= $form->field($model, 'address')->widget(\yii\redactor\widgets\Redactor::className()) ?>
Like this:
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'die_geschichte')->textArea(array('rows'=>25, 'cols'=>50, 'readonly' => true, 'name'=>'xyz; )) ?>
<div class="form-group">
<?= Html::submitButton('Unangemessenen Inhalt melden', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
It is so simple. Just write it like this in your ActiveForm::begin.
<?= $form->field($model, 'downloadSourceCode')->textarea(['maxlength' => 1000]) ?>
and you can get your text area.
If you want textarea without specifying any model, use:
<?= \yii\helpers\Html::textarea('name_attribute_value') ?>
Output will be:
<textarea id="id_attribute_value" name="name_attribute_value"></textarea>
It's simple. Just like this
<?= $form->field($model, 'notes')->textarea(); ?>
<?= $form->field($model, 'notes')->textarea()->label('Notes'); ?>
<?= $form->field($model, 'notes')->textarea(['rows'=>2,'cols'=>5]); ?>
This can be help you
Text area
<?= $form->field($model, 'desc')->textarea(['rows'=>2,'cols'=>5,'id'=>'textarea_id','class'=>'form-control textarea_class']); ?>
<?= $form->field($model, 'desc')->textarea()->label('Description'); ?>
Text
<?= $form->field($model,'name'); ?>
<?= $form->field($model, 'name')->textInput()->hint('Please enter your name')->label('Name') ?>
Password
<?= $form->field($model, 'password')->input('password') ?>
<?= $form->field($model, 'password')->passwordInput() ?>
<?= $form->field($model, 'password')->passwordInput()->hint('Password should be within A-Za-z0-9')->label('Password Hint') ?>
File
<?= $form->field($model, 'uploadFile')->fileInput() ?>
<?php echo $form->field($model, 'uploadFile[]')->fileInput(['multiple'=>'multiple']); ?>
Radio
<?= $form->field($model, 'gender') ->radio(array('label'=>''))
->label('Gender'); ?>
<?= $form->field($model, 'gender')->radio(array(
'label'=>'',
'labelOptions'=>array('style'=>'padding:5px;')))
->label('Gender'); ?>
<?= $form->field($model, 'population')->radioList(array('1'=>'One',2=>'Two')); ?>
List
<?= $form->field($model, 'population')-> listBox(
array('1'=>'1',2=>'2',3=>3,4=>4,5=>5),
array('prompt'=>'Select','size'=>3)
); ?>
<?= $form->field($model, 'population')-> listBox(
array('1'=>'1',2=>'2',3=>3,4=>4,5=>5),
array('disabled' => true,'style'=>'background:gray;color:#fff;'))
->label('Gender'); ?>
Its like this.
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'downloadSourceCode')->textArea(['maxlength' => true]) ?>
<?php ActiveForm::end(); ?>
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'descrip', ['options' => ['class' => 'custom-class']])->textArea(['maxlength' => true, 'placeholder'=>'Invoice Address', "class"=>'form-control']) ?>
<?php $form = ActiveForm::end(); ?>
**You can Also add Tinymce widget instead of text area.**
<?= $form->field($model, 'club_description')->widget(TinyMce::className(), [
'options' => ['rows' => 4],
//'language' => 'EN',
'clientOptions' => [
'plugins' => [
"advlist autolink lists link charmap print preview anchor",
"searchreplace visualblocks code fullscreen",
"insertdatetime media table contextmenu paste"
],
'toolbar' => "undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image"
]
]);?>
**Namespace: use dosamigos\tinymce\TinyMce;
Reference: https://github.com/2amigos/yii2-tinymce-widget**
Step 1 : In your view file don't forgot to add ActiveForm Class
use yii\bootstrap\ActiveForm;
Step 2 : Now add the text area as below in the view
field($model, 'body')->textarea(['rows' => 6]) ?>
<?= $form->field($model, 'description')->textarea(['rows' => '2']) ?>
This code is added in views file:
<div class="col-md-12 mb-10">
<label class="control-label" for="cancellesson-cancel_note">Cancel Note</label>
<textarea id="cancellesson_cancel_note" class="form-control" name="cancellesson[cancel_note]" placeholder="Enter Cancel Note" aria-required="true"></textarea>
</div>
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'description')->textarea(['rows' => '5']) ?>
<?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
<?php ActiveForm::end(); ?>
This line is for textarea
<?= $form->field($model, 'description')->textarea(['rows' => '5']) ?>
With the help of Yii2 active form we can create textarea field in form.
$form->field($model, 'fieldName')->textarea(array('rows'=>2,'cols'=>5));
I`m not pretty sure, but activeTextarea() with option attr is nice
there is alternative extension named kartik-v widget:
use kartik\widgets\ActiveForm;
echo ActiveForm::begin();
echo $form->field($model, 'username');
just install with composer
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'description')->textarea(['rows' => 6]) ?>
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
<?php $form = ActiveForm::end(); ?>
With Model:
<?= $form->field($model, 'user')->textArea(['rows' => 6]) ?>
Without Model:
<?= Html::textarea('sourceCode',null,['rows'=>6]) ?>
It has 2 possibilities. FOr now do this:
field($model, 'user')->textArea(['rows' => 6]) ?>