Jerielle
Jerielle

Reputation: 7520

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

<?php

    class BlogController extends Controller {

        public function actionIndex() {

            $model = new ContactForm;

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

        }

    }

?>

Model

<?php

    class Blog extends CFormModel {

        public $username;

        public function rules() {

            return array (

                array ( 'username', 'required' ),

            );

        }

    }

?>

View

<?php

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

?>
<div class="form">

    <?php echo CHtml::beginForm(); ?>

        <?php echo CHtml::errorSummary( $model ); ?>

        <div class="row">
            <?php echo CHtml::activeLabel( $model, 'username' ); ?>
            <?php echo CHtml::activeTextField($model,'username') ?> <!-- ERROR IF I INCLUDE THE TEXTBOX. WHY? -->
        </div>

    <?php echo Chtml::endForm(); ?>

</div>

Upvotes: 0

Views: 456

Answers (1)

secretlm
secretlm

Reputation: 2361

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():

<?php

    class BlogController extends Controller {

        public function actionIndex() {

            $model = new Blog;

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

        }

    }

?>

Upvotes: 2

Related Questions