CakePHP 3 - How to automatically show form errors for several new objects?

I am trying to use any automatic error checking and display processing for a form using multiple objects.

The user can dynamically create several new objects through the UI form. Data is routed through newEntities():

$this->MyModel->newEntities($data);

The first part of the problem that I have is that in order to check whether it was not possible to check on any of the entities, I have to do this manually, checking each object:

$errors = false;
foreach ($entities as $entity) {
    if ($entity->errors()) {
        $errors = true;
        break;
    }
}
if (!$errors) {
    // Save...

Does Cake provide anything out of the box that allows you newEntities()to check if you could not verify on any of your entities? If not, then it doesn’t matter ...

, , inline .

<?= $this->Form->create(); ?>

create(), ? , , , , , .

, $i , .

$this->Form->hidden("MyModel.$i.field");
+4
2

$this->Form->create($entities);

.

$this->Form->hidden("$i.field");

$this->Form->hidden("MyModel.$i.field");
+2

newEntity . , ().

$array = [];
$array[] = $TableRegistry->newEntity([
    'hi' => 'hey'
]);

foreach($array as $r){
    var_dump($r->errors());
}

, . newEntities, ?

+1

Source: https://habr.com/ru/post/1654511/


All Articles