Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Zend Framework2致命错误:对第39行C:\dir\module\Application\view\layout\layout.phtml中的非对象调用成员函数setAttribute()_Php_Zend Framework2_Zend Form - Fatal编程技术网

Php Zend Framework2致命错误:对第39行C:\dir\module\Application\view\layout\layout.phtml中的非对象调用成员函数setAttribute()

Php Zend Framework2致命错误:对第39行C:\dir\module\Application\view\layout\layout.phtml中的非对象调用成员函数setAttribute(),php,zend-framework2,zend-form,Php,Zend Framework2,Zend Form,如何解决此错误?我试图在谷歌和Zend Framework2的一些文档中找到解决方案,但我运气不好,希望能得到任何帮助。我只想在不使用数据库的情况下显示表单。请帮帮我 这是我的scheduleRequestForm.php代码 namespace Application\Form; use Zend\Captcha; use Zend\Form\Element; use Zend\Form\Form; class scheduleRequestForm extends Form {

如何解决此错误?我试图在谷歌和Zend Framework2的一些文档中找到解决方案,但我运气不好,希望能得到任何帮助。我只想在不使用数据库的情况下显示表单。请帮帮我

这是我的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', 
        ));        
    } 
}
下面是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; 
    } 
} 
这是我的视图脚本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();
?>

控制器操作旨在返回至少一个
视图模型

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; 
}
还要注意,
$this->form
变量不存在于布局中->它存在于viewmodel指向的模板文件中。然后使用
$this->content

模板文件

<?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();
?>
<html>
    <head>
        <title>Test</title>
    </head>
    <body>
        <div>
            <?php echo $this->content; ?>
        </div>
    </body>
</html>

布局文件

<?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();
?>
<html>
    <head>
        <title>Test</title>
    </head>
    <body>
        <div>
            <?php echo $this->content; ?>
        </div>
    </body>
</html>

试验
谢谢,我现在就知道了:)我把整个表单放在index.phtml模板中,然后显示出来