Search code examples
zend-framework2zend-inputfilter

Zend Framework 2 Invalid filter specification provided; does not include "name" key


I have a problem with this error "Invalid filter specification provided; does not include "name" key". I searched it for long time and nothing. Please help me.

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

Solution

  • Seems to me like the login input in 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,
                     ),
                 ),
             ),
         ),
    ));
    

    should be defined like this:

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

    For reference, see