Zend Framework 2 - InputFilter添加InputFilter

时间:2015-02-20 07:28:02

标签: zend-framework2 zend-form2 zend-inputfilter input-filter

我写了一些InputFilters。现在,我想过滤并验证一个表格,其中有几个。所以我想我可以写一个新的InputFilter在构造函数中添加我需要的所有其他东西,并将这个新的一个设置为表单,但它起作用了^^ 这是我的代码:

示例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);
            }
        }
    }
 }


我的配置:

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


我的工厂:

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


RegisterInputFilter(这里我想添加表格所需的全部内容):

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


现在将过滤器添加到我的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');
    }
}



当我直接将myEmailFilter添加到表单时,它可以工作,如果我添加了registerfilter,我得到一个异常

[[附加信息]]: Zend的\输入过滤\异常\ InvalidArgumentException [[文件]]: /home/vagrant/project/vendor/zendframework/zendframework/library/Zend/InputFilter/BaseInputFilter.php:137 [[信息]]: Zend \ InputFilter \ BaseInputFilter :: setData需要一个数组或Traversable参数;收到字符串 [[堆栈跟踪]]: #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 [内部函数]: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) ,对象(关闭)) #8 /home/vagrant/project/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/AbstractController.php(117):Zend \ EventManager \ EventManager->触发器('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) ,对象(Zend \ Http \ PhpEnvironment \ Response)) #10 [内部函数]: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) ,对象(关闭)) #13 /home/vagrant/project/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php(309):Zend \ EventManager \ EventManager->触发器('dispatch',Object(Zend \ Mvc \ MvcEvent) ,对象(关闭)) #14 /home/vagrant/project/public/index.php(17):Zend \ Mvc \ Application-> run() #15 {main}


如何以正确的方式堆叠单个过滤器?认为add()函数也接受InputFilters,但看起来像。

求助: - )

1 个答案:

答案 0 :(得分:1)

你的问题非常严重。你需要重新考虑你的方法。我有一个你可以考虑的文章和工具,在这里:

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