I’m trying to implement user registration using yii ActiveRecord.
the problem is that every time the view is being loaded i get the following exception:
Users has an invalid validation rule. The rule must specify attributes to be validated and the validator name.
#0
+ /Applications/MAMP/htdocs/yiiRoot/framework/base/CModel.php(259): CModel->createValidators()
#1
+ /Applications/MAMP/htdocs/yiiRoot/framework/web/helpers/CHtml.php(1758): CModel->getValidators("username")
#2
+ /Applications/MAMP/htdocs/yiiRoot/framework/web/helpers/CHtml.php(1205): CHtml::activeInputField("text", Users, "username", array("name" => "Users[username]", "id" => "Users_username"))
here is the relevant part from the Users model:
public function rules()
{
return array(
array('username, password, confirmation_uuid, created_at', 'required'),
array('is_male, is_online, is_idle, is_confirmed', 'numerical', 'integerOnly'=>true),
array('username, password, first_name, last_name, conformation_uuid', 'length', 'max'=>255),
array('birthdate, updated_at', 'safe'),
array('id, username, password, first_name, last_name, birthdate, is_male, is_online, is_idle, is_confirmed, confirmation_uuid, created_at, updated_at', 'safe', 'on'=>'search'),
array('username','email','allowName'=>true,'allowEmpty'=>false ),
array('username','required'),
array('password','application.extensions.validators.password','strength'=>'weak', 'on'=>'register'),
array('password', 'compare', 'compareAttribute'=>'confirm_password', 'on'=>'register'),
array('username,password,confirm_password,first_name,last_name,birthdate,is_male,confirmation_uuid','on'=>'register'),
);
}
controller:
public function actionRegister()
{
$form=new Users;
// collect user input data
if(isset($_POST['Users']))
{
$form->attributes=$_POST['User']; // set all attributes with post values
$form->email=$form->username;
// NOTE Changes to any $form->value have to be performed BEFORE $form-validate()
// or else it won't save to the database.
// validate user input and redirect to previous page if valid
if($form->validate())
{
// save user registration
$form->save();
$loginURL = Yii::app()->createUrl('site/login');
$this->redirect($this->render('login',array('loginURL'=>$loginURL))); // Yii::app()->user->returnUrl
}
}
// display the registration form
$this->render('register',array('form'=>$form));
}
view:
<div class="row">
<?php echo CHtml::activeLabel($form,'username'); ?>
<?php echo CHtml::activeTextField($form,'username'); ?>
<?php //echo $form->error($model,'username'); ?>
</div>
why is it checking the validator at that point? and what causes it to fail?
thanks!
Your last rule
has no validator name. You list all attributes, and then scenario, but not validator name.