Model Rules Compare With Custom Text Field

Hey, it’s my form




<?php $form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(

    'layout' => TbHtml::FORM_LAYOUT_VERTICAL,

 )); ?>

<fieldset>

    <?php echo $form->errorSummary($model); ?>

    <!-- SOME FORMS -->

    <?php echo $form->passwordFieldControlGroup($model, 'password'); ?>

    <label class="control-label required" for="password2">

        Powtórz hasło

        <span class="required">*</span>

    </label>

    <?php echo TbHtml::passwordFieldControlGroup('Register[password_repeat]', ''); ?>

</fieldset>

     

<?php echo TbHtml::formActions(array(

    TbHtml::submitButton('Załóż konto', array('color' => TbHtml::BUTTON_COLOR_PRIMARY)),

)); ?>

     

<?php $this->endWidget(); ?>



And I want to compare ‘password’ with ‘password_repeat’.

I’ve added an array in model rules


array('password_repeat', 'safe'),

array('password_repeat', 'required'),

with this code I get an error that "Register.password_repeat" is not defined.

So how to compare these 2 variables?

I’d just do the verification using Javascript. Sometimes that is easier…




if($("#current_password").val().length == 0){

	js:bootbox.alert("You must enter your current password");

	$("#current_password").focus();

	return;

}


if($("#new_password").val().length == 0){

	js:bootbox.alert("You must enter your new password");

	$("#new_password").focus();

	return;

}


if($("#new_password_confirm").val().length == 0){

	js:bootbox.alert("You must re-enter your new password");

	$("#new_password_confirm").focus();

	return;

}


if($("#new_password").val() != $("#new_password_confirm").val()){

	js:bootbox.alert("The new passwords do not match");

	return;

}

							



Also here’s the way Yii handles it if you want to go that route:

You can add the following rule to your model:




return array(

    // Other rules.

    array('pass', 'compare', 'compareAttribute'=>'passConfirmation',

        'on'=>'insert'),

);

”




<?php echo $form->passwordFieldControlGroup($model, 'password'); ?>

<label class="control-label required" for="password2">

        Repeat password

        <span class="required">*</span>

</label>

        <?php echo TbHtml::passwordFieldControlGroup('Register[password_repeat]', ''); ?>


array('password_repeat', 'compare', 'compareAttribute'=>'password','on'=>'insert'),

Not working. Again error "password_repeat" is not defined

An array looks fine


Array ( [Register] => Array ( [login] => LOGIN [email] => E-MAIL [password] => PASSWORD [password_repeat] => PASSWORD REPEAT )

I found a solution. We just have to add public $variable in model class. That’s all.