2014-01-22 43 views
0

欢迎大家,我只是沿着zend framework 2示例代码创建相册管理应用程序(http://framework.zend.com/manual/2.0/en/user-guide/forms-and-actions.html),但运行进入下面的错误,而测试的“添加” functionallity:传递给Zend Form Form :: setInputFilter()的参数必须实现接口InputFilterInterface,null给出

Catchable fatal error: Argument 1 passed to Zend\Form\Form::setInputFilter() must implement interface Zend\InputFilter\InputFilterInterface, null given.

我一直在检查我的应用程序代码,试图解决这个问题,但我只是单纯的看不出有什么不对的地方,甚至是最糟糕的,搜索错误信息不会产生任何结果,这就是为什么我经常性地向你的知识尝试解决这个问题,这里是我的代码:

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

该错误消息指出所述空报错就行45,行455对应于下面的代码片断:

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

从我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', 
      ), 
     )); 
    } 

} 

而且用户是我的用户模型

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

我只是做的一个实例没有看到代码有什么问题,对我来说一切都很好,当我尝试从注册视图中提交一个来自这个错误的时候。但我不明白为什么会发生这种情况。

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

任何帮助表示赞赏,因为我即将放弃。

回答

3

用户模型中的getInputFiler()方法返回$this->inputFilter,但该属性从不设置。我认为你错过了一个$this->inputFilter = $inputFilter分配到该函数的结尾。

+0

非常感谢,你在哪里完全正确,我只是,不知道我怎么会如此盲目,以至于在发布之前没有看到这个。 –

1

在你user.php的变化

return $this->inputFilter; 

return $inputFilter 

$this->inputFilter总是空。正如蒂姆喷泉提到它,它从未设置。

+0

你是对的,但蒂姆碰到了钉子,我是如此愚蠢,我讨厌这种事情发生时,就像我盲目地看着我的代码,这么讨厌,现在我觉得自己像个白痴问这个问题,谢谢你的帮助 –

1

回答上面

给出..但尽管如此,我建议创建一个单独的输入过滤器类,并没有在模型中定义它。如果您有超过1个模型对象,则每个模型实例将在其中具有一个InputFilter定义。

+0

好的建议,会看更多。 –

相关问题