ActiveForm without model yii2

Noobs To be Pro Addict picture Noobs To be Pro Addict · Sep 3, 2015 · Viewed 29k times · Source

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

Answer

Sageth picture Sageth · Sep 3, 2015

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.