php - Zend Framework 2: setAttribute() on a non-object error occurs when trying to edit -
i'm new php zend framework 2 , i'm going through 1 of online tutorials in learning how use it. tutorial involves having create , edit albums. problem when try edit album clicking on edit links, error stating:
call member function setattribute() on non-object
controller:
public function editaction() { $id = (int) $this->params()->fromroute('id', 0); if (!$id) { return $this->redirect()->toroute('album', array( 'action' => 'add' )); } // album specified id. exception thrown // if cannot found, in case go index page. try { $album = $this->getalbumtable()->getalbum($id); } catch (\exception $ex) { return $this->redirect()->toroute('album', array( 'action' => 'index' )); } $form = new albumform(); $form->bind($album); $form->get('submit')->setattribute('value', 'edit'); $request = $this->getrequest(); if ($request->ispost()) { $form->setinputfilter($album->getinputfilter()); $form->setdata($request->getpost()); if ($form->isvalid()) { $this->getalbumtable()->savealbum($album); // redirect list of albums return $this->redirect()->toroute('album'); } // edit: bracket below missing // causing 'setattribute' error when // clicking on edit links of albums } return array( 'id' => $id, 'form' => $form, ); } view:
<?php //module/album/view/album/album/edit.phtml: $title = 'edit album'; $this->headtitle($title); ?> <h1><?php echo $this->escapehtml($title); ?> </h1> <?php $form = $this->form; $form->setattribute('action', $this->url( 'album', array( 'action' => 'edit', 'id' => $this->id, ) )); $form->prepare(); echo $this->form()->opentag($form); echo $this->formhidden($form->get('id')); echo $this->formrow($form->get('title')); echo $this->formrow($form->get('artist')); echo $this->formsubmit($form->get('submit')); echo $this->form()->closetag(); ?> edit: here code albumform.php
albumform.php:
<?php namespace album\form; use zend\form\form; class albumform extends form { public function __construct($name = null) { // want ignore name passed parent::__construct('album'); $this->add(array( 'name' => 'id', 'type' => 'hidden', )); $this->add(array( 'name' => 'title', 'type' => 'text', 'options' => array( 'label' => 'title', ), )); $this->add(array( 'name' => 'artist', 'type' => 'text', 'options' => array( 'label' => 'artist', ), )); $this->add(array( 'name' => 'submit', 'type' => 'submit', 'attributes' => array( 'value' => 'go', 'id' => 'submitbutton', ), )); } } ?> i'm not sure why occurring since i'm following tutorial exactly, appreciated!
Comments
Post a Comment