How to otvalivalas field in yii2?

Good day!
I have a form with multiple select

I added [] to my server, came an array of IDs of the cities of this array correctly ofvalidity, if you specify just cities will come to one value, otherwise yii complains it can't find such a field cities[].

[['cities'], 'each', 'rule' => ['required','integer']],

I need to have only type int, and the field was not empty
March 20th 20 at 11:50
2 answers
March 20th 20 at 11:52
Solution
['cities', function ($attribute, $params, $validator) {
 if(is_array($this->$attribute)) {
 if(empty($this->$attribute)) {
 $this->addError($attribute, 'The array should not be empty');
}
 foreach($this->$attribute as $item) {
 if(!is_int($item)) { 
 $this->addError($attribute, 'The array contains values not an integer');
 break; 
}
 } 
}
}],
Thank you, just a little modified added 'skipOnEmpty' => false instead of is_int and ctype_digit use
public function rules()
{
 return [
 ['cities', 'validateArrayRequired', 'skipOnEmpty' => false],
];
}

 public function validateArrayRequiredInteger($attribute, $params, $validator) {
 if(is_array($this->$attribute)) {
 if(empty($this->$attribute)) {
 $this->addError($attribute, "Select {$attribute}");
}
 foreach($this->$attribute as $item) {
 if(!ctype_digit($item)) {
 $this->addError($attribute, 'Data {$attribute} doesn't contain numbers');
break;
}
}
 } else {
 $this->addError($attribute, "Select {$attribute}");
}
 }
- margot.Koelpin37 commented on March 20th 20 at 11:55
March 20th 20 at 11:54
[
 ['cities', 'each', 'rule' => ['required']],
 ['cities', 'each', 'rule' => ['integer']],
]

https://github.com/yiisoft/yii2/blob/master/docs/g...

otherwise yii complains it can't find such a field cities[]

should not, show the form

Find more questions by tags Yii