0

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

here's my code of scheduleRequestForm.php

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->setAttribute('method', 'post'); 

        $this->add(array( 
            'name' => 'firstname', 
            'type' => 'Zend\Form\Element\Text', 
            'attributes' => array( 
                'placeholder' => 'Type something...', 
                'required' => 'required', 
            ), 
            'options' => array( 
                'label' => 'First name', 
            ), 
        )); 

        $this->add(array( 
            'name' => 'lastname', 
            'type' => 'Zend\Form\Element\Text', 
            'attributes' => array( 
                'placeholder' => 'Type something...', 
                'required' => 'required', 
            ), 
            'options' => array( 
                'label' => 'Last Name', 
            ), 
        )); 

        $this->add(array( 
            'name' => 'csrf', 
            'type' => 'Zend\Form\Element\Csrf', 
        ));        
    } 
}

here's the Validator.php

<?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->inputFilter) 
        { 
            $inputFilter = new InputFilter(); 
            $factory = new InputFactory(); 


        $inputFilter->add($factory->createInput([ 
            'name' => 'firstname', 
            'required' => true, 
            'filters' => array( 
                array('name' => 'StripTags'), 
                array('name' => 'StringTrim'), 
            ), 
            'validators' => array( 
            ), 
        ])); 

        $inputFilter->add($factory->createInput([ 
            'name' => 'lastname', 
            'required' => true, 
            'filters' => array( 
                array('name' => 'StripTags'), 
                array('name' => 'StringTrim'), 
            ), 
            'validators' => array( 
            ), 
        ])); 

            $this->inputFilter = $inputFilter; 
        } 

        return $this->inputFilter; 
    } 
} 

Here's my IndexController.php

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->getRequest(); 

        if($request->isPost()) 
        { 
            $user = new linel(); 

            $formValidator = new scheduleRequestFormValidator(); 
            { 
                $form->setInputFilter($formValidator->getInputFilter()); 
                $form->setData($request->getPost()); 
            } 

            if($form->isValid()){ 
            { 
                $user->exchangeArray($form->getData()); 
            } 
        } 

        return ['form' => $form]; 
    } 
}
}

Here's my view script layout.phtml

<?php
$form = $this->form;
$form->setAttribute('action', $this->url('home', array('action'=>'add')));

echo $this->formLabel($form->get('firstname'));
echo $this->formElement($form->get('firstname'));
echo $this->formElementErrors($form->get('firstname'));
echo $this->formLabel($form->get('lastname'));
echo $this->formElement($form->get('lastname'));
echo $this->formElementErrors($form->get('lastname'));
echo $this->form()->closeTag();
?>
tereško
  • 58,060
  • 25
  • 98
  • 150
Linel Sedillo
  • 35
  • 1
  • 9

1 Answers1

1

Controller Actions are designed to return at least a ViewModel

public function indexAction() 
{ 
    // [...] your other stuff

    $viewModel = new \Zend\View\Model\ViewModel();
    $viewModel->setVariable('form', $form);
    $viewModel->setTemplate('/path/to/viewmodel/template/file.phtml');

    return $viewModel; 
}

also notice that your $this->form variable is not present in your layout -> it is present in template file where your viewmodel is point to. this view template is then injected into your layout with $this->content

template file

<?php
    $form = $this->form;
    $form->setAttribute('action', $this->url('home', array('action'=>'add')));

    echo $this->formLabel($form->get('firstname'));
    echo $this->formElement($form->get('firstname'));
    echo $this->formElementErrors($form->get('firstname'));
    echo $this->formLabel($form->get('lastname'));
    echo $this->formElement($form->get('lastname'));
    echo $this->formElementErrors($form->get('lastname'));
    echo $this->form()->closeTag();
?>

layout file

<html>
    <head>
        <title>Test</title>
    </head>
    <body>
        <div>
            <?php echo $this->content; ?>
        </div>
    </body>
</html>
ins0
  • 3,918
  • 1
  • 20
  • 28