Invalid validation rule: a rule must specify both attribute names and validator type, in Yii2

Mohan Prasad picture Mohan Prasad · Jun 14, 2016 · Viewed 8.5k times · Source

In my basic app project, I'm trying to integrate the signup form, I'm getting this error:

Invalid validation rule: a rule must specify both attribute names and validator type.

My code is here.

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/signup method

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,
        ]);
    }

my view file

<?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>

My User file, this s the file to which the data has to be posted.. I retrieve login details from here.

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;   
    }

}

Answer

Ed209 picture Ed209 · Jun 14, 2016

First rule in your SimUser class is probably causing this error.

return [
    [['user_email', 'user_password_hash']],
    ...
];

Maybe you mean:

return [
    [['user_email', 'user_password_hash'], 'required'],
    ...
];