title: Zend Framework2 Fatal error: Call to a member function setAttribute() on a non-object in C:\dir\module\Application\view\layout\layout.phtml on line 39
body: <p>How can i resolve this error? i tried to find solution in google and some docs of Zend Framework2 but i don't have luck, any help i would appreciate. i just want to display the form without using database. Please help me</p>
  
  <p>here's my code of scheduleRequestForm.php</p>
  
  <pre><code>namespace Application\Form; 
  
  use Zend\Captcha; 
  use Zend\Form\Element; 
  use Zend\Form\Form; 
  
  class scheduleRequestForm extends Form 
  { 
      public function __construct() /*$name = null*/
      { 
          parent::__construct('application\form'); 
  
          $this-&gt;setAttribute('method', 'post'); 
  
          $this-&gt;add(array( 
              'name' =&gt; 'firstname', 
              'type' =&gt; 'Zend\Form\Element\Text', 
              'attributes' =&gt; array( 
                  'placeholder' =&gt; 'Type something...', 
                  'required' =&gt; 'required', 
              ), 
              'options' =&gt; array( 
                  'label' =&gt; 'First name', 
              ), 
          )); 
  
          $this-&gt;add(array( 
              'name' =&gt; 'lastname', 
              'type' =&gt; 'Zend\Form\Element\Text', 
              'attributes' =&gt; array( 
                  'placeholder' =&gt; 'Type something...', 
                  'required' =&gt; 'required', 
              ), 
              'options' =&gt; array( 
                  'label' =&gt; 'Last Name', 
              ), 
          )); 
  
          $this-&gt;add(array( 
              'name' =&gt; 'csrf', 
              'type' =&gt; 'Zend\Form\Element\Csrf', 
          ));        
      } 
  }
  </code></pre>
  
  <p>here's the Validator.php</p>
  
  <pre><code>&lt;?php
  namespace Application\Form; 
  
  use Zend\InputFilter\Factory as InputFactory; 
  use Zend\InputFilter\InputFilter; 
  use Zend\InputFilter\InputFilterAwareInterface; 
  use Zend\InputFilter\InputFilterInterface; 
  
  class scheduleRequestFormValidator implements InputFilterAwareInterface 
  { 
      protected $inputFilter; 
  
      public function setInputFilter(InputFilterInterface $inputFilter) 
      { 
          throw new \Exception("Not used"); 
      } 
  
      public function getInputFilter() 
      { 
          if (!$this-&gt;inputFilter) 
          { 
              $inputFilter = new InputFilter(); 
              $factory = new InputFactory(); 
  
  
          $inputFilter-&gt;add($factory-&gt;createInput([ 
              'name' =&gt; 'firstname', 
              'required' =&gt; true, 
              'filters' =&gt; array( 
                  array('name' =&gt; 'StripTags'), 
                  array('name' =&gt; 'StringTrim'), 
              ), 
              'validators' =&gt; array( 
              ), 
          ])); 
  
          $inputFilter-&gt;add($factory-&gt;createInput([ 
              'name' =&gt; 'lastname', 
              'required' =&gt; true, 
              'filters' =&gt; array( 
                  array('name' =&gt; 'StripTags'), 
                  array('name' =&gt; 'StringTrim'), 
              ), 
              'validators' =&gt; array( 
              ), 
          ])); 
  
              $this-&gt;inputFilter = $inputFilter; 
          } 
  
          return $this-&gt;inputFilter; 
      } 
  } 
  </code></pre>
  
  <p>Here's my IndexController.php</p>
  
  <pre><code>namespace Application\Controller;
  
  use Zend\Mvc\Controller\AbstractActionController;
  use Zend\View\Model\ViewModel;
  use Application\Form\ScheduleRequestForm;
  use Application\Form\Form\Validator; 
  use Application\Form\Model\linel; 
  
  class IndexController extends AbstractActionController
  {
  
      public function indexAction() 
      { 
          $form = new scheduleRequestForm(); 
          $request = $this-&gt;getRequest(); 
  
          if($request-&gt;isPost()) 
          { 
              $user = new linel(); 
  
              $formValidator = new scheduleRequestFormValidator(); 
              { 
                  $form-&gt;setInputFilter($formValidator-&gt;getInputFilter()); 
                  $form-&gt;setData($request-&gt;getPost()); 
              } 
  
              if($form-&gt;isValid()){ 
              { 
                  $user-&gt;exchangeArray($form-&gt;getData()); 
              } 
          } 
  
          return ['form' =&gt; $form]; 
      } 
  }
  }
  </code></pre>
  
  <p>Here's my view script layout.phtml</p>
  
  <pre><code>&lt;?php
  $form = $this-&gt;form;
  $form-&gt;setAttribute('action', $this-&gt;url('home', array('action'=&gt;'add')));
  
  echo $this-&gt;formLabel($form-&gt;get('firstname'));
  echo $this-&gt;formElement($form-&gt;get('firstname'));
  echo $this-&gt;formElementErrors($form-&gt;get('firstname'));
  echo $this-&gt;formLabel($form-&gt;get('lastname'));
  echo $this-&gt;formElement($form-&gt;get('lastname'));
  echo $this-&gt;formElementErrors($form-&gt;get('lastname'));
  echo $this-&gt;form()-&gt;closeTag();
  ?&gt;
  </code></pre>