The validator in my zend framework 2 are not working. Even as you can see required=>true works and shows me error if I pass an empty string. But the min and max length validations are not working and $form->isValid() just return true. Any help will be appreciated.
I'm following this tutorial on Zend website. http://framework.zend.com/manual/2.3/en/user-guide/forms-and-actions.html
<?php
namespace AdminAuthentication\Model;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
use Zend\InputFilter\Factory as InputFactory;
class Login implements InputFilterAwareInterface{
public $username;
public $password;
protected $inputFilter;
public function exchangeArray($data){
$this->username = isset($data['username']) ? $data['username'] : null;
$this->password = isset($data['password']) ? $data['password'] : 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' => 'username',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim' ),
),
'validators' => array(
array('name' => 'StringLength',
'encoding' => 'UTF-8',
'min' => 5,
'max' => 35
)
)
)));
$inputFilter->add($factory->createInput(array(
'name' => 'password',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim' ),
),
'validators' => array(
array('name' => 'StringLength',
'encoding' => 'UTF-8',
'min' => 5
)
)
)));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
}
I think you missed something in the documentation tutorial. Your code is wrong.
You have :
'validators' => array(
array('name' => 'StringLength',
'encoding' => 'UTF-8',
'min' => 5,
'max' => 35
)
)
You should have this :
'validators' => array (
array (
'name' => 'StringLength',
'options' => array (//<-----options array here
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100
)
)
)
The min, max and encoding should be in the options array.
Well, above code was in documentation and not working. I don't know the reason but I've found the solution. For validator, replace
'validators' => array(
array('name' => 'StringLength',
'encoding' => 'UTF-8',
'min' => 5
)
)
with
'validators' => array(
new StringLength(
array(
'encoding' => 'UTF-8',
'min' => 5
)
),
),
Here is the complete code.
namespace AdminAuthentication\Model;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
use Zend\InputFilter\Factory as InputFactory;
use Zend\Validator\StringLength;
class Login implements InputFilterAwareInterface
{
public $username;
public $password;
protected $inputFilter;
public function exchangeArray($data)
{
$this->username = isset($data['username']) ? $data['username'] : null;
$this->password = isset($data['password']) ? $data['password'] : 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' => 'username',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
new StringLength(
array(
'encoding' => 'UTF-8',
'min' => 5,
'max' => 35
)
),
),
)));
$inputFilter->add($factory->createInput(array(
'name' => 'password',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
new StringLength(
array(
'encoding' => 'UTF-8',
'min' => 5
)
),
),
)));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
}
Related
I have the next code in a model:
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' => 'query',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 256,
),
),
),
)));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
What I have to add, to the code become with error messages in my language ( non english)? (Errors : "required", "StringLength")
Will have seen an information about the validator:
Zend 2.1 Validator
My zend framework 2 project concerns an online restaurant Menu and something is wrong with my code because I am having this error:
Fatal error: Interface 'Pizza\Model\InputFilterAwarelnterface' not found
C:\wamp\www\pizza\module\Pizza\src\Pizza\Model\pizza.php on line 9
Please help me to find what's wrong with my code.
Here is pizza.php file:
<?php
namespace Pizza\Model;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterInterface;
use Zend\InputFilter\InputFilterAwareInterface;
class Pizza implements InputFilterAwarelnterface
{
public $id;
public $name;
public $ingredients;
public $smallprice;
public $bigprice;
public $familyprice;
public $partyprice;
protected $inputFilter;
public function __construct()
{
}
public function exchangeArray($data)
{
$this->id = (!empty($data['id'])) ? $data['id'] :null;
$this->name = (!empty($data['name'])) ? $data['name'] :null;
$this->ingredients = (!empty($data['ingredients'])) ? $data['ingredients'] :null;
$this->smallprice = (!empty($data['smallprice'])) ? $data['smallprice'] :null;
$this->bigprice = (!empty($data['bigprice'])) ? $data['bigprice'] :null;
$this->familyprice = (!empty($data['familyprice'])) ? $data['familyprice'] :null;
$this->partyprice = (!empty($data['partyprice'])) ? $data['partyprice'] :null;
}
public function getArrayCopy()
{
return get_object_vars($this);
}
public function setInputFilter(InputFilterInterface $inputfilter)
{
throw new \Exception("not used");
}
public function getInpunFilter()
{
if(!$this->inputFliter)
{
$inputfilter = new InputFilter();
$inputfilter->add(array('name' => 'name',
'required' => true,
'filters' => array('name' => 'StringTrim'),
array('name' => 'StringTags'),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 5,
'max' =>30,),
))
)
);
$inputfilter->add(array('name' => 'ingredients',
'required' => true,
'filters' => array('name' => 'StringTrim'),
array('name' => 'StringTags'),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 5,
'max' =>255,),
))
)
);
$inputfilter->add(array('name' => 'smallprice',
'required' => true,
'filters' => array('name' => 'StringTrim'),
array('name' => 'StringTags'),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 5,),
),
array(
'name' => 'float',
'options' => array(
'locale' => 'en_us')
)
)
)
);
$inputfilter->add(array('name' => 'bigprice',
'required' => true,
'filters' => array('name' => 'StringTrim'),
array('name' => 'StringTags'),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 5,),
),
array(
'name' => 'float',
'options' => array(
'locale' => 'en_us')
)
)
)
);
$inputfilter->add(array('name' => 'familyprice',
'required' => true,
'filters' => array('name' => 'StringTrim'),
array('name' => 'StringTags'),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 5,),
),
array(
'name' => 'float',
'options' => array(
'locale' => 'en_us')
)
)
)
);
$inputfilter->add(array('name' => 'partyprice',
'required' => true,
'filters' => array('name' => 'StringTrim'),
array('name' => 'StringTags'),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 5,),
),
array(
'name' => 'float',
'options' => array(
'locale' => 'en_us')
)
)
)
);
return $this->inputFilter;
}
return $this->inputFilter;
}
}
Simple typo:
class Pizza implements InputFilterAwarelnterface
^
This character should be an 'I', not a 'l'
My zend framework 2 project concerns an online restaurant Menu and something is wrong with my code because I am having this error:
Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW) in
C:\wamp\www\pizza\module\Pizza\src\Pizza\Model\pizza.php on line 57
Please help me to find what's wrong with my code. Here is pizza.php file:
<?php
namespace Pizza\Model;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterInterface;
use Zend\InputFilter\InputFilterAwareInterface;
class Pizza implements InputFilterAwareInterface
{
public $id;
public $name;
public $ingredients;
public $smallprice;
public $bigprice;
public $familyprice;
public $partyprice;
protected $inputFilter;
public function __construct()
{
}
public function exchangeArray($data)
{
$this->id = (!empty($data['id'])) ? $data['id'] :null;
$this->name = (!empty($data['name'])) ? $data['name'] :null;
$this->ingredients = (!empty($data['ingredients'])) ? $data['ingredients'] :null;
$this->smallprice = (!empty($data['smallprice'])) ? $data['smallprice'] :null;
$this->bigprice = (!empty($data['bigprice'])) ? $data['bigprice'] :null;
$this->familyprice = (!empty($data['familyprice'])) ? $data['familyprice'] :null;
$this->partyprice = (!empty($data['partyprice'])) ? $data['partyprice'] :null;
}
public function getArrayCopy()
{
return get_object_vars($this);
}
public function setInputFilter(InputFilterInterface $inputfilter)
{
throw new \Exception("not used");
}
public function getInputFilter()
{
if(!$this->inputFilter)
{
$inputfilter = new InputFilter();
$inputfilter->add(array('name' => 'name',
'required' => true,
'filters' => array('name' => 'StringTrim'),
array('name' => 'StringTags')),
'validators' => array( //line 57
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 5,
'max' =>30,)
))
)
);
$inputfilter->add(array('name' => 'ingredients',
'required' => true,
'filters' => array('name' => 'StringTrim'),
array('name' => 'StringTags')),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 5,
'max' =>255,)
))
)
);
$inputfilter->add(array('name' => 'smallprice',
'required' => true,
'filters' => array('name' => 'StringTrim'),
array('name' => 'StringTags')),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 5,),
),
array(
'name' => 'float',
'options' => array(
'locale' => 'en_us')
)
)
)
);
$inputfilter->add(array('name' => 'bigprice',
'required' => true,
'filters' => array('name' => 'StringTrim'),
array('name' => 'StringTags')),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 5,),
),
array(
'name' => 'float',
'options' => array(
'locale' => 'en_us')
)
)
)
);
$inputfilter->add(array('name' => 'familyprice',
'required' => true,
'filters' => array('name' => 'StringTrim'),
array('name' => 'StringTags')),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 5,),
),
array(
'name' => 'float',
'options' => array(
'locale' => 'en_us')
)
)
)
);
$inputfilter->add(array('name' => 'partyprice',
'required' => true,
'filters' => array('name' => 'StringTrim'),
array('name' => 'StringTags')),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 5,),
),
array(
'name' => 'float',
'options' => array(
'locale' => 'en_us')
)
)
)
);
$this->inputFilter=$inputfilter;
}
return $this->inputFilter;
}
}
You are missing an array in 'filters':
'filters' => array('name' => 'StringTrim'),
array('name' => 'StringTags')),
Should be:
'filters' => array(array('name' => 'StringTrim'),
array('name' => 'StringTags')),
Same goes for other input.
your first array is just end just before 'validator' index on line number 57
validators index is not part of any array. its gone outside of array. so 'validators' => showing the error . make sure 'validators' part of which array...
Catchable fatal error: Argument 1 passed to
Zend\Form\Form::setInputFilter() must implement interface
Zend\InputFilter\InputFilterInterface, null given, called in
C:\xampp\htdocs\pizza\module\Pizza\src\Pizza\Controller\PizzaController.php
on line 52 and defined in
C:\xampp\htdocs\pizza\vendor\zendframework\zendframework\library\Zend\Form\Form.php
on line 652
this is the file :
<?php
namespace Pizza\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Pizza\Form\AddPizzaForm;
use Pizza\Form\EditPizzaForm;
use Zend\View\Model\ViewModel;
use Pizza\Model\Pizza;
use Zend\View\Model\JSonModel;
class PizzaController extends AbstractActionController{
protected $pizzaTable;
public function indexAction()
{
return new ViewModel(array('pizzas' => $this->getPizzaTable()->find_all()));
}
public function addAction()
{
$add_form = new AddPizzaForm();
$request = $this->getRequest();
if ($request->isPost()) {
$pizza = new Pizza();
$add_form->setInputFilter($pizza->getInputFilter());
$add_form->setData($request->getPost());
if ($add_form->isValid()) {
$pizza->exchangeArray($add_form->getData());
$this->getPizzaTable()->save($pizza);
}
return $this->redirect()->toRoute('pizza');
}
return array('form' => $add_form);
}
public function editAction()
{
$id = (int) $this->params()->fromRoute('id',0);
if ($id == 0) {
//redirect to index action Route.
return $this->redirect()->toRoute('pizza');
}else{
$pizza = $this->getPizzaTable()->find_by_id($id);
$edit_form = new EditPizzaForm();
$edit_form->bind($pizza);
$request = $this->getRequest();
if ($request->isPost()) {
$edit_form->setInputFilter($pizza->getInputFilter());
$edit_form->setData($request->getPost());
if ($edit_form->isValid()) {
$this->getPizzaTable()->save($pizza);
return $this->redirect()->toRoute('pizza');
}
}
return array('id'=> $id,'form' => $edit_form);
}
}
public function deleteAction()
{
$id = (int) $_POST['id'];
$this->getPizzaTable()->delete($id);
$msg = "good";
$response = new JSonModel(array('response' => $msg));
return $response;
}
public function getPizzaTable()
{
if (!$this->pizzaTable) {
$sm = $this->getServiceLocator();
$this->pizzaTable = $sm->get('Pizza\Model\PizzaTable');
}
return $this->pizzaTable;
}
}
the Model File : Pizza
<?php
namespace Pizza\Model;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterInterface;
use Zend\InputFilter\InputFilterAwareInterface;
class Pizza implements InputFilterAwareInterface
{
public $id;
public $name;
public $ingredients;
public $smallprice;
public $bigprice;
public $familyprice;
public $partyprice;
protected $inputFilter;
public function __construct(){
}
public function exchangeArray($data){
$this->id = (!empty($data['id'])) ? $data['id'] : null;
$this->name = (!empty($data['name'])) ? $data['name'] : null;
$this->ingredients = (!empty($data['ingredients'])) ? $data['ingredients'] : null;
$this->smallprice = (!empty($data['smallprice'])) ? $data['smallprice'] : null;
$this->bigprice = (!empty($data['bigprice'])) ? $data['bigprice'] : null;
$this->familyprice = (!empty($data['familyprice'])) ? $data['familyprice'] : null;
$this->partyprice = (!empty($data['partyprice'])) ? $data['partyprice'] : null;
}
public function getArrayCopy(){
return get_object_vars($this);
}
public function setInputFilter(InputFilterInterface $inputfilter){
throw new \Exception("not Used");
}
public function getInputFilter(){
if (!$this->inputFilter) {
$inputfilter = new InputFilter();
$inputfilter->add(array(
'name' => 'name',
'required' => true,
'filters' => array(
array('name' => 'StringTrim'),
array('name' => 'StripTags')
),
'validators'=> array(
array('name' => 'StringLength',
'options'=> array(
'encoding' => 'UTF-8',
'min' => 5,
'max' =>30,
))
)
));
$inputfilter->add(array(
'name'=> 'ingredients',
'required' => true,
'filters' => array(
array('name' => 'StringTrim'),
array('name' => 'StripTags')
),
'validators'=> array(
array('name' => 'StringLength',
'options'=> array(
'encoding' => 'UTF-8',
'min' => 5,
'max' =>255,
))
)
));
$inputfilter->add(array(
'name' => 'smallprice',
'required' => true,
'filters' => array(
array('name' => 'StringTrim'),
array('name' => 'StripTags')
),
'validators'=> array(
array('name' => 'StringLength',
'options'=> array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 5,
))
)
));
$inputfilter->add(array(
'name' => 'bigprice',
'required' => true,
'filters' => array(
array('name' => 'StringTrim'),
array('name' => 'StripTags')
),
'validators'=> array(
array('name' => 'StringLength',
'options'=> array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 5,
))
)
));
$inputfilter->add(array(
'name' => 'familyprice',
'required' => true,
'filters' => array(
array('name' => 'StringTrim'),
array('name' => 'StripTags')
),
'validators'=> array(
array('name' => 'StringLength',
'options'=> array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 5,
))
)
));
$inputfilter->add(array(
'name'=> 'partyprice',
'required' => true,
'filters' => array(
array('name' => 'StringTrim'),
array('name' => 'StripTags')
),
'validators'=> array(
array('name' => 'StringLength',
'options'=> array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 5,
))
)
));
$this->inputFilter = $inputfilter;
}else{
return $this->inputFilter;
}
}
}
Please remove else in your getInputFilter method. I think this is what caused the error. This is why you got the error : null given, the $inputfilter is never returned the first time.
I corrected it for you. Replace your method by this one :
public function getInputFilter(){
if (!$this->inputFilter) {
$inputfilter = new InputFilter();
$inputfilter->add(array(
'name' => 'name',
'required' => true,
'filters' => array(
array('name' => 'StringTrim'),
array('name' => 'StripTags')
),
'validators'=> array(
array('name' => 'StringLength',
'options'=> array(
'encoding' => 'UTF-8',
'min' => 5,
'max' =>30,
))
)
));
$inputfilter->add(array(
'name'=> 'ingredients',
'required' => true,
'filters' => array(
array('name' => 'StringTrim'),
array('name' => 'StripTags')
),
'validators'=> array(
array('name' => 'StringLength',
'options'=> array(
'encoding' => 'UTF-8',
'min' => 5,
'max' =>255,
))
)
));
$inputfilter->add(array(
'name' => 'smallprice',
'required' => true,
'filters' => array(
array('name' => 'StringTrim'),
array('name' => 'StripTags')
),
'validators'=> array(
array('name' => 'StringLength',
'options'=> array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 5,
))
)
));
$inputfilter->add(array(
'name' => 'bigprice',
'required' => true,
'filters' => array(
array('name' => 'StringTrim'),
array('name' => 'StripTags')
),
'validators'=> array(
array('name' => 'StringLength',
'options'=> array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 5,
))
)
));
$inputfilter->add(array(
'name' => 'familyprice',
'required' => true,
'filters' => array(
array('name' => 'StringTrim'),
array('name' => 'StripTags')
),
'validators'=> array(
array('name' => 'StringLength',
'options'=> array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 5,
))
)
));
$inputfilter->add(array(
'name'=> 'partyprice',
'required' => true,
'filters' => array(
array('name' => 'StringTrim'),
array('name' => 'StripTags')
),
'validators'=> array(
array('name' => 'StringLength',
'options'=> array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 5,
))
)
));
$this->inputFilter = $inputfilter;
}
return $this->inputFilter;
}
You should check that $pizza->getInputFilter() is returning an object that implements a Zend\InputFilter\InputFilterInterface interface. Because setInputFilter() method of your Form class only accepts this kind of objects.
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.
The getInputFiler() method in your user model returns $this->inputFilter, but that property is never set. I think you're missing a $this->inputFilter = $inputFilter assignment towards the end of that function.
In your User.php change
return $this->inputFilter;
to
return $inputFilter
Your $this->inputFilter is always null. As Tim Fountain has mentioned it, it is never set.
Answer is given above
..but nevertheless I suggest to create a separate InputFilter class and do not define it within your model. If you have more than 1 model object, each model instance will have an InputFilter definition inside.