[newbie] rules (unique & compare)

Hi!

I a newbie on YII and trying to create my very first form on it…

My tables…


CREATE TABLE IF NOT EXISTS `Entreprise` (

  `id` bigint(20) NOT NULL AUTO_INCREMENT,

  ...

  `blnActif` tinyint(1) NOT NULL,

  `created` datetime NOT NULL,

  PRIMARY KEY (`id`),

  KEY `typeId` (`entrepriseTypeId`)

) ENGINE=MyISAM DEFAULT CHARSET=utf8;


REATE TABLE IF NOT EXISTS `EntrepriseUser` (

  `id` bigint(20) NOT NULL AUTO_INCREMENT,

  `entrepriseId` bigint(20) NOT NULL,

  ...

  `email` varchar(255) NOT NULL,

  `password` varchar(255) NOT NULL,

  `blnActif` tinyint(1) NOT NULL,

  `lastlogin` datetime DEFAULT NULL,

  `created` datetime NOT NULL,

  PRIMARY KEY (`id`),

  UNIQUE KEY `email` (`email`),

  KEY `entrepriseId` (`entrepriseId`)

) ENGINE=MyISAM DEFAULT CHARSET=utf8;

my rules function in models/EntrepriseRegister.php file




	public function rules()

	{

		return array(

			array('emailUser', 'email'),

			array('emailUser', 'unique', 'attributeName'=>'EntrepriseUser.email'),

			array('passwordUser', 'length', 'min' => 6),

			array('passwordUser', 'compare', 'compareAttribute'=>'passwordCheckUser')

		);

	}



part of my views/register.php file


<fieldset>

    <div class="row">

        <?php echo CHtml::activeLabel($model,'emailUser'); ?>

        <?php echo CHtml::activeTextField($model,'emailUser') ?>

    </div>

    <div class="row">

        <?php echo CHtml::activeLabel($model,'passwordUser'); ?>

        <?php echo CHtml::activePasswordField($model,'passwordUser') ?>

    </div>

    <div class="row">

        <?php echo CHtml::activeLabel($model,'passwordCheckUser'); ?>

        <?php echo CHtml::activePasswordField($model,'passwordCheckUser') ?>

    </div>

</fieldset>

My problems are:

  • the rule "unique" on "emailUser" generates an error…

  • the rule “compare” on password doesn’t work => it returns always “Password User must be repeated exactly”

Thanks in advance for your sugestions…

the unique validator validates against an AR table, You either have to put that validator inside an AR model or add the ‘modelClass’ attribute to that rule and specify the AR model where the ‘emailUser’ located in.

The compare attribute compares between two elements. You need to specify the rule


array('passwordUser', 'compare', 'compareAttribute'=>'passwordCheckUser'),

as:


array('passwordCheckUser', 'compare', 'compareAttribute'=>'passwordUser'),

Many thanks, Vince! :slight_smile:

If that may help other people, according to my tables and model, that is the correct rule:


array('emailUser', 'unique', 'className' => 'EntrepriseUser', 'attributeName' => 'email')

That is ‘className’ and ‘attributeName’ attributes…

http://www.yiiframework.com/doc/api/CUniqueValidator#className-detail

http://www.yiiframework.com/doc/api/CUniqueValidator#attributeName-detail