[英]trouble with registration using yii framework
I'm trying to implement user registration using yii ActiveRecord. 我正在尝试使用yii ActiveRecord来实现用户注册。 the problem is that every time the view is being loaded i get the following exception:
问题是,每次加载视图时,我都会收到以下异常:
User has an invalid validation rule.
The rule must specify attributes to be validated and the validator name.
here is the relevant part from the Users model: 这是用户模型的相关部分:
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('username, password,verifyCode,email','message'=>'Tidak boleh kosong'),
//array('password','compare'),
array('verifyCode', 'captcha', 'required', 'allowEmpty'=>!extension_loaded('gd')),
array('level_id', 'numerical', 'integerOnly'=>true),
array('username', 'length', 'max'=>20),
array('password, email', 'length', 'max'=>50),
//array('password_repeat','required'),
array('avatar', 'file', 'types'=>'gif,png,jpg'),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('id, username, password, email, joinDate, level_id, avatar', 'safe', 'on'=>'search'),
);
}
Controller: 控制器:
public function actionRegister() {
$registration = new RegistrationForm("register");
// collect user input data
if (isset($_POST['RegistrationForm'])) {
$registration->attributes = $_POST['RegistrationForm'];
$registration->attributes = $_POST['RegistrationForm'];
// validate user input and redirect to the previous page if valid
if ($registration->validate()) {
// create an account model
$account = new User;
$account->username = $registration->username;
$account->password = $registration->password;
$account->email = $registration->email;
$account->joinDate = new CDbExpression('NOW()');
//$account->level_id = 1;
if ($account->save()) {
$member = new Member;
$member->attributes = $registration->attributes;
$member->user_id = $account->id;
if ($member->save()) {
$this->redirect(array('index'));
} else {
$registration->addErrors($member->getErrors());
}
} else {
$registration->addErrors($account->getErrors());
}
}
}
$this->render('register', array('model' => $registration));
}
This is because you missed a validator in your first rule. 这是因为您在第一条规则中错过了验证者。 As I think, you missed the required validator.
我认为,您错过了必需的验证器。
So you need to change this: 因此,您需要更改以下内容:
array('username, password,verifyCode,email','message'=>'Tidak boleh kosong'),
To something like this: 对于这样的事情:
array('username, password,verifyCode,email','required','message'=>'Tidak boleh kosong'),
Another mistake is, that your using the captcha validator . 另一个错误是,您使用验证码验证器 。 Captcha can only be used in a view with a CFormModel , a CCaptcha Widget and a CCaptcha action .
验证码只能在具有CFormModel , CCaptcha小部件和CCaptcha操作的视图中使用。
If you want a file upload, that isn't required, you need to add the allowEmpty : 如果要上传文件(不需要),则需要添加allowEmpty :
array('avatar', 'file', 'allowEmpty' => true, 'types'=>'gif,png,jpg'),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.