Hi, please assist. I keep getting this error Undefined variable: model. What am I doing wrong?
here’s my view code:
<?php $form = ActiveForm::begin();
$form->field($model, 'name')
?>
<?= $form->field($model, 'names') ?>
<?= $form->field($model, 'company') ?>
<?= $form->field($model, 'email') ?>
<?= $form->field($model, 'phone') ?>
<?= $form->field($model, 'message') ?>
<div class = "form-group">
<?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
This is my controller:
public function actionContact() {
$model = new ModelContactForm();
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
return $this->sendHome();
} else {
return $this->render('contactForm', array('fContact' => $model));
}
}
And this is my index.php
echo $this->render(’/site/contactForm’, array(‘fContact’ => $model));
This line is incorrect:
return $this->render('contactForm', array('fContact' => $model));
All the variables named $model in the view file, i.e. these lines of code:
<?= $form->field($model, 'names') ?>
<?= $form->field($model, 'company') ?>
Are looking for the variable $model, and because above in your controller you have instead called it “fContact” you are getting an error message saying the model cannot be found. It should be like this:
return $this->render('contactForm', array('model' => $model));
I changed it and I still get the same error " Undefined variable: model"
That’s my view file, maybe that’s where the issue is?
Are you sure you are looking at the correct view file? Whatever controller action you are calling must pass the model through to the view file.
Your action should look something like this:
/**
* Displays contact page.
*
* @return Response|string
*/
public function actionContact()
{
$model = new ContactForm();
if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
Yii::$app->session->setFlash('contactFormSubmitted');
return $this->refresh();
}
return $this->render('contact', [
'model' => $model,
]);
}
So at a basic level of functionality (without $model->load) it should look like this for you:
public function actionContact()
{
$model = new ModelContactForm();
return $this->render('contact', [
'model' => $model,
]);
}
That’s how my controller looks like
That looks fine, and your view file is called “contactForm”?
Yes, its called contactForm
What’s at the top of the new model file you created called “ModelContactForm”? It should look something like this:
<?php
namespace app\models;
use Yii;
use yii\base\Model;
/**
* ModelContactForm is the model behind the contact form.
*/
class ModelContactForm extends Model
And also in that controller file where you call in the models, should it not be:
<?php
namespace app\controllers;
use Yii;
use yii\web\Controller;
use app\models\ModelContactForm;
use app\models\ModelService;
In controller file, I’ve changed it also:
use Yii;
use yii\web\Controller;
use app\models\ModelContactForm;
use app\models\ModelService;
So I changed the index.php to this - echo $this->render(’/site/contactForm’, array(‘model’ => $model)); instead of having this echo $this->render(’/site/contactForm’, array(‘fContact’ => $model));
I now have a new error
looks like is pulling the service model not the contact form model. How do I fix this?
Just for closure on this support topic, in your model you referenced the class property as “names” instead of “name” which is why you got the error above:
public $names;
It should have been:
public $name;
Thank you so much for all your help.
I finally solved the issue.