yii 登录表单

yii 登录表单  

2012-05-09 17:21:18|  分类: yii |字号 订阅

model
class LoginForm extends CFormModel {}
表单的模型要继承CFormModel

controller
$model=new LoginForm;

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

controller 实例化要用到的表单,传递给views

views

<?php $form=$this->beginWidget('CActiveForm', array('id'=>'login-form','enableAjaxValidation'=>true,)); ?>

....

<?php $this->endWidget(); ?>

views用 beginWidget('CActiveForm')生成一个表单

填入表单元素

<div class="form"> <?php $form=$this->beginWidget('CActiveForm', array('id'=>'login-form','enableAjaxValidation'=>true,)); ?> <p class="note">Fields with <span class="required">*</span> are required.</p> <div class="row"> <?php echo $form->labelEx($model,'username'); ?> <?php echo $form->textField($model,'username'); ?> <?php echo $form->error($model,'username'); ?> </div> <div class="row"> <?php echo $form->labelEx($model,'password'); ?> <?php echo $form->passwordField($model,'password'); ?> <?php echo $form->error($model,'password'); ?> <p class="hint"> Hint: You may login with <tt>demo/demo</tt>. </p> </div> <div class="row rememberMe"> <?php echo $form->checkBox($model,'rememberMe'); ?> <?php echo $form->label($model,'rememberMe'); ?> <?php echo $form->error($model,'rememberMe'); ?> </div> <div class="row submit"> <?php echo CHtml::submitButton('Login'); ?> </div> <?php $this->endWidget(); ?> </div><!-- form -->

解释:

<?php echo $form->labelEx($model,'username'); ?>

输出model里各元素的配置值

labelEx 指输出 model的username的扩展配置值
label 指输出 model的username的配置值
$model 中username的配置的部分

public function rules() { return array( array('username, password', 'required'), array('rememberMe', 'boolean'), array('password', 'authenticate'), ); }

public function attributeLabels() { return array('rememberMe'=>'Remember me next time','username'=>'this is test',); }

views中

<?php echo $form->error($model,'password'); ?>

对应model的配置值为:

public function authenticate($attribute,$params) { $this->_identity=new UserIdentity($this->username,$this->password); if(!$this->_identity->authenticate()) $this->addError('password','Incorrect username or password.'); }


输出结果:
<label class="required" for="LoginForm_username">this is test <span class="required">*</span></label>

echo $form->labelEx($model,'map_id');
比如这个是输出 map_id 在model里配置的值,
如果我想在后面输出 数字 来输出ID1,  ID2这样的情况,就只能使用下面的做法:

CHtml::label(($model->map_id).$i);

====================================================================================
提交表单,处理表单
controller

$model=new LoginForm;

if(isset($_POST['LoginForm'])) { $model->attributes=$_POST['LoginForm']; if($model->validate() && $model->login()) $this->redirect(Yii::app()->user->returnUrl); }

$model->attributes 为setProperty()魔术方法,将表单的值赋值给 $model里相应的变量

model中要有相应的变量(类似extract($_POST))

public $username; public $password; public $rememberMe;


验证表单
controller
  if(isset($_POST['LoginForm']))
  {
   $model->attributes=$_POST['LoginForm'];
   if($model->validate() && $model->login())
    $this->redirect(Yii::app()->user->returnUrl);
  }
model 中的验证规则

public function rules() { return array( array('username, password', 'required'), array('rememberMe', 'boolean'), array('password', 'authenticate'), ); }


验证用户
controller

if(isset($_POST['LoginForm'])) { $model->attributes=$_POST['LoginForm']; if($model->validate() && $model->login()) $this->redirect(Yii::app()->user->returnUrl); } $this->render('login',array('model'=>$model)); }


model
public function login()
 {
  if($this->_identity===null)
  {
   $this->_identity=new UserIdentity($this->username,$this->password);
   $this->_identity->authenticate();
  }
  if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
  {
   $duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
   Yii::app()->user->login($this->_identity,$duration);
   return true;
  }else
   return false;
 }
UserIdentity 类放在components中,为了方便改变验证方法
使用YII的CUserIdentity 验证(主要是用了返回代码:ERROR_USERNAME_INVALID等)

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

CUserIdentity 类

class CUserIdentity extends CBaseUserIdentity {

public $username; public $password;

public function __construct($username,$password) { $this->username=$username; $this->password=$password; }

.......

CBaseUserIdentity 类

abstract class CBaseUserIdentity extends CComponent implements IUserIdentity { const ERROR_NONE=0; const ERROR_USERNAME_INVALID=1; const ERROR_PASSWORD_INVALID=2; const ERROR_UNKNOWN_IDENTITY=100;

......


===========================================================================================
User类 显示,获得用户的信息
User是CActiveRecord 类,可以操作数据库。将数据表字段都extracts为变量

table 'tbl_user': * @var integer $id * @var string $username * @var string $password * @var string $salt * @var string $email * @var string $profile


<?php class User extends CActiveRecord{ public static function model($className=__CLASS__) { return parent::model($className); } public function tableName() { return '{{user}}'; }

查询数据库

/ 查找满足指定条件的结果中的第一行 $user=User::model()->find($condition,$params); // 查找具有指定主键值的那一行 $user=User::model()->findByPk($userID,$condition,$params); // 查找具有指定属性值的行 $user=User::model()->findByAttributes($attributes,$condition,$params); // 通过指定的 SQL 语句查找结果中的第一行 $user=User::model()->findBySql($sql,$params);

$condition 是一条sql 的where 子句 ,格式:

find('LOWER(username)=?',array(strtolower($this->username)))

find('postID=:postID', array(':postID'=>10));

find('id = :id AND title = :title', array(':id' => 1,':title' => '标题',));

=====================================================================
密码验证:
use 模型

public function validatePassword($password) { return $this->hashPassword($password,$this->salt)===$this->password; }

public function hashPassword($password,$salt) { return md5($salt.$password); }


图示:


正确的话:
model记录用户

public function login() { if($this->_identity===null) { $this->_identity=new UserIdentity($this->username,$this->password); $this->_identity->authenticate(); } if($this->_identity->errorCode===UserIdentity::ERROR_NONE) { $duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days Yii::app()->user->login($this->_identity,$duration); return true; } else return false; }

controller返回前一URL

if(isset($_POST['LoginForm'])) { $model->attributes=$_POST['LoginForm']; if($model->validate() && $model->login()) $this->redirect(Yii::app()->user->returnUrl); }


错误时处理
controller

if(isset($_POST['LoginForm'])) { $model->attributes=$_POST['LoginForm']; if($model->validate() && $model->login()) $this->redirect(Yii::app()->user->returnUrl); }

$model->validate() 方法验证 rules()规定的规则

model

public function rules() { return array( array('username, password', 'required'), array('rememberMe', 'boolean'), array('password', 'authenticate'), ); }

public function authenticate($attribute,$params) { $this->_identity=new UserIdentity($this->username,$this->password); if(!$this->_identity->authenticate()) $this->addError('password','Incorrect username or password.'); }


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值