syedsafir
syedsafir

Reputation: 13

form and validation not working in yii

This is my second question related to YII framework.
I have created a form in YII framework. It is not posting any data. Also validation is not working.

Here is the code of my Controller class:

   class RegisterController extends Controller
   {
    public function actionIndex()
    {
        $model = new C_regsiter();

        if (isset($_POST['C_register'])) {

            // do something
            $model->data = $_POST['C_register'];
            $model->username = $_POST['C_register']['username'];
            $model->Password = $_POST['C_register']['Password'];
            $model->email = $_POST['C_register']['email'];

            if ($model->validate()) {
              //do something
          }
        } else {}

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

Here is the Code of my Model Class:

    class C_regsiter extends CFormModel{

    public $username;
    public $Password;
    public $email;
    public $data;
    protected  $id;

    public function rules(){

    return array(
    array('username,password,email','required','on'=>'Register'),
    array('email','email'),         
    );

    }

    public function attributelabel(){

    return array(
     'username'=>'User Name',
     'password'=>'Password',
     'email'=>'Email Address '
    ); 

    }
    }

Here is the code of my View Class

   <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'); ?>
   <?php echo CHtml::error($model, 'username') ?>
   </div>

   <div class="row">
        <?php echo CHtml::activeLabel($model,'email'); ?>
        <?php echo TbHtml::activeEmailField($model , 'email') ?>
        <?php echo CHtml::error($model, 'email') ?>
   </div>

   <div class="row">
   <?php  echo CHtml::activeLabel($model, 'password'); ?>
   <?php  echo  CHtml::activePasswordField($model, 'Password');  ?>
   <?php echo CHtml::error($model, 'password') ?>
   </div>

   <div class="row">
     <?php echo CHtml::submitButton('Register') ;?>
   </div>

   <?php echo CHtml::endForm(); ?>  
   </p>

Any help is highly appreciated.

Upvotes: 0

Views: 575

Answers (2)

Bizley
Bizley

Reputation: 18021

Isn't this because you have got typo in model's name? C_regsiter instead of C_register and you check C_register POST data. Anyway use this.

public function actionIndex()
{
    $model = new C_regsiter();

    $data = Yii::app()->request->getPost('C_regsiter');
    if ($data) {
        $model->setAttributes($data);
        if ($model->validate()) {
            // ....
        }
    }

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

Upvotes: 0

Osama Jetawe
Osama Jetawe

Reputation: 2705

you have to set model attribute try this inside your action

class RegisterController extends Controller
 {

    public function actionIndex()
    {
        $model = new C_regsiter();
       if (isset($_POST['C_register'])) {
            $model->attributes = $_POST['C_register'];

            if ($model->validate()) {
               //do something

        }
    }

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

Upvotes: 1

Related Questions