Search code examples
phpyii2yii2-basic-app

Issue with an ActiveRecord Class rules with yii2


I just started learning Yii 2 yesterday and I have a problem that I don't understand. It's working well with this code, but if I uncomment the 2 lines I have this error:

[...]a rule must specify both attribute names and validator type.

<?php
namespace app\models\customer;
use yii\db\ActiveRecord;

class CustomerRecord extends ActiveRecord
{
    public static function tableName()
    {
        return 'customer';
    }

    public function rules()
    {
        return [
            //['name' => 'string'],
            //['name' => 'required'],
            ['birth_date', 'date', 'format' => 'd-m-Y'] ,
            ['birth_date', 'required'] ,
            ['notes', 'safe'] ,
        ];
    }
}

I made some researches before posting here.


Solution

  • You list single attribute or array of attributes for the rule, then name of validator, then validator parameters so it should be:

    ['name', 'string'],
    ['name', 'required'],