参数传递给zend form form :: setInputfilter()必须实现接口inputfilter



好吧,问候大家,我只是沿着Zend Framework 2示例代码创建专辑管理应用程序(http://framework.zend.zend.com/manual/manual/2.0/en/用户指南/forms-and-actions.html),但在测试" add"功能方面遇到了以下错误:

可捕获的致命错误:参数1传递给 zend form form :: setInputfilter()必须实现接口 Zend InputFilter InputFilterInterface,null给出。

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

SystemController.php
<?php

namespace SystemController;
use ZendMvcControllerAbstractActionController;
use ZendViewModelViewModel;
use SystemModelUser;
use SystemFormUserRegisterForm;
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('SystemModelUserTable');
        }
        return $this->userTable;
    }
}

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

$form->setInputFilter($user->getInputFilter());

来自我的UserRegisterform类的实例

UserRegisterForm.php
<?php

namespace SystemForm;
use ZendFormForm;
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',
            ),
        ));
  }
}

,用户是我用户模型的实例

User.php
<?php

namespace SystemModel;
use ZendInputFilterFactory as InputFactory;     
use ZendInputFilterInputFilter;                 
use ZendInputFilterInputFilterAwareInterface;   
use ZendInputFilterInputFilterInterface;        

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;
    }
}

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

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

任何帮助。

用户模型中的getInputFiler()方法返回$this->inputFilter,但是该属性永远不会设置。我认为您在该功能结束时缺少$this->inputFilter = $inputFilter分配。

在您的user.php更改中

return $this->inputFilter; 

to

return $inputFilter

您的$this->inputFilter始终为无效。正如Tim Fountain提到的那样,它永远不会设置。

答案在上面给出

..但是,我建议创建一个单独的输入式类别,并且不要在模型中定义它。如果您有一个以上的模型对象,则每个模型实例将具有一个输入式定义。

最新更新