在我的基本应用程序项目中,我正在尝试集成注册表单,我收到此错误:
验证规则无效:规则必须同时指定属性名称和验证程序类型。
我的代码在这里。
SignUpForm.php
<?php
namespace app\models;
use yii\base\Model;
use app\models\User;
/**
* Signup form
*/
class SignupForm extends Model
{
public $user_fname;
public $user_email;
public $user_password_hash;
/**
* @inheritdoc
*/
public function rules()
{
return [
[['user_fname','user_email', 'user_password_hash'], 'required'],
// rememberMe must be a boolean value
['user_password_hash','match','pattern'=>'$\S*(?=\S*[a-z])(?=\S*[A-Z])(?=\S*[\d])\S*$','message'=>'Password must have atleast 1 uppercase and 1 number '],
[['user_password_hash'],'string','min'=>6],
//email validation
['user_email','email']
[['user_email'], 'string', 'max' => 255],
[['user_fname'], 'string', 'max' => 45],
];
}
/**
* Signs user up.
*
* @return User|null the saved model or null if saving fails
*/
public function signup()
{
if (!$this->validate()) {
return null;
}
$user = new SimUser();
$user->user_fname = $this->user_fname;
$user->user_email = $this->user_email;
$user->setPassword($this->user_password_hash);
$user->generateAuthKey();
$user->save();
return $user;
}
}
Site Controller.php /注册方法
public function actionSignup()
{
$model = new SignupForm();
if ($model->load(Yii::$app->request->post())) {
if ($user = $model->signup()) {
if (Yii::$app->getUser()->login($user)) {
return $this->goHome();
}
}
}
return $this->render('signup', [
'model' => $model,
]);
}
我的观看文件
<?php
/* @var $this yii\web\View */
/* @var $form yii\bootstrap\ActiveForm */
/* @var $model \frontend\models\SignupForm */
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
$this->title = 'Signup';
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="site-signup">
<h1><?= Html::encode($this->title) ?></h1>
<p>Please fill out the following fields to signup:</p>
<div class="row">
<div class="col-lg-5">
<?php $form = ActiveForm::begin(['id' => 'form-signup']); ?>
<?= $form->field($model, 'user_fname')->textInput() ?>
<?= $form->field($model, 'user_email')->textInput() ?>
<?= $form->field($model, 'user_password_hash')->passwordInput() ?>
<div class="form-group">
<?= Html::submitButton('Signup', ['class' => 'btn btn-primary', 'name' => 'signup-button']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
</div>
</div>
我的用户文件,这是必须将数据发布到的文件。我从这里检索登录详细信息。
namespace app\models;
use Yii;
/**
* This is the model class for table "sim_user".
*
* @property integer $user_id
* @property string $user_email
* @property string $user_password_hash
* @property string $user_fname
* @property string $user_lname
* @property integer $user_company
* @property string $user_authcode
* @property integer $user_suspended
* @property string $user_created
* @property integer $user_deleted
* @property string $user_auth_key
* @property string $user_access_token
*/
class SimUser extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'sim_user';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['user_email', 'user_password_hash']],
[['user_company', 'user_suspended', 'user_deleted'], 'integer'],
[['user_created'], 'safe'],
[['user_email'], 'string', 'max' => 255],
[['user_password_hash'], 'string', 'max' => 20],
[['user_fname', 'user_lname'], 'string', 'max' => 45],
[['user_auth_key'], 'string', 'max' => 32],
[['user_access_token'], 'string', 'max' => 100],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'user_id' => Yii::t('app', 'User ID'),
'user_email' => Yii::t('app', 'User Email'),
'user_password_hash' => Yii::t('app', 'User Password Hash'),
'user_fname' => Yii::t('app', 'User Fname'),
'user_lname' => Yii::t('app', 'User Lname'),
'user_company' => Yii::t('app', 'User Company'),
'user_authcode' => Yii::t('app', 'User Authcode'),
'user_suspended' => Yii::t('app', 'User Suspended'),
'user_created' => Yii::t('app', 'User Created'),
'user_deleted' => Yii::t('app', 'User Deleted'),
'user_auth_key' => Yii::t('app', 'User Auth Key'),
'user_access_token' => Yii::t('app', 'User Access Token'),
];
}
public function getAuthKey() {
return $this->user_auth_key;
}
public function getId() {
return $this->user_id;
}
public function validateAuthKey($authKey) {
return $this->user_auth_key = $authkey;
}
public static function findIdentity($id) {
return self::findOne($id);
}
public static function findIdentityByAccessToken($token, $type = null) {
return $this->user_access_token;
}
public static function findByUsername($email){
return self::findOne(['user_email'=>$email]);
}
public function validatePassword($password){
return $this->user_password_hash === $password;
}
public function setPassword($password)
{
$this->user_password_hash = Yii::$app->security->generatePasswordHash($password);
}
public function generateAuthKey()
{
$this->user_auth_key = Yii::$app->security->generateRandomString();
}
public function beforeSave($insert)
{
if (parent::beforeSave($insert))
{
if ($this->isNewRecord)
{
$this->user_auth_key = \Yii::$app->security->generateRandomString();
}
return true;
}
return false;
}
}
答案 0 :(得分:2)
SimUser类中的第一条规则可能会导致此错误。
return [
[['user_email', 'user_password_hash']],
...
];
也许你的意思是:
return [
[['user_email', 'user_password_hash'], 'required'],
...
];
答案 1 :(得分:1)
在您的代码中
['user_email','email']
应该是
[['user_email','email'], 'email],
或
[['user_email'],'email']