今天来进行用户注册功能
yii集成了很多验证框架,framework/validators下面,这个文件夹的最后一个文件
CValidator.php 是总的验证信息,根据请求参数决定需要那种验证 如:url验证 “'url'=>'CUrlValidator',”
email验证 'url'=>'CUrlValidator',
1。定义数据表模型,models/xxx.php
2,配置好控制器和试图的信息后,打开用户注册页面,把里面的form表单,用户输入框 和 label 用yii小部件代替
3。在模型里面设置标签名字与数据库字段对应 如user.model
public function attributeLabels(){
array(
"name"=>"用户名",
"password"=>"密码",
"email"=>"邮箱",
"QQ"=>"QQ",
"tel"=>"电话",
"sex"=>"性别",
"xueli"=>"学历",
"hoddy"=>"爱好",
"introduce"=>"简历",
);
}
注册表单整体代码如下
<BODY>
<!--放入view具体内容-->
<div class="block box">
<div class="usBox">
<div class="usBox_2 clearfix">
<div class="logtitle3"></div>
<?php $form=$this->beginWidget('CActiveForm');?>
<table cellpadding="5" cellspacing="3"
style="text-align: left; width: 100%; border: 0;">
<tbody>
<tr>
<td style="width: 13%; text-align: right;">
<?php echo $form->label($user_model,'name')?>
</td>
<td style="width: 87%;">
<?php echo $form->textField($user_model,'name', array('class'=>'inputBg()','id'=>'username'));?>
<span style="color: red;"></span>
</td>
</tr>
<tr>
<td align="right">
<?php echo $form->label($user_model,'password')?>
</td>
<td>
<?php echo $form->passwordField($user_model,'password', array('class'=>'inputBg()','id'=>'passowrd'));?>
</td>
</tr>
<tr>
<td align="right"><label for="User_password2">密码确认</label></td>
<td>
<input class="inputBg" size="25" name="User[password2]" id="User_password2" type="password" />
</td>
</tr>
<tr>
<td align="right"><?php echo $form->label($user_model,'email')?></td>
<td>
<?php echo $form->textField($user_model,'email', array('class'=>'inputBg()','id'=>'email'));?>
</td>
</tr>
<tr>
<td align="right"><?php echo $form->label($user_model,'QQ')?></td>
<td>
<?php echo $form->textField($user_model,'QQ', array('class'=>'inputBg()','id'=>'qq'));?>
</td>
</tr>
<tr>
<td align="right"><?php echo $form->label($user_model,'tel')?></td>
<td>
<?php echo $form->textField($user_model,'tel', array('class'=>'inputBg()','id'=>'tel'));?>
</td>
</tr>
<tr>
<!--radioButtonList($model,$attribute,$data,$htmlOptions=array())-->
<td align="right"><?php echo $form->label($user_model,'sex')?></td>
<td>
<?php echo $form->radioButtonList($user_model,'sex',$sex,array('separator'=>' '))?>
</td>
</tr>
<tr>
<!--dropDownList($model,$attribute,$data,$htmlOptions=array())-->
<td align="right"><?php echo $form->label($user_model,'xueli')?></td>
<td>
<?php echo $form->dropDownList($user_model,'xueli',$xueli); ?>
</td>
</tr>
<tr>
<!--checkBoxList($model,$attribute,$data,$htmlOptions=array())-->
<td align="right">
<?php echo $form->label($user_model,'hobby')?></td>
<td>
<?php echo $form->checkBoxList($user_model,'hobby',$hoddy,array('separator'=>' ')); ?>
</td>
</tr>
<tr>
<!--textArea($model,$attribute,$htmlOptions=array())-->
<td align="right">
<?php echo $form->label($user_model,'introduce')?></td>
<td>
<?php echo $form->textArea($user_model,'introduce',array('cols'=>50,'rows'=>5)); ?>
</td>
</tr>
<tr>
<td> </td>
<td align="left"><input name="Submit" value="提交"
class="us_Submit_reg" type="submit" /></td>
</tr>
<tr>
<td colspan="2"> </td>
</tr>
</tbody>
</table>
<?php $this->endWidget(); ?>
</div>
</div>
</div>
<!--放入view具体内容-->
</div>
</BODY>
如果需要设置邮箱不能为空,那么可以把CEmailValidator.php修改 public $allowEmpty=true;