Yii Framework Forum: [newbie] rules (unique & compare) - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

[newbie] rules (unique & compare) Rate Topic: -----

#1 User is offline   GiB Swiss 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 01-May 10

Posted 01 May 2010 - 09:06 AM

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...
0

#2 User is offline   Vince. 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 432
  • Joined: 29-December 08
  • Location:Los Angeles,CA

Posted 01 May 2010 - 10:18 AM

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'),

Vince Gabriel, Lead Developer.
Posted ImagePosted ImagePosted Image
2

#3 User is offline   GiB Swiss 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 01-May 10

Posted 01 May 2010 - 11:13 AM

Many thanks, Vince! :-)
0

#4 User is offline   GiB Swiss 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 01-May 10

Posted 01 May 2010 - 12:24 PM

View PostVince., on 01 May 2010 - 10:18 AM, said:

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.


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.yiiframew...lassName-detail
http://www.yiiframew...buteName-detail
2

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users