hd.
hd.

Reputation: 18316

registration form using Yii framework

I am new to Yii framework. I want to implement a registration form with Yii . I check the blog project on its demo projects but it hasn't got a registration form. Do anyone knows a tutorial about this issue?

Upvotes: 4

Views: 21605

Answers (4)

AnsellC
AnsellC

Reputation: 835

First, create a table for Users. Generate a User model and a controller based on this table using gii and adjust the code to your liking. Inside the User controller create a function to handle the registration:

function actionRegister() {
    $model = new User('register');

    // collect user input data
    if(isset($_POST['User']))
    {
        $model->attributes=$_POST['User'];

        // validate user input and redirect to the previous page if valid
        $model->setAttributes(array(
            'datereg' => time(), //additional data you want to insert
            'lastlogin' => time() //additional

        ));

        if($model->save())
        {
                           //optional 
            $login=new LoginForm;
            $login->username = $_POST['User']['username'];
            $login->password = $_POST['User']['password'];
            if($login->validate() && $login->login())
                $this->redirect('/pages/welcome');
        }

    }
    else
       // display the registration form
       $this->render('register',array('model'=>$model));
    }

You must have a valid view file (register.php)

$login=new LoginForm;
$login->username = $_POST['User']['username'];
$login->password = $_POST['User']['password'];
if($login->validate() && $login->login())
$this->redirect('/pages/welcome');

This block of code 'authenticates' the user and logs him in right after a successful registration. It uses CUserIdentity. $login->login() hashes the password.

Also, you must have something like this to process the data before it inserts it into the table

    public function beforeSave() {

    if(parent::beforeSave() && $this->isNewRecord) {

        $this->password = md5($this->password);

    }

    return true;
}

Hashing the password inside the controller is also fine. However I wanna do everything DB related inside the Model class.

You may notice that I didn't call $model-validate() here. This is because $model->save() also calls the validation method. More info: http://www.yiiframework.com/doc/api/1.1/CActiveRecord#save-detail

Upvotes: 4

For registration system.

You should follow the steps.

  1. 1st you make a table user/signup/register as you wish.
  2. Now create CRUD for that so you can easily insert your data in user table.

I have a table and it have five different fields i.e. id, name, conatct_no, email, password

  1. Now you create a folder in view named it "register".
  2. In view you just made a file index.php and register.php
  3. register.php is your form. which you can made.

Register.php

<h2>Registration Form</h2>
<div class="form">
<?php echo CHtml::beginForm(); ?>

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

    <div class="row">
        <?php echo CHtml::activeLabel($model,'Name'); ?>
        <?php echo CHtml::activeTextField($model,'name') ?>
    </div>


    <div class="row">
        <?php echo CHtml::activeLabel($model,'Contact Number'); ?>
        <?php echo CHtml::activeTextField($model,'contact_no') ?>
    </div>

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

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

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

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

    <?php echo CHtml::endForm(); ?>
</div><!-- form -->
  1. Now you need a controller file for registration i.e. registerController.php.

registerController.php

class registerController extends Controller
{
  public  function actionIndex() {
    $this->render('index');
  }

  public function actionRegister() {
    $model   = new RegisterForm ;
    $newUser = new User;

if(isset($_POST['RegisterForm'])) {
      $model->attributes = $_POST['RegisterForm'];
      if($model->validate()) {
      $newUser->name = $model->name ;
      $newUser->contact_no = $model->contact_no;
      $newUser->email = $model->email;
      $newUser->password = $model->password;
      if($newUser->save()) {
       $this->_identity = new UserIdentity($newUser->email,$newUser->password);
        if($this->_identity->authenticate())
         Yii::app()->user->login($this->_identity);
         $this->redirect(Yii::app()->user->returnUrl);
        }
      }
    }
$this->render('register',array('model'=>$model));
  }
}
  1. Now you need a model file for your validation. RegisterForm.php

    class RegisterForm extends CFormModel { public $contact_no ; public $name ; public $email; public $password ; public $retypepassword;

    public function tableName() { return 'user'; }

    public function rules() { return array( array('name, contact_no, email, password, retypepassword', 'required'), array('name, email, password, retypepassword', 'length', 'max'=>200), array('email', 'email', 'message'=>'Please insert valid Email'), array('contact_no', 'length', 'max'=>30), array('retypepassword', 'required', 'on'=>'Register'), array('password', 'compare', 'compareAttribute'=>'retypepassword'), array('id, name, contact_no, email', 'safe', 'on'=>'search'), ); } }

You need to also change the UserIdentity.php file in your protected/component directory.

class UserIdentity extends CUserIdentity
{
  private $_id ;

  public function authenticate()
  {
    $email = strtolower($this->username);
    $user = User::model()->find('LOWER(email)=?',array($email));
    if($user === null)
     $this->errorCode = self::ERROR_USERNAME_INVALID ;
    else if(!$user->password === $this->password)
     $this->errorCode = self::ERROR_PASSWORD_INVALID ;
    else {
     $this->_id = $user->id ;
     $this->username = $user->email ;
     $this->errorCode = self::ERROR_NONE ;
    }
     return $this->errorCode == self::ERROR_NONE ;
     return !$this->errorCode;
   }

   public function getId() {
    return $this->_id ;
   }    
} 

That's it.

It is a complete registration form set up, i have made it myself.

If you are first timer, then you need to understand how the controller work and how controller interact with view and model. If you are already familiar with it then you may easily can make registration system with your own.

Here is the link which help you to understand yii.

http://www.yiiframework.com/screencasts/

Thanks.

Upvotes: 6

Dmitrii Korotovskii
Dmitrii Korotovskii

Reputation: 586

  • You must create Registration form, using CAvtiveForm (see LoginForm for example).
  • Create your Users model.
  • Create controller:

    class UsersController extends Controller
    {
         public function actionSignup()
         {
            $model = new RegistrationForm;
    
            if (isset($_POST['RegistrationForm'])) {
                 if ($model->validate()) {
                     $user = new Users;
                     $user->populateRecord($form->attributes);
                     $user->save();
                     $this->redirect(Yii::app()->createUrl('/'));
                 }
            }
    
            $this->render('signup', array('model' => $model));
         }
     }
    

Upvotes: 0

Onkar Janwa
Onkar Janwa

Reputation: 3950

It is easy to create a form with yii using CActiveForm.

Here is a link of yii documentation :: Yii CActiveForm

Upvotes: 1

Related Questions