0
votes

i have written some InputFilters. Now i wanne filter and validate a form with a couple of them. So i thought i can write a new InputFilter add all other i need in constructer and set this new one to the form, but it dosent work ^^ here is my code:

sample InputFilter

class MyEmailFilter extends InputFilter
{
    protected $config = array(
        array(
            'name'          => 'email',
            'required'      => true,
            'allow_empty'   => false,
            'validators'    => array(
                array(
                    'name' => 'NotEmpty',
                    'options' => array(
                        'messages' => array(
                            NotEmpty::IS_EMPTY => 'Please type in your emailaddress.',
                        )
                    ),
                    'break_chain_on_failure' => true,
                ),
                array(
                    'name'  => 'EmailAddress',
                    'break_chain_on_failure' => true,
                ),
            ),
        ),
    );

    public function init()
    {
        foreach ($this->config as $name => $input) {
            if (is_string($name)) {
                $this->add($input, $name);
            } else {
                $this->add($input);
            }
        }
    }
 }


my config:

return array(
    'input_filters'   => array(
        'invokables' => array(
            'member.myemailfilter' => 'Member\InputFilter\MyEmailFilter',
        ),
        'factories' => array(
            'member.registerfilter' => 'Member\InputFilter\Service\RegisterFilterFactory',
        ),
        'shared'     => array(
            'common.myemailfilter' => true,
            'member.registerfilter' => true,
        ),
    ),
);


my factory:

class RegisterFilterFactory implements FactoryInterface
{
    public function createService(ServiceLocatorInterface $inputFilterPluginManager)
    {
        /** @var ServiceLocatorInterface $serviceLocator */
        $serviceLocator = $inputFilterPluginManager->getServiceLocator();

        /** @var MyEmailFilter $emailFilter */
        $emailFilter = $serviceLocator->get('InputFilterManager')->get('member.myemailfilter');

        return new RegisterFilter($emailFilter);
    }
}


the RegisterInputFilter (here i wanne add all i need for the form):

class RegisterFilter extends InputFilter
{
    public function __construct(InputFilter $myEmailFilter)
    {
        $this->add($myEmailFilter, 'email');
    }
}


now adding the filter to the form in my registercontroller:

class RegisterController extends AbstractActionController
{
    /**
     * registrierungszeuch
     */
    public function indexAction()
    {
        if ($this->getAuthService()->hasIdentity()) {
            return $this->redirect()->toRoute('home');
        }

        $form = $this->getRegisterForm();

        if ($this->request->isPost()) {
            $form->setData($this->params()->fromPost());
            $form->setInputFilter($this->getRegisterFilter());
            if ($form->isValid()) {
                var_dump('valide');
            } else {
                var_dump('invalide');
            }
        }

        $viewModel = new ViewModel();

        $viewModel
            ->setVariable('form', $form);

        return $viewModel;
    }

    /**
     * @return Form
     */
    private function getRegisterForm()
    {
        return $this->getServiceLocator()->get('FormElementManager')->get('form.user.register');
    }

    /**
     * @return RegisterFilter
     */
    private function getRegisterFilter()
    {
        return $this->getServiceLocator()->get('InputFilterManager')->get('member.registerfilter');
    }
}



when i direct add myEmailFilter to form, it works, if i add the registerfilter, i get an exception

[[Additional information]]: Zend\InputFilter\Exception\InvalidArgumentException [[File]]: /home/vagrant/project/vendor/zendframework/zendframework/library/Zend/InputFilter/BaseInputFilter.php:137 [[Message]]: Zend\InputFilter\BaseInputFilter::setData expects an array or Traversable argument; received string [[Stack trace]]: #0 /home/vagrant/project/vendor/zendframework/zendframework/library/Zend/InputFilter/BaseInputFilter.php(535): Zend\InputFilter\BaseInputFilter->setData('') #1 /home/vagrant/project/vendor/zendframework/zendframework/library/Zend/InputFilter/BaseInputFilter.php(147): Zend\InputFilter\BaseInputFilter->populate() #2 /home/vagrant/project/vendor/zendframework/zendframework/library/Zend/Form/Form.php(467): Zend\InputFilter\BaseInputFilter->setData(Array) #3 /home/vagrant/project/module/Member/src/Controller/RegisterController.php(29): Zend\Form\Form->isValid() #4 /home/vagrant/project/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/AbstractActionController.php(83): Member\Controller\RegisterController->indexAction() #5 [internal function]: Zend\Mvc\Controller\AbstractActionController->onDispatch(Object(Zend\Mvc\MvcEvent)) #6 /home/vagrant/project/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent)) #7 /home/vagrant/project/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure)) #8 /home/vagrant/project/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/AbstractController.php(117): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure)) #9 /home/vagrant/project/vendor/zendframework/zendframework/library/Zend/Mvc/DispatchListener.php(114): Zend\Mvc\Controller\AbstractController->dispatch(Object(Zend\Http\PhpEnvironment\Request), Object(Zend\Http\PhpEnvironment\Response)) #10 [internal function]: Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent)) #11 /home/vagrant/project/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent)) #12 /home/vagrant/project/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure)) #13 /home/vagrant/project/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php(309): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure)) #14 /home/vagrant/project/public/index.php(17): Zend\Mvc\Application->run() #15 {main}


how can i stack single filters in the right way? thought the add() function also accepting InputFilters but it dosent looks like.

thx for help :-)

1

1 Answers

1
votes

Your question is pretty loaded. You need to reconsider your approach. I have a little article and tool you can consider, here:

http://circlical.com/blog/2015/7/6/zend-framework-2-forms-factories-inputfilters-and-hydration