rknalajala
(Ramakrishnanalajala)
September 7, 2011, 9:41am
1
hi all,
i have a form wherein i have 3 fields
old password
newpassword
confirmpassword
i want to compare new password and confirm password.i have tried the following code
<tr>
<div class="row">
<td><?php echo $form->labelEx($model,'New Password'); ?> </td>
<td><?php
echo $form->passwordField($model,'password',array('value'=>'','class'=>'required'),array('size'=>45,'maxlength'=>45)); ?></td>
</div>
</tr>
<tr>
<div class="row">
<td><?php echo $form->labelEx($model,'Confirm Password'); ?> </td>
<td><?php
echo $form->passwordField($model,'password',array('value'=>'','class'=>'bounce'),array('size'=>45,'maxlength'=>45)); ?></td>
</div>
</tr>
model rules:
array('password', 'compare', 'compareAttribute'=>'cpassword','on'=>'Generate'),
but if i give different values in both fields it is taking only one value rather it should prompt me an error.
where did i go wrong?plz help
thanks
gregmolnar
(Molnargerg)
September 7, 2011, 9:56am
2
<tr>
<div class="row">
<td><?php echo $form->labelEx($model,'New Password'); ?> </td>
<td><?php
echo $form->passwordField($model,'password',array('value'=>'','class'=>'required'),array('size'=>45,'maxlength'=>45)); ?></td>
</div>
</tr>
<tr>
<div class="row">
<td><?php echo $form->labelEx($model,'Confirm Password'); ?> </td>
<td><?php
echo $form->passwordField($model,'cpassword',array('value'=>'','class'=>'bounce'),array('size'=>45,'maxlength'=>45)); ?></td>
</div>
</tr>
The confirm password field is the same as the password in your code. You need to have a virtual attribute for the password confirmation and create a field to that.
rknalajala
(Ramakrishnanalajala)
September 7, 2011, 12:01pm
3
i have defined cpassword in my model but i dont get to know what is a virtual attribute please let me know it.
is there something more to be done?
mukeshsoni
(Mukeshsoni)
September 7, 2011, 12:24pm
4
create another attribute in your model -
public $confirmPassword;
make the attribute safe and set it as comparison attribute
array(‘cpassword’, ‘compare’, ‘compareAttribute’=>‘confirmPassword’),
array(‘confirmPassword’, ‘safe’)); //for allowing massive assignment
Specify the $confirmPassword as the input name for your confirm password field in the view
sayan
(Devianossen)
September 7, 2011, 1:19pm
5
CompareAttribute should be ‘password’.
<your User model class>
...
public $cpassword = '';
...
public function rules() {
return array(
...
array('password', 'required', 'on' => '..., updatePassword'),
array('password', 'length', 'min' => 6, 'max' => 32, 'message' => ...),
array('cpassword', 'compare', 'compareAttribute' => 'password', 'on' => '..., updatePassword', 'message' => ... ),
...
$cpassword as the input name for your confirm password field in the view.