rules for additional fields ( not from database )

Hi ,

I want to give change password functionality to my site ,

I used code below in view ,

Fields are not specified in database.


<div class="row">

    	<?php echo CHtml::label('New Password','PASSWORD'); ?>

    	<?php echo CHtml::passwordField('PASSWORD'); ?>

     </div>

    

     <div class="row">

    	<?php echo CHtml::label('Confirm New Password','NEWPASSWORD'); ?>

    	<?php echo CHtml::passwordField('NEWPASSWORD'); ?>

     </div>

my query is ,

Can I use rules ( in model ) to compare both fields and give error message as in yii error format ???

Yes.

If you name your fields like this:

password

password_repeat

You can use the compare validator to make sure they match.

See this:

http://www.yiiframework.com/doc/api/1.1/CCompareValidator

You can use CFormModel. It provides you with almost the same features as CActiveRecord does, except for db accessing.

And yes, you can use CCompareValidator to compare the 2 fields of your input form that has a CFormModel model as its background.

The following 3 sections in the guide will show you the details.

Creating Model

Creating Action

Creating Form

here i used following code in model ,


return array(

    array('E_NAME, E_EMAIL,  PASSWORD', 'required'),

but PASSWORD is not any column in database , this not show required field (*) in view ,

i use following code for view ,


<div class="row">

    	<?php echo CHtml::label('New Password','PASSWORD'); ?>

    	<?php echo CHtml::passwordField('PASSWORD'); ?>

</div>

and what i use to show error like normally we use code like ,


<div class="row">

		<?php echo $form->labelEx($model,'E_NAME'); ?>

     		<?php echo $form->textField($model,'E_NAME', array( 'value' => $empData['E_NAME'])); ?>

		<?php echo $form->error($model,'E_NAME'); ?>    //  see here we use to display error

</div>

In a CActiveRecord model, you can add a property that is not a database column. And it will be treated almost the same as other database backed properties.




public $new_password;

public $new_password_confirm;

...

return array(

    array('E_NAME, E_EMAIL, new_password, new_password_confirm', 'required'),

    array('new_password', 'compare', 'compareAttribute'=>'new_password_confirm'),

...

<div class="row">

    <?php echo $form->labelEx($model, 'new_password'); ?>

    <?php echo $form->passwordField($model, 'new_password'); ?>

    <?php echo $form->error($model,'new_password'); ?>

</div>

   

<div class="row">

    <?php echo $form->labelEx($model, 'new_password_confirm'); ?>

    <?php echo $form->passwordField($model, 'new_password_confirm'); ?>

    <?php echo $form->error($model,'new_password_confirm'); ?>

</div>



And a CFormModel model is a model that has no database backed properties.

So, take a time to read the sections of the guide in my previous post, and it will be a great help to you.

Its working now.

thankx.

:D