Well, greeting everybody, i was just following along the zend framework 2 example code for creating an album management app (http://framework.zend.com/manual/2.0/en/user-guide/forms-and-actions.html) but ran into the following error while testing the "add" functionallity:
Catchable fatal error: Argument 1 passed to Zend\Form\Form::setInputFilter() must implement interface Zend\InputFilter\InputFilterInterface, null given.
I have been checking my app code to try and solve this problem but i just simply dont see what's wrong with it, even worst, searching for the error message doesn't yield any results, which is why i'm recurring to your knowledge to try and solve this, here is my code:
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;
}
}
The error message point out that the null is being given on the line 45, line 455 corresponds to the following code snippet:
$form->setInputFilter($user->getInputFilter());
From is an instance of my UserRegisterForm class
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',
),
));
}
}
And user is an instance of my user model
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;
}
}
I just don't see what is wrong with the code, for me everything is fine, this error is showing up when i try and submit a from from the register view. but i just don't understand why this is happening.
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();
Any help is appreciated, 'cause i'm just about to give up.