0
votes

I have the following Model class:

<?php
namespace Tropa\Model;

use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;

class Setor
{
    public $codigo;
    public $nome;
    protected $inputFilter;

    public function exchangeArray($data)
    {
        $this->codigo = (isset($data['codigo'])) ? $data['codigo'] : null;
        $this->nome = (isset($data['nome'])) ? $data['nome'] : null;
    }

    public function getInputFilter()
    {
        if (!$this->inputFilter) {
            $inputFilter = new InputFilter();
            $factory = new InputFactory();
            $inputFilter->add($factory->createInput(array(
                'name' => 'codigo',
                'required' => false,
                'filters' => array(
                    array('name' => 'ToInt'), ## It was Int, but in PHP 7 it not works anymore, so I replaced it as suggest the zend documentation https://framework.zend.com/apidoc/2.4/classes/Zend.Filter.Int.html
                ),
                'validators' => array(
                    array(
                        'name' => 'Between',
                        'options' => array(
                            'min' => 0,
                            'max' => 3600
                        )
                    ),
                ),
            )));
            $inputFilter->add($factory->createInput(array(
                'name' => 'nome',
                'required' => true,
                'filters' => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                ),
                'validators' => array(
                    array(
                        'name' => 'StringLength',
                        'options' => array(
                            'encoding' => 'UTF-8',
                            'min' => 2,
                            'max' => 30,
                        )
                    )
                ),
            )));
            $this->inputFilter = $inputFilter;
        }

        return $this->inputFilter;
    }
}

When I run this class I get the message:

Zend\Filter\FilterPluginManager::get was unable to fetch or create an instance for ToInt

Anybody knows how to validate Integers now that PHP 7 reserved the word Int?

Added:

After installing the zendframework/zend-filter version 2.7.1 the error message had changed to:

Fatal error: Uncaught TypeError: Argument 1 passed to Zend\ServiceManager\AbstractPluginManager::__construct() must implement interface Zend\ServiceManager\ConfigInterface, instance of Zend\ServiceManager\ServiceManager given, called in C:\xampp\htdocs\corps\vendor\zendframework\zend-filter\src\FilterChain.php on line 112 and defined in C:\xampp\htdocs\corps\vendor\zendframework\zendframework\library\Zend\ServiceManager\AbstractPluginManager.php:60 Stack trace: #0 C:\xampp\htdocs\corps\vendor\zendframework\zend-filter\src\FilterChain.php(112): Zend\ServiceManager\AbstractPluginManager->__construct(Object(Zend\ServiceManager\ServiceManager)) #1 C:\xampp\htdocs\corps\vendor\zendframework\zend-filter\src\FilterChain.php(183): Zend\Filter\FilterChain->getPluginManager() #2 C:\xampp\htdocs\corps\vendor\zendframework\zendframework\library\Zend\InputFilter\Factory.php(358): Zend\Filter\FilterChain->attachByName('\Zend\Filter\To...', NULL, 1000) #3 C:\xampp\htdocs\corps\vendor\zendframework\zendframework\library\Zend\InputFilt in C:\xampp\htdocs\corps\vendor\zendframework\zendframework\library\Zend\ServiceManager\AbstractPluginManager.php on line 60

1
Which version of zend-filter do you have installed? (Run composer show zendframework/zend-filter.)gsc
I ran the command and got the message "package zendframework/zend-filter not found"jairhumberto
When the problem has first appeared? Maybe you’ve somehow removed this package? On the other hand, FilterPluginManager belongs to this package as well, it wouldn’t be able to give you an error if not installed.gsc
I was following a book, but the book was write before the release of php 7, so there was no problem with the Int filter before.jairhumberto
Does it work when you remove this filter? Can you try replacing 'ToInt' with \Zend\Filter\ToInt::class? Does composer show output any filter/validator-related packages? In which versions? Do you run composer commands from the project’s root directory?gsc

1 Answers

1
votes

It seems you’ve got zend-filter 2.3 installed? I think that you should start upgrading all Zend Framework packages to at least 2.4 version. zend-filter 2.3 doesn’t contain ToInt filter, but 2.4 does. Both versions contain Int filter, but in 2.4 it only serves as a link to ToInt and additionally generates an error message (have a look at Int class source in 2.4).