YII,Model里面取Form提交的值
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了YII,Model里面取Form提交的值,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3053字,纯文字阅读大概需要5分钟。
内容图文
![YII,Model里面取Form提交的值](/upload/InfoBanner/zyjiaocheng/199/54f2c90247cf499085bae338cff54fa9.jpg)
Controller
public function actionLogin() { $model = new LoginForm; // if it is ajax validation request if (isset($_POST['ajax']) && $_POST['ajax'] === 'LoginForm') { echo CActiveForm::validate($model); Yii::app()->end(); } if (isset($_POST['LoginForm'])) { $model->attributes = $_POST['LoginForm']; var_dump($model->attributes); //Question One if ($model->validate() && $model->login()) { $this->redirect(array('form/index')); } } $this->render('login', array('model' => $model)); }
我的LoginForm Model里面,我要取userdomain的值,但是一直为空
class LoginForm extends CFormModel { public $username; public $password; public $userdomain; private $_identity; /** * Declares the validation rules. * The rules state that username and password are required, * and password needs to be authenticated. */ public function rules() { return array( // username and password are required array('username', 'required', 'message' => 'Pls Key In Your NT Account.'), array('password', 'required', 'message' => 'Pls Key In Your NT Password.'), array('userdomain', 'required', 'message' => 'Pls Select Your Domain Host.'), // password needs to be authenticated array('password', 'authenticate'), ); } /** * Declares attribute labels. */ public function attributeLabels() { return array( 'userdomain'=>'User Domain', ); } /** * Authenticates the password. * This is the 'authenticate' validator as declared in rules(). */ public function authenticate($attribute, $params) { if (!$this->hasErrors()) { $this->_identity = new UserIdentity($this->username, $this->password, $this->userdomain); if (!$this->_identity->authenticate()) $this->addError('password', 'Incorrect username or password.'); } } /** * Logs in the user using the given username and password in the model. * @return boolean whether login is successful */ public function login() { if ($this->_identity === null) { $this->_identity = new UserIdentity($this->username, $this->password, $this->userdomain); $this->_identity->authenticate(); } if ($this->_identity->errorCode === UserIdentity::ERROR_NONE) { return true; } else { return false; } }}
但是我在Question One这里读取来是有值的
username' => string 'dasdasda' (length=8)
'password' => string 'asdasdasd' (length=9)
'userdomain' => string 'xxxxx' (length=5)
回复讨论(解决方案)
我再仔细查咯一下..
userdomain已经传给Model咯..
但是我从Model里面传到Component / UserIdentity的没有userdomain/
问题出在这里
CUserIdentity的构造函数
默认只有构造username,password...
所以不能传值给CUserIdentity.
必须在component / UserIdentity里面再定义一个方法..
把userdomain给传递进去
内容总结
以上是互联网集市为您收集整理的YII,Model里面取Form提交的值全部内容,希望文章能够帮你解决YII,Model里面取Form提交的值所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。