【问题标题】:Zend Framework 2 Invalid filter specification provided; does not include "name" keyZend Framework 2 提供了无效的过滤器规范;不包括“名称”键
【发布时间】:2015-12-30 22:26:27
【问题描述】:

我对“提供的过滤器规范无效;不包括“名称”键”这个错误有疑问。我搜索了很长时间,一无所获。请帮帮我。

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

RegisterForm.php

<?php

namespace Users\Form;

use Zend\Form\Form;

class RegisterForm extends Form
{
    public function __construct($name = null)
    {
        parent::__construct('Register');
        $this->setAttribute('method', 'post');
        $this->setAttribute('enctype', 'multipart/form-data');

        $this->add(array(
            'name' => 'userid',
            'type' => 'hidden',
        ));

        $this->add(array(
            'name' => 'login',
            'attributes' => array(
                'type' => 'Text',
                'required' => 'required',
            ),
            'options' => array(
                'label' => 'Login'
            ),
        ));

        $this->add(array(
            'name' => 'email',
            'attributes' => array(
                'type' => 'Email',
            ),
            'options' => array(
                'label' => 'Email'
            ),
            'attributes' => array(
                'required' => 'required'
            ),
            'filters' => array(
                array('name' => 'StringTrim'),
            ),
            'validators' => array(
                array(
                    'name' => 'EmailAddress',
                    'options' => array(
                        'messages' => array(
                        \Zend\Validator\EmailAddress::INVALID_FORMAT => 'Niepoprawny format adresu email'
                        )
                    )
                ),
            ),
        ));

        $this->add(array(
            'name' => 'password',
            'attributes' => array(
                'type' => 'Password',
                'required' => 'required',
            ),
            'options' => array(
                'label' => 'Hasło'
            ),
        ));

        $this->add(array(
            'name' => 'confirm_password',
            'attributes' => array(
                'type' => 'Password',
                'required' => 'required',
            ),
            'options' => array(
                'label' => 'Potwierdź hasło'
            ),
        ));

        $this->add(array(
            'name' => 'typeid',
            'type' => 'hidden',
        ));

        $this->add(array(
            'name' => 'submit',
            'type' => 'Submit',
            'attributes' => array(
                'value' => 'Zarejestruj',
                'class' => 'btn btn-primary',
            ),
        ));      
    }
}

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'),
                ),
        ));
    }
}

【问题讨论】:

    标签: zend-framework2 zend-inputfilter


    【解决方案1】:

    您的登录输入的输入过滤器规范似乎有错误,您的验证器数组在您的过滤器数组内。它应该看起来像这样......

    RegisterFilter.php

    <?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,
                        ),
                    ),
                ),
            ));
    
            /// ...
        }
    }
    

    【讨论】:

      【解决方案2】:

      在我看来就像Users\Form\RegisterFilter 中的login 输入

      $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,
                   ),
               ),
           ),
      ));
      

      参考,见

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-10
        • 2017-04-02
        • 2014-07-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多