Form object to view

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Form object to view

poashoas
This post has NOT been accepted by the mailing list yet.
hi there,

I have a script that gets and adds to the viewmodel:

  $viewModel = $event->getViewModel();
  $viewModel->setVariables($arrViewModel);

$arrViewModel contains an array('myForm' => $returnedFormFromFunctionBelow).
How do I render the form "ready-set-go" for the template?

  public function textForm ($textId, $setText)
  {
    $form = new \Zend\Form\Form('textForm');

    $form->add(array(
      'name' => 'textid',
      'attributes' => array(
        'type' => 'hidden',
        'value' => $textId,
      ),
      'filters' => array(
         array('StringTrim'),
      ),
    ))
    ->add(array(
      'name' => 'textinput',
      'attributes' => array(
        'type' => 'textarea',
        'value' => $setText,
      ),
      'filters' => array(
        array('StringTrim'),
      ),
      'options' => array(
        'label'    => 'Tekst'
      )
    ))
    ->add(array(
      'type' => 'Zend\Form\Element\Csrf',
      'name' => 'csrf'
    ))
    ->add(array(
      'name' => 'submit',
      'attributes' => array(
        'type' => 'submit',
        'value' => 'Save'
      ),
    ));

    return $form;
  }

When I do <? echo $myForm ?> I get:
Object of class Zend\Form\Form could not be converted to string.
luk
Reply | Threaded
Open this post in threaded view
|

Re: Form object to view

luk
poashoas wrote
hi there,

I have a script that gets and adds to the viewmodel:

  $viewModel = $event->getViewModel();
  $viewModel->setVariables($arrViewModel);

$arrViewModel contains an array('myForm' => $returnedFormFromFunctionBelow).
How do I render the form "ready-set-go" for the template?

  public function textForm ($textId, $setText)
  {
    $form = new \Zend\Form\Form('textForm');

    $form->add(array(
      'name' => 'textid',
      'attributes' => array(
        'type' => 'hidden',
        'value' => $textId,
      ),
      'filters' => array(
         array('StringTrim'),
      ),
    ))
    ->add(array(
      'name' => 'textinput',
      'attributes' => array(
        'type' => 'textarea',
        'value' => $setText,
      ),
      'filters' => array(
        array('StringTrim'),
      ),
      'options' => array(
        'label'    => 'Tekst'
      )
    ))
    ->add(array(
      'type' => 'Zend\Form\Element\Csrf',
      'name' => 'csrf'
    ))
    ->add(array(
      'name' => 'submit',
      'attributes' => array(
        'type' => 'submit',
        'value' => 'Save'
      ),
    ));

    return $form;
  }

When I do <? echo $myForm ?> I get:
Object of class Zend\Form\Form could not be converted to string.
You would need to use form view helpers to render the form, like:

$form = $this->myForm;
$form->setAttribute('action', $this->url('module', array('action' => 'add')));
$form->prepare();
echo $this->form()->openTag($form);
echo $this->formHidden($form->get('id'));
echo $this->formRow($form->get('name'));
echo $this->form()->closeTag();

In case you want to use only <?php echo $myForm;?> you would need to implement in your Form class __toString method and use form view helpers to generate content or build your own form helper to achieve the same thing.
Cheers, -- Luke Mierzwa