ホームページ  >  記事  >  バックエンド開発  >  YII、モデルのフォームによって送信された値を取得します

YII、モデルのフォームによって送信された値を取得します

WBOY
WBOYオリジナル
2016-06-23 14:01:03826ブラウズ

LDAP を使用したログイン ページがあります...
コントローラー

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 モデルで、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;        }    }}


しかし、質問 1 で読んだ内容は貴重です
ユーザー名 => 文字列 'dasdasda' (長さ = 8)
'パスワード' => 文字列 'asdasdasd' (長さ = 9)
'ユーザードメイン' => 文字列 'xxxxx' (長さ = 5)


ディスカッションへの返信(解決策)

注意深く確認します...
ユーザードメインがモデルに渡されました...
しかし、モデルからコンポーネント/UserIdentityに渡すときにユーザードメイン/がありません

問題は、
CUserIdentity のコンストラクターがユーザー名、パスワード... のみを構築することです。
そのため、値を CUserIdentity に渡すことができません。
コンポーネント / UserIdentity... で別のメソッドを定義する必要があります。
userdomain を渡す必要があります。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
前の記事:アパッチ次の記事:アパッチ