Answer the question
In order to leave comments, you need to log in
[[+content_image]]
Is this validation rule ok?
Models usually have single-level data. Difficulties arise on several levels.
Is this method of validation normal?
<?php
class Items
{
public function rules()
{
return [
[['items'], 'checkItem']
];
}
protected function checkItem()
{
$status = (is_array($this->items) || is_object($this->items)) && $this->items;
if ($status) {
foreach ($this->items as $item) {
$itemModel = new Item;
$status = ($itemModel->load($item, '') && $itemModel->validate());
if (!$status) {
break;
}
}
}
return $status;
}
}
Answer the question
In order to leave comments, you need to log in
1. Instead
public function rules()
{
return [
['items'], 'checkItem'
];
}
public function rules()
{
return [
[['items'], 'checkItem']
];
}
foreach ($this->items as $item) {
$item = new Item;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question