我正在尝试使用 yii ActiveRecord 实现用户注册。问题是每次加载视图时都会出现以下异常:
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"))
这是用户模型的相关部分:
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'),
);
}
控制器:
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));
}
看法:
<div class="row">
<?php echo CHtml::activeLabel($form,'username'); ?>
<?php echo CHtml::activeTextField($form,'username'); ?>
<?php //echo $form->error($model,'username'); ?>
</div>
为什么此时要检查验证器?是什么导致它失败?谢谢!