Yii register form does not work -
i write user form register in usermodel don't set rules.
the log said:
04:24:19.273058 warning application failed set unsafe attribute "username" of "user". in d:\xampp\htdocs\2013819\shop\protected\controllers\usercontroller.php (59) in d:\xampp\htdocs\2013819\shop\index.php (16) 04:24:19.273178 warning application failed set unsafe attribute "password" of "user". in d:\xampp\htdocs\2013819\shop\protected\controllers\usercontroller.php (59) in d:\xampp\htdocs\2013819\shop\index.php (16) 04:24:19.273281 warning application failed set unsafe attribute "user_email" of "user". in d:\xampp\htdocs\2013819\shop\protected\controllers\usercontroller.php (59) in d:\xampp\htdocs\2013819\shop\index.php (16) 04:24:19.273381 warning application failed set unsafe attribute "user_qq" of "user". in d:\xampp\htdocs\2013819\shop\protected\controllers\usercontroller.php (59) in d:\xampp\htdocs\2013819\shop\index.php (16) 04:24:19.273478 warning application failed set unsafe attribute "user_tel" of "user". in d:\xampp\htdocs\2013819\shop\protected\controllers\usercontroller.php (59) in d:\xampp\htdocs\2013819\shop\index.php (16) 04:24:19.273572 warning application failed set unsafe attribute "user_sex" of "user". in d:\xampp\htdocs\2013819\shop\protected\controllers\usercontroller.php (59) in d:\xampp\htdocs\2013819\shop\index.php (16) 04:24:19.273665 warning application failed set unsafe attribute "user_xueli" of "user". in d:\xampp\htdocs\2013819\shop\protected\controllers\usercontroller.php (59) in d:\xampp\htdocs\2013819\shop\index.php (16) 04:24:19.273761 warning application failed set unsafe attribute "user_hobby" of "user". in d:\xampp\htdocs\2013819\shop\protected\controllers\usercontroller.php (59) in d:\xampp\htdocs\2013819\shop\index.php (16) 04:24:19.273855 warning application failed set unsafe attribute "user_introduce" of "user". in d:\xampp\htdocs\2013819\shop\protected\controllers\usercontroller.php (59) in d:\xampp\htdocs\2013819\shop\index.php (16)
function actionregister(){ //实例化数据模型对象user $user_model = new user(); /** * renderpartial不渲染布局 * render会渲染布局 */ //$this ->renderpartial('register'); //性别信息 $sex[1] = "男"; $sex[2] = "女"; $sex[3] = "保密"; //定义学历 $xueli[1] = "-请选择-"; $xueli[2] = "小学"; $xueli[3] = "初中"; $xueli[4] = "高中"; $xueli[5] = "大学"; //定义爱好信息 $hobby[1] = "篮球"; $hobby[2] = "足球"; $hobby[3] = "排球"; $hobby[4] = "棒球"; //如果用户有注册表单 if(isset($_post['user'])){ //给模型收集表单信息 //foreach($_post['user'] $_k => $_v){ // $user_model -> $_k = $_v; //} //上边的foreach,在yii框架里边有优化,使用模型属性attributes来进行优化 //attributes 属性已经把foreach集成好了,我们可以直接使用 $user_model -> attributes = $_post['user']; //实现信息存储 if($user_model -> save()) $this ->redirect ('./index.php'); //重定向到首页 } $this -> render('register',array('user_model'=>$user_model,'sex'=>$sex,'xueli'=>$xueli,'hobby'=>$hobby)); } <?php /** * 用户模型model * 13-5-15 下午9:01 //时间通过netbeans快捷键 ctrl+j * 两个基本方法: * model * tablename */ class user extends cactiverecord{ //获得数据模型方法 public static function model($classname = __class__) { return parent::model($classname); } //定义数据表名字 public function tablename(){ return "{{user}}"; } //设置标签名字与数据库字段对应 public function attributelabels() { return array( 'username'=>'用户名', 'password'=>'密 码', 'user_sex'=>'性 别', 'user_qq'=>'qq号码', 'user_hobby'=>'爱 好', 'user_xueli'=>'学 历', 'user_introduce'=>'简 介', 'user_email'=>'邮 箱', 'user_tel'=>'手机号码', ); } /* * 实现用户注册表单验证 * 在模型里边设置一个方法,定义具体表单域验证规则 */ /* public function rules() { return array( array('username','required','message'=>'用户名必填'), array('password','required','message'=>'密码必填'), ); }*/ }
you must not comment out rules()
function.
in it, must set attributes safe, allow yii copy fields form model:
public function rules() { return array( array('username, password, user_sex, user_qq, user_hobby, user_xueli, user_introduce, user_email, user_tel', 'safe'), ); }
see also: http://www.yiiframework.com/wiki/161/understanding-safe-validation-rules/
Comments
Post a Comment