Update form managing
#1
Posted 28 April 2010 - 03:10 AM
i'm quite new to Yii... so this can be quite a stupid question.
I have a user moel that saves on my db sha1 versions of passwords. i'd like my users to be able to set a new password when updating their user profile... how could i prevent the password field to be already completed on the update form?
thanks a lot
joey
an Italian with Suomi inside.
#2
Posted 28 April 2010 - 03:29 AM
add $model->password='';
#3
Posted 28 April 2010 - 03:41 AM
i thought not to display the password field in the update form and i'd like to create a new action for the model: newpassword.
So, in UsersController i added an action:
public function actionNewpassword()
{
$model=$this->loadModel();
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['BoUsers']))
{
if($model->password == sha1($_POST['BoUsers']['password']));
$model->attributes=$_POST['BoUsers'];
$model->password = sha1($model->password);
if($model->save())
$this->redirect(array('view','id'=>$model->idBOUsers));
}
$this->render('newpassword',array(
'model'=>$model,
));
}
i thought that $this->render would have called the file newpassword.php in views folder... but i got an error 404... what's the matter?
thanks!
an Italian with Suomi inside.
#4
Posted 28 April 2010 - 04:04 AM
but I tryed to request a nonexistent view and
I'm getting error 500 UserController cannot find the requested view "updatexy", and you got the 404 - don't know why
#5
Posted 28 April 2010 - 04:53 AM
mdomba, on 28 April 2010 - 04:04 AM, said:
but I tryed to request a nonexistent view and
I'm getting error 500 UserController cannot find the requested view "updatexy", and you got the 404 - don't know why
thanks!!
i got it!
i was calling the link without passing an id to it... now it goes!
an Italian with Suomi inside.
#6
Posted 28 April 2010 - 08:14 AM
i added a field newpassword to the BoUsers class, then my _formpass.php looks like this:
<div class="row">
<?php echo $form->labelEx($model,'password'); ?>
<?php echo $form->passwordField(BoUsers::model(),'password',array('size'=>50,'maxlength'=>50)); ?>
<?php echo $form->error($model,'password'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'newpassword'); ?>
<?php echo $form->passwordField(BoUsers::model(),'newpassword',array('size'=>50,'maxlength'=>50)); ?>
<?php echo $form->error($model,'newpassword'); ?>
</div>
and my actionNewpassword is:
public function actionNewpassword()
{
$model=$this->loadModel();
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['BoUsers']))
{
if($model->validatePassword(sha1($_POST['BoUsers']['password']))){
$model->attributes=$_POST['BoUsers'];
echo $_POST['BoUsers']['newpassword'];
$model->password = sha1($model->newpassword);
if($model->save())
$this->redirect(array('view','id'=>$model->idBOUsers));
}
else $model->addError('password','The password provided is incorrect!');
}
$this->render('newpassword',array(
'model'=>$model,
));
} . i also added a rule in the model:
array('newpassword','required','on'=>'newpassword', 'message'=>'hey, guy... set a password!'),expecting my newpassword field to be required in the form, but it's not. Does anyone know why?
... nothing works at the moment
an Italian with Suomi inside.
#7
Posted 28 April 2010 - 08:23 AM
#8
Posted 28 April 2010 - 08:33 AM
Should i build a new scenario, then? i'll try that
an Italian with Suomi inside.
#9
Posted 28 April 2010 - 08:37 AM
#10
Posted 28 April 2010 - 08:52 AM
thanks a lot!
an Italian with Suomi inside.
#11
Posted 28 April 2010 - 11:09 AM
this way it asks me for a value for newpassword in the update action too... i think i should try setting up a newpassword scenario. i'll try that out. thanks a lot!
an Italian with Suomi inside.
#12
Posted 29 April 2010 - 03:26 AM
here's the code!
the function actionNewpassword sets the scenario:
public function actionNewpassword()
{
$model=$this->loadModel();
$model->setScenario("newpwd");
// Uncomment the following line if AJAX validation is needed
//$this->performAjaxValidation($model);
if(isset($_POST['BoUsers']))
{
if($model->validatePassword(sha1($_POST['BoUsers']['password']))){
$model->attributes=$_POST['BoUsers'];
$model->newpassword=$_POST['BoUsers']['newpassword'];
if($model->validate()){
$model->password = sha1($model->newpassword);
}
if($model->save()){
$this->redirect(array('view','id'=>$model->idBOUsers));
}
}
else $model->addError('password','The password provided is incorrect!');
}
$this->render('newpassword',array(
'model'=>$model,
));
} and this is the rule:
array('newpassword','required','on'=>'newpwd', 'message'=>'you must set a new password!'),
an Italian with Suomi inside.

Help











