~~~php
class MyModel extends Model
{
public $arrayAttribute = [];
public function rules()
{
return [
checks if every category ID is an integer
['categoryIDs', 'each', 'rule' => ['integer']],
]
}
}
~~~
> Note: This validator will not work with inline validation rules.
/** * @depends testValidate */ public function testAllowMessageFromRule() { $model = FakedValidationModel::createWithAttributes(['attr_one' => ['text']]); $validator = new EachValidator(['rule' => ['integer']]); $validator->allowMessageFromRule = true; $validator->validateAttribute($model, 'attr_one'); $this->assertContains('integer', $model->getFirstError('attr_one')); $model->clearErrors(); $validator->allowMessageFromRule = false; $validator->validateAttribute($model, 'attr_one'); $this->assertNotContains('integer', $model->getFirstError('attr_one')); }
/** * @return array */ public function rules() { return [[['name', 'shortName'], 'required'], ['companyId', 'exist', 'targetClass' => Company::className(), 'targetAttribute' => '_id'], ['ahuConfirmation', 'boolean'], ['ahuConfirmation', 'filter', 'filter' => function ($value) { return boolval($value); }], ['confirmPersonId', 'exist', 'targetClass' => User::className(), 'targetAttribute' => '_id'], ['supportEmailsInput', function ($attribute) { if ($this->{$attribute} === "") { $this->supportEmails = []; return; } $value = explode(';', trim($this->{$attribute}, ';')); $validator = new EachValidator(['rule' => ['email']]); if ($validator->validate($value)) { $this->supportEmails = $value; } else { $this->addError($attribute, "Неверный формат"); } }, 'skipOnEmpty' => false], ['ipAddress', 'match', 'pattern' => '/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/'], [['phone', 'contact', 'model', 'gatekeeperNumber', 'note'], 'safe'], [['companyId', 'confirmPersonId'], MongoIdValidator::className(), 'forceFormat' => 'object']]; }