好吧,问候每个人,我只是按照zend框架2示例代码创建专辑管理应用程序(http://framework.zend.com/manual/2.0/en/user-guide/forms-and-actions.html),但在测试“添加”功能时遇到以下错误:
可捕获的致命错误:参数1传递给 Zend \ Form \ Form :: setInputFilter()必须实现接口 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;
}
}
错误消息指出在第45行上给出了null,第455行对应于以下代码片段:
$form->setInputFilter($user->getInputFilter());
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',
),
));
}
}
用户是我的用户模型的实例
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();
感谢任何帮助,因为我正要放弃。
答案 0 :(得分:3)
用户模型中的getInputFiler()
方法返回$this->inputFilter
,但该属性永远不会设置。我认为你在该功能结束时错过了$this->inputFilter = $inputFilter
任务。
答案 1 :(得分:1)
在您的User.php更改
中return $this->inputFilter;
到
return $inputFilter
您的$this->inputFilter
始终为空。正如Tim Fountain所提到的那样,它从未被设定过。
答案 2 :(得分:1)
上面给出了答案
..但是我建议创建一个单独的InputFilter类,不要在模型中定义它。如果您有多个模型对象,则每个模型实例内部都将包含一个InputFilter定义。