Form input values out of synch with Form context object

I have a page with multiple forms on it. Each form renders data for a different entity (and associated data). Here is what a sample page looks like when rendered:

Of special note for understanding my problem:

  • the left column shows different related data for each form
  • in the right column, the ‘Expected’ value (in the label) matches the ‘Actual’ value (the input’s value).

The Problem

When I submit one of the forms on the page, that form’s data will be submitted, processed, and the page will be re-rendered for further work on other forms.

However, when the page re-renders, the data from the form that was processed is shown in every form… but only in the right-hand column. This is data from the first-level entity. The associated data is correct.

Here is the page after submitting the second form:

Of special note in this view:

  • the associated data is rendered correctly
  • the labels on the right show correct data but the input values are incorrect

Other details and more insight into the problem

  • Each of the forms has a unique id based on the id of the record it displays
  • Every input has a unique id based on the id of the first-level entity for that form and the field name

These form features are created in this way:

<?= $this->Form->create(
    $restock_line,
    ['idPrefix' => $restock_line->id, 'id' => "form-$restock_line->id"]
); ?>

That creates form tags that look like this:

A typical input looks like this:

In fact, you can see in the first example input that the id-attribute does not match the value-attribute although they are both being rendered from the same entity object!

Here is the code that is generating the two inputs illustrated above:

    <?= $this->Form->control('id', ['type' => 'hidden']); ?>
    <?= $this->Form->control('status', ['type' => 'hidden']); ?>

WTF

Does anyone have any clue what might be going on?

I would look more at the logical flow of my code if it weren’t for the ample evidence in the labels and id-attributes that the proper data is available.

Is it time for an issue?

I found a solution but don’t understand the reason.

When a form is processed, rather than allowing the process to flow through the action and re-render the form, I have to redirect to the action after processing the form.

// this is the way that did not work

// restocks/view method
// some action code here
if ($this->request->is(['patch', 'post', 'put'])) {
    try {
        $this->shelveLine($restock_line);
    } catch (\Exception $e) {
        $this->Flash->set($e->getMessage());
    }
    Cache::delete($this->StackTable->cacheKey($id), $this->StackTable->cacheName());
}
// more action code here
// render the page here
// this is solved the problem

// restocks/view method
// some action code here
if ($this->request->is(['patch', 'post', 'put'])) {
    try {
        $this->shelveLine($restock_line);
    } catch (\Exception $e) {
        $this->Flash->set($e->getMessage());
    }
    Cache::delete($this->StackTable->cacheKey($id), $this->StackTable->cacheName());
    
    // this is the critical change
    return $this->redirect(['action' => 'view', $id]);
}
// more action code here
// render the page here