Html tags in textarea

Good day!

I have form created with GII.




<div class="articles-form">

    <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>

    <?= $form->field($model, 'title')->textInput(['maxlength' => 512]) ?>

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

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

    <?= $form->field($model, 'date_created')->textInput() ?>

    <?= $form->field($model, 'date_modified')->textInput() ?>

    <?= $form->field($model, 'sort')->textInput() ?>

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

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

    <?= $form->field($model, 'file')->fileInput() ?>

    <div class="form-group">

        <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>

    </div>

    <?php ActiveForm::end(); ?>

    <script>

        var editor = CodeMirror.fromTextArea(document.getElementById("articles-full_text"), {

            lineNumbers: true,

            lineWrapping: true,

            matchBrackets: true,

            mode: "application/x-httpd-php",

            indentUnit: 2,

            indentWithTabs: true,

            tabMode: "indent"

        });

    </script>

</div>



As you can see, I have textarea with id "articles-full_text", that contains html tags. For example, I need to record this full-text:

<span style="font-weight: bold; color: #a23;">Hello</span>

but when I open page, that contains data from article, page source shows me this:

&lt;span style=&quot;font-weight: bold; color: #a23;&quot;&gt;Hello&lt;/span&gt;

I am not sure, but I think, database contains right variant. I have tried to find information about filters in Yii2, but useless.

Can someone explain, how can I disable html tags filter in this textarea? Thanks.

The form is behaving correctly. You just need to prevent the HTML encoding from happening on the page where you display the content.

Please post the code for this page.

views/articles/view.php




<div class="articles-view">


    <h1><?= Html::encode($this->title) ?></h1>


    <p>

        <?= Html::a('Update', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>

        <?= Html::a('Delete', ['delete', 'id' => $model->id], [

            'class' => 'btn btn-danger',

            'data' => [

                'confirm' => 'Are you sure you want to delete this item?',

                'method' => 'post',

            ],

        ]) ?>

    </p>


    <?= DetailView::widget([

        'model' => $model,

        'attributes' => [

            'id',

            'title',

            'preview_text:ntext',

            'full_text:ntext',

            'date_created',

            'date_modified',

            'sort',

            'keywords:ntext',

            'description:ntext',

        ],

    ]) ?>


</div>



models/Articles.php




class Articles extends \yii\db\ActiveRecord

{

    public $file;

    /**

     * @inheritdoc

     */

    public static function tableName()

    {

        return 'articles';

    }


    /**

     * @inheritdoc

     */

    public function rules()

    {

        return [

            [['title', 'full_text', 'date_created', 'date_modified'], 'required'],

            [['preview_text', 'full_text', 'keywords', 'description', 'preview_picture'], 'string'],

            [['date_created', 'date_modified'], 'safe'],

            [['sort'], 'integer'],

            [['title'], 'string', 'max' => 512],

            [['preview_picture'], 'string', 'max' => 1024],

            [['file'], 'file']

        ];

    }


    /**

     * @inheritdoc

     */

    public function attributeLabels()

    {

        return array(

            'id' => 'ID',

            'title' => 'Title',

            'preview_text' => 'Preview Text',

            'full_text' => 'Full Text',

            'date_created' => 'Date Created',

            'date_modified' => 'Date Modified',

            'sort' => 'Sort',

            'keywords' => 'Keywords',

            'description' => 'Description',

            'preview_picture' => 'Preview Picture',

        );

    }

}



In the DetailView, use ‘fieldName:html’ instead of ‘fieldName:ntext’.

It works. Thank you very much!

I tried to erase "ntext" and type just




'attributes' => [

            'id',

            'title',

            'preview_text:ntext',

            'full_text',

            'date_created',

            'date_modified',

            'sort',

            'keywords:ntext',

            'description:ntext',

        ],



but this did not work. I didn’t know, that I can write “full_text:html”. Can you point me where can I read about this in documentation? I cannot find. But i think, there are some another key words, that I need to know.

Thanks again. :)

http://www.yiiframework.com/doc-2.0/yii-i18n-formatter.html

http://www.yiiframework.com/doc-2.0/guide-output-formatter.html