27

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?

brg
  • 7,332
  • 8
  • 44
  • 60
  • 1
    http://www.yiiframework.com/doc-2.0/guide-input-forms.html you should re-read manual more times if you have such question. – user1954544 May 18 '16 at 15:40

25 Answers25

89

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.

Alex
  • 7,679
  • 7
  • 37
  • 59
Qurashi
  • 1,401
  • 15
  • 26
12

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

Community
  • 1
  • 1
Midhun
  • 3,450
  • 1
  • 20
  • 25
3

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(); ?>
3

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.'); ?>
Mahmut Aydın
  • 715
  • 10
  • 21
  • Too much hardcoded HTML. Try don't use such coding in real projects. If you need custom HTML in Yii2 project (e.g. you use not Bootstrap styles) you should write your own widgets and use it. – Alexander Emelianov Nov 03 '17 at 14:06
2

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.

Kailas
  • 2,831
  • 5
  • 37
  • 43
1

If you map with model then following code should be OK for you:

<?= $form->field($model, 'downloadSourceCode')->textarea() ?>
akmnahid
  • 736
  • 2
  • 6
  • 18
1
<?= $form->field($model, 'field_name')->textArea(['maxlength' => 300, 'rows' => 6, 'cols' => 50,'placeholder'=>'Enter Message Here.....']) ?>
  • 1
    By using the ['maxlength' => 300'] we can set maxlength for textarea and also control the width and height of textarea using ['rows' => 6, 'cols' => 50] and using ['placehoder'=>'we can set our custom message'] – Rahul Vetal Mar 20 '17 at 18:12
1

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()) ?>
0

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(); ?>
Patricia Heimfarth
  • 2,688
  • 2
  • 22
  • 29
0

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.

Bhola Singh
  • 71
  • 1
  • 2
  • 4
0

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>
arogachev
  • 31,868
  • 6
  • 105
  • 113
Mohammed H
  • 6,326
  • 12
  • 72
  • 119
0

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]); ?>
sosono88
  • 26
  • 1
0

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'); ?>
Kalpesh Desai
  • 1,307
  • 14
  • 16
0

Its like this.

    <?php $form = ActiveForm::begin(); ?>
    <?= $form->field($model, 'downloadSourceCode')->textArea(['maxlength' =>     true]) ?>
    <?php ActiveForm::end(); ?>
Sameera Sampath
  • 606
  • 1
  • 11
  • 20
0
<?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**
0

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]) ?>
Anubhav Tiwari
  • 229
  • 3
  • 10
0
<?= $form->field($model, 'description')->textarea(['rows' => '2']) ?>
Bugs
  • 4,356
  • 9
  • 30
  • 39
0

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>
0
 <?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']) ?>
HP371
  • 683
  • 8
  • 20
0

With the help of Yii2 active form we can create textarea field in form.

$form->field($model, 'fieldName')->textarea(array('rows'=>2,'cols'=>5));
Yegor Androsov
  • 3,222
  • 1
  • 17
  • 28
-1

I`m not pretty sure, but activeTextarea() with option attr is nice

-1

there is alternative extension named kartik-v widget:

use kartik\widgets\ActiveForm;
echo ActiveForm::begin();
echo $form->field($model, 'username');

just install with composer

seseorank
  • 19
  • 4
-1
 <?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(); ?>
Jaya Kumari
  • 330
  • 1
  • 3
  • 11
-1

With Model:

<?= $form->field($model, 'user')->textArea(['rows' => 6]) ?>

Without Model:

<?= Html::textarea('sourceCode',null,['rows'=>6]) ?>
Insane Skull
  • 8,810
  • 9
  • 38
  • 59
Pratik Karmakar
  • 227
  • 1
  • 8
-2

It has 2 possibilities. FOr now do this: field($model, 'user')->textArea(['rows' => 6]) ?>