Skip to content

Instantly share code, notes, and snippets.

@dmitrybelyakov
Created March 6, 2014 07:58
Show Gist options
  • Save dmitrybelyakov/9384493 to your computer and use it in GitHub Desktop.
Save dmitrybelyakov/9384493 to your computer and use it in GitHub Desktop.
Bind entity to form with fieldsets
<?php
/**
* Example action
* This is an example action that tests binding of forms to entities.
* @return \Zend\View\Model\ViewModel
*/
public function exampleAction()
{
$node = new ArrayObject(array(
'id' => 9,
'locale' => 'en_US',
'status' => 'published',
'title' => 'Test',
'slug' => 'test-item',
));
$form = new \Zend\Form\Form('example');
//meta
$meta = new \Zend\Form\Fieldset('Meta');
$meta->add(new \Zend\Form\Element\Text('title', ['label' => 'Title']));
$form->add($meta);
//general
$general = new \Zend\Form\Fieldset('General');
$general->add(new \Zend\Form\Element\Text('slug', ['label' => 'Slug']));
$form->add($general);
$form->bind($node);
$response['form'] = $form;
$vm = new ViewModel(array('form' => $form));
$vm->setTemplate('example');
return $vm;
}
<?php $this->form->prepare(); ?>
<?= $this->form()->openTag($this->form) ?>
<?php foreach ($this->form as $elementOrFieldset): ?>
<?php if ($elementOrFieldset instanceof Zend\Form\Fieldset): ?>
<?= $this->formCollection($elementOrFieldset) ?>
<?php else: ?>
<?= $this->formRow($elementOrFieldset) ?>
<?php endif; ?>
<?php endforeach; ?>
<?= $this->form()->closeTag() ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment