10

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
1
  • you define public $KOMENTAR in your model page
    – vishuB
    Sep 3, 2015 at 6:35

2 Answers 2

21

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.

4
  • 4
    Do not use BaseHtml. Use yii\helpers\Html instead.
    – Tony
    Sep 3, 2015 at 7:18
  • 1
    You are wrong about "Html have more tools for view design", you should read this yiiframework.com/doc-2.0/…
    – soju
    Sep 3, 2015 at 7:45
  • 1
    You should check your Code, there are some synthax errors in it like at beginForm()([ 'method' => 'post', ]) and (['rows' => 6, name='KOMENTAR'])
    – bnu
    Mar 2, 2016 at 15:32
  • It is nice, but does not help me with CKEDITOR 4 + widget for yii2 ( github.com/2amigos/yii2-ckeditor-widget ), when model is absent, so only js helps: $inputText = Html::textArea('PageData[data]', $data0, ['id'=>'pagedata-data']); echo $inputText; $this->registerJs('CKEDITOR.replace($("textarea#pagedata-data")[0], {})'); Feb 23, 2022 at 2:07
14

Since you are using compact('KOMENTAR'), you should have a $KOMENTAR variable.

Read more about compact : http://php.net/manual/fr/function.compact.php

Or you should simply create your model like this :

$model = new \yii\base\DynamicModel(['KOMENTAR']);
$model->addRule(['KOMENTAR'], 'string', ['max' => 128]);
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
    // do what you want 
}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.