传递给 Zend\Form\Form::setInputFilter() 的参数必须实现接口 InputFilterInterface,给定 null

Q_r*_*_ro 0 php forms zend-framework2

好吧,问候大家,我只是按照 zend Framework 2 示例代码创建相册管理应用程序(http://framework.zend.com/manual/2.0/en/user-guide/forms-and-actions.html)但在测试“添加”功能时遇到以下错误:

可捕获的致命错误:传递给 Zend\Form\Form::setInputFilter() 的参数 1 必须实现接口 Zend\InputFilter\InputFilterInterface,给定 null。

我一直在检查我的应用程序代码以尝试解决这个问题,但我只是看不出它出了什么问题,甚至最糟糕的是,搜索错误消息不会产生任何结果,这就是为什么我要重复您的知识尝试解决这个问题,这是我的代码:

SystemController.php

<?php


namespace System\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use System\Model\User;
use System\Form\UserRegisterForm;

Class SystemController extends AbstractActionController
{

    protected $userTable;

    public function indexAction()
    {
       return new ViewModel(array(
            'system' => $this->getUserTable()->fetchAll(),
        ));
    }

    public function editAction()
    {

    }

    public function deleteAction()
    {

    }

    public function registerAction()
    {
        $form = new UserRegisterForm();
        $form->get('submit')->setValue('Register');

        $request = $this->getRequest();
        if ($request->isPost()) {
            $user = new User();
            $form->setInputFilter($user->getInputFilter());
            $form->setData($request->getPost());

            if ($form->isValid()) {
                $user->exchangeArray($form->getData());
                $this->getUserTable()->saveUser($user);

                // Redirect to list of albums
                return $this->redirect()->toRoute('system');
            }
        }
        return array('form' => $form);
    }

    public function loginAction()
    {

    }

    public function usersAction()
    {

    }

    public function getUserTable()
    {
        if (!$this->userTable) {
            $sm = $this->getServiceLocator();
            $this->userTable = $sm->get('System\Model\UserTable');
        }
        return $this->userTable;
    }
}
Run Code Online (Sandbox Code Playgroud)

错误消息指出第 45 行给出了 null,第 455 行对应于以下代码片段:

$form->setInputFilter($user->getInputFilter());
Run Code Online (Sandbox Code Playgroud)

From 是我的 UserRegisterForm 类的一个实例

UserRegisterForm.php

<?php


namespace System\Form;

use Zend\Form\Form;

class UserRegisterForm extends Form
{
  public function __construct($name = null)
  {
      parent::__construct('user');
      $this->setAttribute('method', 'post');

      $this->add(array(
          'name' => 'id',
          'attributes' => array(
              'type'=>'hidden',
          ),
      ));

      $this->add(array(
            'name' => 'username',
            'attributes' => array(
                'type'  => 'text',
            ),
            'options' => array(
                'label' => 'Username',
            ),
        ));

      $this->add(array(
            'name' => 'first_name',
            'attributes' => array(
                'type'  => 'text',
            ),
            'options' => array(
                'label' => 'First Name',
            ),
        ));

      $this->add(array(
            'name' => 'last_name',
            'attributes' => array(
                'type'  => 'text',
            ),
            'options' => array(
                'label' => 'Last Name',
            ),
        ));

      $this->add(array(
            'name' => 'email',
            'attributes' => array(
                'type'  => 'text',
            ),
            'options' => array(
                'label' => 'E-mail',
            ),
        ));

      $this->add(array(
            'name' => 'password',
            'attributes' => array(
                'type'  => 'password',
            ),
            'options' => array(
                'label' => 'Password',
            ),
        ));

       $this->add(array(
            'name' => 'type',
            'attributes' => array(
                'type'  => 'text',
            ),
            'options' => array(
                'label' => 'Type',
            ),
        ));

      $this->add(array(
            'name' => 'submit',
            'attributes' => array(
                'type'  => 'submit',
                'value' => 'Register User',
                'id' => 'submitbutton',
            ),
        ));
  }

}
Run Code Online (Sandbox Code Playgroud)

user 是我的用户模型的一个实例

User.php
<?php



namespace System\Model;

use Zend\InputFilter\Factory as InputFactory;     
use Zend\InputFilter\InputFilter;                 
use Zend\InputFilter\InputFilterAwareInterface;   
use Zend\InputFilter\InputFilterInterface;        


class User implements InputFilterAwareInterface
{
    public $id;
    public $first_name;
    public $last_name;
    public $username;
    public $email;
    public $password;
    public $type;

    public function exchangeArray($data)
    {
        $this->id     = (isset($data['id'])) ? $data['id'] : null;
        $this->first_name = (isset($data['first_name'])) ? $data['first_name'] : null;
        $this->last_name  = (isset($data['last_name'])) ? $data['last_name'] : null;
        $this->username = (isset($data['username'])) ? $data['username'] : null;
        $this->email = (isset($data['email'])) ? $data['email'] : null;
        $this->password = (isset($data['password'])) ? $data['password'] : null;
        $this->password = (isset($data['type'])) ? $data['type'] : null;
    }

    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(array(
                'name'     => 'id',
                'required' => true,
                'filters'  => array(
                    array('name' => 'Int'),
                ),
            )));

            $inputFilter->add($factory->createInput(array(
                'name'     => 'username',
                'required' => true,
                'filters'  => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                ),
                'validators' => array(
                    array(
                        'name'    => 'StringLength',
                        'options' => array(
                            'encoding' => 'UTF-8',
                            'min'      => 3,
                            'max'      => 100,
                        ),
                    ),
                ),
            )));

            $inputFilter->add($factory->createInput(array(
                'name'     => 'first_name',
                'required' => true,
                'filters'  => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                ),
                'validators' => array(
                    array(
                        'name'    => 'StringLength',
                        'options' => array(
                            'encoding' => 'UTF-8',
                            'min'      => 1,
                            'max'      => 100,
                        ),
                    ),
                ),
            )));

            $inputFilter->add($factory->createInput(array(
                'name'     => 'last_name',
                'required' => true,
                'filters'  => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                ),
                'validators' => array(
                    array(
                        'name'    => 'StringLength',
                        'options' => array(
                            'encoding' => 'UTF-8',
                            'min'      => 1,
                            'max'      => 100,
                        ),
                    ),
                ),
            )));

            $inputFilter->add($factory->createInput(array(
                'name'     => 'email',
                'required' => true,
                'filters'  => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                ),
                'validators' => array(
                    array(
                        'name'    => 'StringLength',
                        'options' => array(
                            'encoding' => 'UTF-8',
                            'min'      => 10,
                            'max'      => 150,
                        ),
                    ),
                ),
            )));

            $inputFilter->add($factory->createInput(array(
                'name'     => 'password',
                'required' => true,
                'filters'  => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                ),
                'validators' => array(
                    array(
                        'name'    => 'StringLength',
                        'options' => array(
                            'encoding' => 'UTF-8',
                            'min'      => 6,
                            'max'      => 50,
                        ),
                    ),
                ),
            )));


        }

        return $this->inputFilter;
    }
}
Run Code Online (Sandbox Code Playgroud)

我只是看不出代码有什么问题,对我来说一切都很好,当我尝试从寄存器视图提交时,会出现此错误。但我只是不明白为什么会发生这种情况。

register.phtml

<?php

$title = 'Register a new user';
$this->headTitle($title);
?>
<h1><?php echo $this->escapeHtml($title); ?></h1>
<?php
$form = $this->form;
$form->setAttribute('action', $this->url('system', array('action' => 'register')));
$form->prepare();

/*
echo $this->formCollection($form);
 */

echo $this->form()->openTag($form);
echo $this->formHidden($form->get('id'));
echo $this->formRow($form->get('username'));
echo $this->formRow($form->get('first_name'));
echo $this->formRow($form->get('last_name'));
echo $this->formRow($form->get('email'));
echo $this->formRow($form->get('password'));
echo $this->formRow($form->get('type'));
echo $this->formSubmit($form->get('submit'));
echo $this->form()->closeTag();
Run Code Online (Sandbox Code Playgroud)

感谢任何帮助,因为我即将放弃。

Tim*_*ain 5

getInputFiler()用户模型中的方法返回,$this->inputFilter但该属性从未设置。$this->inputFilter = $inputFilter我认为您在该功能结束时错过了一项作业。