array('question','type','type'=>'array','allowEmpty'=>false),
你可以验证你是否收到了正确的数组,但你不知道这个数组里面有什么。要验证数组元素,您应该执行以下操作:
<?php
class TestForm extends CFormModel
{
public $ids;
public function rules()
{
return [
['ids', 'arrayOfInt', 'allowEmpty' => false],
];
}
public function arrayOfInt($attributeName, $params)
{
$allowEmpty = false;
if (isset($params['allowEmpty']) and is_bool($params['allowEmpty'])) {
$allowEmpty = $params['allowEmpty'];
}
if (!is_array($this->$attributeName)) {
$this->addError($attributeName, "$attributeName must be array.");
}
if (empty($this->$attributeName) and !$allowEmpty) {
$this->addError($attributeName, "$attributeName cannot be empty array.");
}
foreach ($this->$attributeName as $key => $value) {
if (!is_int($value)) {
$this->addError($attributeName, "$attributeName contains invalid value: $value.");
}
}
}
}