I have problems generating errors in a model by configuring rules/validators.
In Yii 1.1 i used option “allowEmpty”=>false in validators to require them to have a valid value, what should I use in Yii2?
Also the integer validator has problems, is set option “integerOnly”=>true, but that does not give error when I give the attribute a string.
The model:
namespace app\models;
class Test extends \yii\base\Model {
public $date;
public $age;
public function rules() {
return [
// This should give error if value is null because of skipOnEmpty=false
[['date'], 'date', 'format'=>'php:Y-m-d', 'skipOnEmpty'=>false],
// This should give error if value is string "20"
[['age'], 'integer', 'integerOnly'=>true],
];
}
}
The code I test with gives empty array in last line:
$model = new Test();
$model->age = '20'; // integer validator option "integerOnly"=>true should cause error
$model->date = null; // skipOnEmpty=>false should cause error (empty not allowed)
$model->validate();
var_dump($model->getErrors());
I have tested this code on Yii 2.0.35, 2.0.48 and 2.0.48.1.