In YII and Model, take the value submitted by Form. I have a logon page using 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)); }
In my LoginForm Model, I want to get the userdomain value, but it is always empty
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; } }}
But I read it here in Question One to have a value.
Username '=> string 'dasdasda' (length = 8)
'Password' => string 'asdasd' (length = 9)
'Userdomain '=> string 'xxxxx' (length = 5)
Reply to discussion (solution)
I will check it again ..
Userdomain has been passed to Model ..
However, no userdomain/is uploaded from the Model to Component/UserIdentity/
The problem lies here.
CUserIdentity constructor
By default, only username, password...
Therefore, the value cannot be passed to CUserIdentity.
You must define another method in component/UserIdentity ..
Pass userdomain