Zend framework2 Zend Framework 2提供的过滤器规范无效;不包括「;名称“;钥匙

Zend framework2 Zend Framework 2提供的过滤器规范无效;不包括「;名称“;钥匙,zend-framework2,zend-inputfilter,Zend Framework2,Zend Inputfilter,我有一个错误“提供的筛选器规范无效;未包含”name“key”。我找了很长时间,什么也没找到。请帮帮我 RegisterController.php <?php namespace Users\Controller; use Zend\Mvc\Controller\AbstractActionController; use Zend\View\Model\ViewModel; use Users\Form\RegisterForm; use Users\Form\RegisterFilte

我有一个错误“提供的筛选器规范无效;未包含”name“key”。我找了很长时间,什么也没找到。请帮帮我

RegisterController.php

<?php
namespace Users\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Users\Form\RegisterForm;
use Users\Form\RegisterFilter;

class RegisterController extends AbstractActionController
{
    public function indexAction()
    {
        $form = new RegisterForm();
        $viewModel = new ViewModel(array('form' => $form));
        return $viewModel;
    }
    public function confirmAction()
    {
        $viewModel = new ViewModel();
        return $viewModel;
    }

    public function processAction()
    {
        if(!$this->request->isPost())
        {
            return $this->redirect()->toRoute(NULL, array('controller' => 'register', 'action' => 'index'));
        }
        $post = $this->request->getPost();
        $form = new RegisterForm();
        $inputFilter = new RegisterFilter();
        $form->setInputFilter($inputFilter);
        $form->setData($post);
        if(!$form->isValid())
        {
            $model = new ViewModel(array(
                'error' => true,
                'form' => $form,
            ));
            $model->setTemplate('users/register/index');
            return $model;
        }
        $this->createUser($form->getData());
        return $this->redirect()->toRoute(NULL, array(
            'controller' => 'register',
            'action' => 'confirm',
        ));
    }

    protected function createUser(array $data)
    {
        $sm = $this->getServiceLocator();
        $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
        $resultSetPrototype = new \Zend\Db\ResultSet\ResultSet();
        $resultSetPrototype->setArrayObjectPrototype(new \Users\Model\User);
        $tableGateway = new \Zend\Db\TableGateway\TableGateway('user', $dbAdapter, null, $resultSetPrototype);

        $user = new User();
        $user->exchangeArray($data);
        $userTable = new UserTable($tableGateway);
        $userTable->saveUser($user);
        return true;
    }
}

您的登录输入的输入筛选器规范中似乎有错误,验证程序数组位于筛选器数组中。应该是这样的

RegisterFilter.php

<?php

namespace Users\Form;

use Zend\InputFilter\InputFilter;

class RegisterFilter extends InputFilter
{
    public function __construct()
    {       
        $this->add(array(
                'name' => 'userid',
                'required' => true,
                'filters' => array(
                    array('name' => 'Int'),
                ),
        ));

        $this->add(array(
            'name' => 'login',
            'required' => true,
            'filters' => array(
                array(
                    'name' => 'StripTags',
                ),
                'validators' => array(
                    array(
                        'name' => 'StringLength',
                        'options' => array(
                            'encoding' => 'UTF-8',
                            'nim' => 2,
                            'max' => 20,
                        ),
                    ),
                ),
            ),
        ));

        $this->add(array(
            'name' => 'email',
            'required' => true,
            'validators' => array(
                array(
                    'name' => 'EmailAddress',
                    'options' => array(
                        'domain' => true,
                    ),
                ),
            ),
        ));

        $this->add(array(
            'name' => 'password',
            'required' => true,
        ));

        $this->add(array(
            'name' => 'confirm_password',
            'required' => true,
        ));

        $this->add(array(
                'name' => 'typeid',
                'required' => true,
                'filters' => array(
                    array('name' => 'Int'),
                ),
        ));
    }
}
<?php

namespace Users\Form;

use Zend\InputFilter\InputFilter;

class RegisterFilter extends InputFilter
{
    public function __construct()
    {       
       // ...

        $this->add(array(
            'name' => 'login',
            'required' => true,
            'filters' => array(
                array(
                    'name' => 'StripTags',
                ),
            ),
            // validators go here !
            'validators' => array(
                array(
                    'name' => 'StringLength',
                    'options' => array(
                        'encoding' => 'UTF-8',
                        'min' => 2, // should be 'min' not 'nim'
                        'max' => 20,
                    ),
                ),
            ),
        ));

        /// ...
    }
}

在我看来,就像
Users\Form\RegisterFilter

$this->add(array(
     'name' => 'login',
     'required' => true,
     'filters' => array(
         array(
             'name' => 'StripTags',
         ),
         'validators' => array(
             array(
                 'name' => 'StringLength',
                 'options' => array(
                     'encoding' => 'UTF-8',
                     'min' => 2,
                     'max' => 20,
                 ),
             ),
         ),
     ),
));
应该这样定义:

$this->add(array(
     'name' => 'login',
     'required' => true,
     'filters' => array(
         array(
             'name' => 'StripTags',
         ),
      ),
      'validators' => array(
          array(
             'name' => 'StringLength',
             'options' => array(
                 'encoding' => 'UTF-8',
                 'min' => 2,
                 'max' => 20,
             ),
         ),
     ),
));
有关参考,请参阅

$this->add(array(
     'name' => 'login',
     'required' => true,
     'filters' => array(
         array(
             'name' => 'StripTags',
         ),
      ),
      'validators' => array(
          array(
             'name' => 'StringLength',
             'options' => array(
                 'encoding' => 'UTF-8',
                 'min' => 2,
                 'max' => 20,
             ),
         ),
     ),
));