CakePHP3: cannot display a virtual field in a Form

192 Views Asked by At

In a beforeMarshall(), I create an additionnal field in my entity. In my controller, I debug($myEntity) just before assigning the data to the view, and I see my field. I even created a getter like follow:

protected function _getTmpImage() {

    if (isset($this->_properties['tmp_image'])) {
        debug($this->_properties['tmp_image']);
        return $this->_properties['tmp_image'];
    } else {
        return null;
    }
}

and In my view, I see the corresponding debug() display.

But the follwing line:

<?= $this->Form->hidden('agpoi_images.'.$key.'.tmp_image'); ?>

desesperately creates the following html code:

<input name="agpoi_images[0][tmp_image]" class="form-control" value="" type="hidden">

Can you tell me why?

PS: I also tried to add the following statement in my entity class, even if I think I don't have to do that according to the doc

class MyEntity extends Entity
{

    protected $_virtual = ['tmp_image'];

}
1

There are 1 best solutions below

7
On

In view file, when using form elements, the $fieldname must be of the form ‘Modelname.fieldname’ which makes cakephp easy to substitute controller data and to send the post request in such format.

In your case, you can create the hidden form field in the same way $this->Form->hidden(‘MyEntity.tmp_image) which will be displayed as

<input name=“MyEntity[tmp_image]" class="form-control" value=“<tmp_image_value>" type="hidden">