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.
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,
]);
}
}