Reputation: 313
i am new to YII framework. i am writing a simple method to change password, i am want to validate the controls like if blank then show the error message or if password and re-type password does not match then show error. i have used the below code to do this :
public function actionChange()
{
$model=new Users;
$model->scenario='change';
if (isset($_POST['Users'])) {
$model->setAttributes($_POST['Users']);
if($model->validate())
{
print "true";
}
else{
print "false";
}
if($model->validate())
{
$pass = md5($_POST['Users']['newPassword']);
$userModel = Users::model()->findByPk(Yii::app()->user->id);
$userModel->password = $pass;
$data = $userModel->update();
}
if(isset($data))
{
Yii::app()->user->setFlash('success',"Password changed successfully!");
$this->redirect(array('Users/change'), true);
}
}
$this->render('change_password', array('model'=>$model,true));
}
But **$model->validate()**
always returns false to me either i fill the fields or not.
UPDATED:
here is rule function :
public function rules()
{
return array(
array('is_active', 'numerical', 'integerOnly'=>true),
array('first_name, joining_date,last_name, employee_code, username, password, role', 'required'),
array('employee_code', 'numerical', 'integerOnly'=>true),
array('username','email'),
array('username','valid_username','on'=>array('create')),
//array('username', 'contraints', 'readOnly'=>true, 'on'=>'update'),
array('currentPassword, newPassword, newPasswordRepeat', 'required','on'=>array('change')),
//array('newPassword', 'length', 'min' => 6, 'max'=>20, 'message'=>Yii::t("translation", "{attribute} is too short.")),
//array('newPassword','ext.SPasswordValidator.SPasswordValidator', 'preset' => 'strong', 'max' => 41),
array('newPassword', 'compare', 'compareAttribute'=>'newPasswordRepeat','on'=>array('change')),
array('currentPassword', 'equalPasswords','on'=>array('change')),
array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements(),'on'=>array('forgotPassword')),
array('joining_date', 'safe'),
array('user_id, first_name, last_name, employee_code, username, password, role, joining_date, pending_regular_leave, pending_medical_leave, allocated_regular_leave, allocated_medical_leave, is_active', 'safe', 'on'=>'search'),
);
}
public function equalPasswords($attribute, $params)
{
$user = Users::findByPk(Yii::app()->user->id);
if ($user->password != md5($this->currentPassword))
$this->addError($attribute, 'Old password is incorrect.');
}
please help me.
Upvotes: 2
Views: 10088
Reputation: 7556
Try using the correct method for setting the scenario you have two options:
$model=new Users('change');
or
$model=new Users;
$model->setScenario('change');
Also another issue is you aren't loading a model from the DB, unless you let them enter their user_id or whatever your primary key is on the form. Most likely you need to add this:
$model = Users::model()->findByPk(Yii::app()->user->id);
//$model=new Users; //Should be removed
Otherwise it doesn't even know which record it is suppose to update in the DB plus all your other fields will be completely empty.
Also if you are using a form widget like CActiveForm you can also add this to your view and it will show you the messages from failed validations:
echo $form->errorSummary($model);
Upvotes: 2
Reputation: 5355
For some reason your validation failes. Try to display validation error in else block (when validation fails).
if($model->validate()) {
....
}
else {
$errors = $model->getErrors();
var_dump($errors); //or print_r($errors)
exit;
}
Another thing if it doesn't help, you can try to comment some validation rules from your rules and see what rule failes.
Upvotes: 4