Yii2: How to add textarea in yii2
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 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.