0
votes

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

    }
}
2
actually there is problem with your Model named Pizza, because input filter is defined on Model Pizza. So plz check your Model first or plz write the Model so we can sort out your problem. Thanks. and make sure you have added this on your Model also namespace Pizza\Model; use Zend\InputFilter\InputFilter; use Zend\InputFilter\InputFilterAwareInterface; use Zend\InputFilter\InputFilterInterface; class Pizza implements InputFilterAwareInterface {...Prashant Vardhan Singh
i add the code of the Model can you check it pllz :DOmae

2 Answers

0
votes

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;


}

Hope it helps !

0
votes

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.