# Form input values out of synch with Form context object

**URL:** <https://discourse.cakephp.org/t/form-input-values-out-of-synch-with-form-context-object/8377>\
**Category:** Need Help\
**Created:** [September 14, 2020, 6:13am UTC](https://discourse.cakephp.org/t/form-input-values-out-of-synch-with-form-context-object/8377 "2020-09-14T06:13:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dreamingmind](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dreamingmind/32/1857_2.png) [@dreamingmind](https://discourse.cakephp.org/u/dreamingmind)\
**Post date:** [September 14, 2020, 6:13am UTC](https://discourse.cakephp.org/t/form-input-values-out-of-synch-with-form-context-object/8377/1 "2020-09-14T06:13:39Z")

</div>

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:

 ![Screen Shot 2020-09-13 at 10.42.48 PM](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/1/1e070cfd0614bd40b531129054fdd3af888861a9.png)

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:

 ![Screen Shot 2020-09-13 at 10.51.18 PM](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/9/92e4aa14dcfb642c6c0b381c92d82c6aaa114b91.png)

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:

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

```

That creates form tags that look like this:

 ![Screen Shot 2020-09-13 at 11.01.21 PM](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/3/36d7eab9d21dc8a9bd240ac72da652414e966106.png)

A typical input looks like this:

 ![Screen Shot 2020-09-13 at 11.03.47 PM](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/1/1803dcb1db1d664d4acff6612eed71da53daf809.png)

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:

```php
    <?= $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?

---

<div class="post-metadata">

**Author:** ![dreamingmind](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dreamingmind/32/1857_2.png) [@dreamingmind](https://discourse.cakephp.org/u/dreamingmind)\
**Post date:** [September 14, 2020, 4:34pm UTC](https://discourse.cakephp.org/t/form-input-values-out-of-synch-with-form-context-object/8377/2 "2020-09-14T16:34:52Z")

</div>

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.

```php
// 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

```

```php
// 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

```
