Yii2 create a form without a model

I was wondering how I can create a form without a model in the Yii2 structure, since I create a mailchimp registration form, so the model is not needed if the code generates the form below, but as you can see, it uses the model.

<?php $form = ActiveForm::begin(['id' => 'login-form']); ?> <?= $form->field($model, 'title')->textInput(['maxlength' => 255]) ?> <?php ActiveForm::end(); ?> 

I am still using activeform, how can I remove the $ model variable without causing it to fail?

+7
javascript php frameworks yii2 mailchimp
source share
3 answers

Yii2 has this cute little thing called DynamicModel . This basically allows you to create the model on the fly so that you can still use all ActiveForm and validation, but without having to write the entire model class for it. May be interesting.

Example from the documentation:

 public function actionSearch($name, $email) { $model = DynamicModel::validateData(compact('name', 'email'), [ [['name', 'email'], 'string', 'max' => 128], ['email', 'email'], ]); if ($model->hasErrors()) { // validation fails } else { // validation succeeds } } 

Obviously, this instance can also be used for ActiveForm widget. You can then perform the correct validation in your actions, and then transfer your MailChimp data. It may be convenient if you want to run HTML Purifier as part of this check for content

+9
source share

use Html Input with active form <?=Html::input('text','','',['class'=>'form-control'])?>

+5
source share

As suggested by @DamienPirsy - use plain. If you want to use the yii2 functions for it, use Class yii \ helpers \ BaseHtml ( http://www.yiiframework.com/doc-2.0/yii-helpers-basehtml.html ). There are all methods for constructing any shape as you want. Then you can control it with any action on any controller of your application. But this is not so. This is why Yii / Yii2 advises you to use models.

0
source share

All Articles