首页 > 后端开发 > php教程 > YII,Model里面取Form提交的值

YII,Model里面取Form提交的值

WBOY
发布: 2016-06-23 14:01:03
原创
904 人浏览过

我有一个登录页面,用的是LDAP...
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
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板