Home >Backend Development >PHP Tutorial >yii YII widget creates login form form Login form_PHP tutorial
YII框架必须遵循其表单的创建方法
登录模型错做与数据库操作模型是一致的,不同的是不跟数据库交互
,用的是小部件,在创建表单之前,要在用户控制模块完成以下代码
protected
--models
--LoginFrom
在这个LoginFrom里面设置标签名
/**
* Declares attribute labels.
*/
public function attributeLabels()
{
return array(
'username'=>'用户名',
'password'=>'密 码',
//'rememberMe'=>'Remember me next time',
}
接下来创建登陆模型对象(controllers 控制登录模块)
$user_login=new LoginForm();
$this ->render('login',array(''=>$user_login));//绿色部分和前台代码$user_login 要一致
接下来创建view视图
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
在前台登录页面可以这么写:
//绿色部分注意不要写错
beginWidget('CActiveForm'); ?>
////////////////////////////////////////////////// ///////////////////////////////////////////////////// ///////////////////////////////////////////////////// ///////////////////////////
Next, create the verification of the form data
It has two methods, one is the save method and the other is the validate method
The form data validation save() method can verify the data at the same time. If the verification is successful, the data can be stored in save(). There will be a step later that will execute the rules() method of the model, which is to verify the form
We are now using to implement the verification of the login form. We can call validate(). This method verifies the information we entered. This saves the save step, and the rules method will also be executed later
Configure verification prompt information (in model--->loginForm)
public function rules() ), > // Remembermene to be a Boolean ////////////////////////////////////////////////// ///////////////////////////////////////////////////// //////////////////////////// Use UerIdentity component for user verification
Username required'),
// Array ('Rememberme ",' Boolean '), // Password Needs to be authenticated
Array (' Password ', 'Authenticate '),
);
}
Then add an error message to the front-end login interface
error($user_login,'username')?>
error($user_login,'password'); ?>
www.bkjia.com