Create a form without a model

I was wondering how can I create a form without a model in Yii2 framework as I am creating a mailchimp signup form so a model isn’t necessary the below code generates a form however as you can see it uses a model.


<?php $form = ActiveForm::begin(['id' => 'login-form']); ?>


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


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

Do I still use activeform, how can I remove the $model variable without it throwing up an error?

No without a model you do not use ActiveForm. You use the Html helpers I believe.

Alternatively you could create a none DB model to use, but you said you did not want to use model.

The best way to do it would be a none DB model. A none DB model is a model which extends …

\yii\base\Model

And does not connect to DB. You define public properties in the model and they become your form fields etc.

id do the same thing that JamesBarnsley said and create a model that extends yii\base\model. The main reason is that you can use all of yii’s built in validators and functions. I’m bored so ill just show how to do it for your example so if someone else needs help it could help them.

model




<?php


namespace frontend\models;


use Yii;


class MailChimpSignUp extends \yii\base\Model {


	public $name;

	public $title;

	public $email;


	public function rules() {

    	return [

        	[['title', 'name', 'email'], 'required'],

        	['email', 'email'],

    	];

	}


	public function attributeLabels() {

    	return [

        	'email' => 'Email',

        	'title' => 'Title',

        	'name' => 'Name',

    	];

	}


};

?>




controller


  

use frontend\models\MailChimpSignUp;


 public function actionMailChimp() {

    	$model = new MailChimpSignUp();   	

    	if ($model->load(Yii::$app->request->post())) {

   		//do something with the $model->email ect.. varibles

        	

        	return $this->redirect(['anotherpage']);

    	} else {


        	return $this->render('mailchimpsignup', [

                    	'model' => $model,

        	]);

    	}

	}



form




<?php $form = ActiveForm::begin(['id' => 'login-form']); ?>

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

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

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

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