Monday, September 23, 2019

php - Error in creating a simple form in Yii framework



Hello guys I am new to Yii framework. Before I am using CodeIgniter as my framework. And now I decided to switch to Yii. Because of its amazing features. But I am having a hard time in studying it. Now I am creating a simple form from scratch. I didn't use the gii tool.
My problem is if I included a textbox the output is an exception. Here it is.



CException

Property "ContactForm.username" is not defined.


C:\xampp\htdocs\yii\framework\web\helpers\CHtml.php(2529)


I don't know what does it mean. i guess I need to declare the input's name. But how?



Here's my code



Controller





class BlogController extends Controller {

public function actionIndex() {

$model = new ContactForm;

$this->render('index', array( 'model' => $model ));

}


}

?>


Model





class Blog extends CFormModel {

public $username;

public function rules() {

return array (

array ( 'username', 'required' ),


);

}

}

?>


View





$this->breadcrumbs = array (
'Blog',
);

?>

















Answer



Property "ContactForm.username" is not defined.


It means your ContactForm doesn't have a username property. So you have to define username property in your ContactForm.



class Blog extends CFormModel {


public $username;

public function rules() {

return array (

array ( 'username', 'required' ),

);


}

}


As I see, you defined username in your Blog model.



BTW, I guess you want to use Blog model instead of ContactForm in actionIndex():





class BlogController extends Controller {

public function actionIndex() {

$model = new Blog;

$this->render('index', array( 'model' => $model ));


}

}

?>

No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...