0
votes

I want to edit a record from a database with a Zend Form (Zend Framework 2).

In ZF1 I did in my controller:

$values = $table->getValues();
$form = new MyForm();
$form->populate($values);
$this->view->form = $form;

and in the viewscript:

<?php echo $this->form ?>

In ZF2 I tried in my controller:

$values = $table->getValues();
$form = new MyForm();
$form->populateValues($values); // form->setData($values) does not work either
return array('form' => $form);

and in my viewscript:

<?php echo $this->form()->openTag($form) ?>
<?php echo $this->formCollection($form) ?>
<?php echo $this->form()->closeTag($form) ?>

but that renders the form, without data however.

what is the correct way to do this?

2
I think you need to $form->prepare(); after $form->setData($values); - Crisp

2 Answers

0
votes

You need to call prepare() on your form before you open it in your view script. For example:

<?php $form->prepare(); ?>
<?php echo $this->form()->openTag($form) ?>
<?php echo $this->formCollection($form) ?>
<?php echo $this->form()->closeTag($form) ?>

See the reference guide here

Note the following point:

the prepare() method. You must call it prior to rendering anything in the view (this function is only meant to be called in views, not in controllers).

0
votes

The issue was that $table->getValues() in my code returns an arrayObject whereas populateValues() expects an array.