Monday 23 September 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

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...