簡體   English   中英

PHP 7 Zend Framework 2.4 輸入過濾器

[英]PHP 7 Zend Framework 2.4 InputFilter

請幫助修復以下錯誤。 我正在關注Zend 框架文檔

致命錯誤:

Cannot use 'Int' as class name as it is reserved in E:\Working\PHP\Zend-Framework\Zend-Framework-2\zf2-stable\vendor\zendframework\zendframework\library\Zend\Filter\Int.php on line 12

錯誤:

array('name' => 'Int'), ## Fatal error
array('name' => 'ToInt') ## Zend\Filter\FilterPluginManager::get was unable to fetch or create an instance for Zend\Filter\ToInt
array('name' => 'StringTrim') ## Works Only for Edit and Delete, however Not for Add

環境 :

$ php --version
PHP 7.0.9 (cli) (built: Jul 20 2016 11:08:23) ( ZTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies

$ composer --version
Composer version 1.2.1 2016-09-12 11:27:19

$ composer show
zendframework/zendframework 2.3.3 Zend Framework 2
zendframework/zendxml       1.0.2 Utility library for XML usage, best practices, and security in PHP 

zf2-tutorial/module/Album/src/Album/Model/Album.php

<?php

namespace Album\Model;

use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;

class Album implements InputFilterAwareInterface
{
    public $id;
    public $artist;
    public $title;
    protected $inputFilter;

    public function exchangeArray($data)
    {
        $this->id     = (isset($data['id']))     ? $data['id']     : null;
        $this->artist = (isset($data['artist'])) ? $data['artist'] : null;
        $this->title  = (isset($data['title']))  ? $data['title']  : 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'     => 'id',
                'required' => true,
                'filters'  => array(
                    array('name' => 'Int'), ## Fatal error
                ),
            ));

            $inputFilter->add(array(
                'name'     => 'artist',
                '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(array(
                'name'     => 'title',
                'required' => true,
                'filters'  => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                ),
                'validators' => array(
                    array(
                        'name'    => 'StringLength',
                        'options' => array(
                            'encoding' => 'UTF-8',
                            'min'      => 1,
                            'max'      => 100,
                        ),
                    ),
                ),
            ));

            $this->inputFilter = $inputFilter;
        }

        return $this->inputFilter;
    }
}

zf2-tutorial/module/Album/view/album/album/add.phtml

<?php

// module/Album/view/album/album/add.phtml:

$title = 'Add new album';
$this->headTitle($title);
?>
<h1><?php echo $this->escapeHtml($title); ?></h1>
<?php
$form->setAttribute('action', $this->url('album', array('action' => 'add')));
$form->prepare();

echo $this->form()->openTag($form);
echo $this->formHidden($form->get('id'));
echo $this->formRow($form->get('title'));
echo $this->formRow($form->get('artist'));
echo $this->formSubmit($form->get('submit'));
echo $this->form()->closeTag();

編輯

注釋掉公共函數 getInputFilter() 中的以下幾行有效:

        /*
        $inputFilter->add(array(
            'name'     => 'id',
            'required' => true,
            'filters'  => array(
                array('name' => 'Int'),
            ),
        ));
        */

謝謝。

我很確定你應該在這個版本中使用 IsInt 過濾器而不是 Int 過濾器。

Zend\I18n\Validator\IsInt

我使用"zendframework/zendframework": "2.3.9",

1) 創建類ToInt.php

<?php
namespace Application\Filter;

use Zend\Filter\AbstractFilter;

class ToInt extends AbstractFilter
{
    /**
     * Returns (int) $value
     *
     * If the value provided is non-scalar, the value will remain unfiltered
     *
     * @param  string $value
     * @return ToInt|mixed
     */
    public function filter($value)
    {
        if (!is_scalar($value)) {
            return $value;
        }
        $value = (string) $value;

        return (int) $value;
    }
}

2)在輸入


$inputFilter->add($factory->createInput(array(
    'name' => 'id',
    'required' => true,
    'filters' => array(
        array('name' => '\Application\Filter\ToInt'),
    ),
)));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM