zf2表单类问题

时间:2013-07-24 11:43:18

标签: zend-framework2

上面的网址不是答案,我已经通过了DB适配器

我在Filter表单类中调用“Db \ RecordExists”时遇到了问题。

我已经在控制器中输入DB ADAPTPR。

$sm = $this->getServiceLocator();
 $dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');

 $form->setInputFilter(new RegisterStepFirstFilter($dbAdapter));    
                    $form->setData($request->getPost()); 
                    if (!$form->isValid()) {    

}   

我想在注册时查看是否存在电子邮件。我正在使用Zend框架表单类进行注册。我已经定义了表单类和类过滤器,并在Controller中调用它们。

除了“Db \ RecordExists”之外,每件事情都运转良好。该表单不会检查针对db ad的唯一电子邮件,而只是提交者。但是其他过滤器和验证器正常工作

$this->add(array(
            'name' => 'user_email',
            'required' => true,
            'filters' => array(
                array('name' => 'StripTags'),
                array('name' => 'StringTrim'),
                array('name' => 'HtmlEntities'),

            ),
            'validators' => array(
                            array('name' => 'EmailAddress'),
                            array('name' => 'StringLength', 'options' => array('encoding' => 'UTF-8', 'min' => 1, 'max' => 200,),
                            array('name' => 'Db\RecordExists', 'options' => array('table' => 'y2m_user','field' => 'user_email',  'adapter' => $dbAdapter),),
                ),
            ),
        ));

我的表单类是

<?php
namespace User\Form;
use Zend\Form\Form; 

class RegisterStepFirst extends Form
{ 
     protected $captchaElement= null;

    public function __construct($name = null)
    {
        // we want to ignore the name passed
        parent::__construct('user');
        $this->setAttribute('method', 'post');

        //$this->add(array('hash','csrf_token',array('salt'=>get_class($this).'s3cr3t%Ek@on9!'));   

        $this->add(array(
     'type' => 'Zend\Form\Element\Csrf',
     'name' => 'csrf',
     'options' => array(
             'csrf_options' => array(
                     'timeout' => 600,
                     'salt' => 'unique'
             )
     )
 ));

       $this->add(array(
            'name' => 'user_given_name',
            'type' => 'Text',
            'options' => array(
                'label' => 'Display Name',
            ),
            'attributes' => array(
                'id' => 'user_given_name',                              
            ) 
        ));

        $this->add(array(
            'name' => 'user_email',
            'type' => 'Text',
            'options' => array(
                'label' => 'Email',
            ),
            'attributes' => array(
                'placeholder' => 'mail@yourdomain', //set selecarray()ted to '1'
                'id' => 'user_email',
                'size'  => '100',

            ) 
        ));
        $this->add(array(
            'name' => 'user_password',
            'type' => 'Password',
            'options' => array(
                'label' => 'Password',
            ),
            'attributes' => array(               
                'id' => 'user_password',
                'size'  => '100',               
            ) 
        ));

        $this->add(array(
            'name' => 'user_retype_password',
            'type' => 'Password',
            'options' => array(
                'label' => 'Confirm Password',
            ),
            'attributes' => array(               
                'id' => 'user_retype_password',
                'size'  => '100',               
            ) 
        ));
        $this->add(array(
            'name' => 'submit',
            'type' => 'Submit',
            'attributes' => array(
                'value' => 'Register',
                'id' => 'submitbutton',
            ),
        ));


    }
}

过滤器是

<?php
namespace User\Form;
use Zend\InputFilter\InputFilter;
class RegisterStepFirstFilter extends InputFilter
{
    private $dbAdapter;
    public function __construct($dbAdapter) {         
        $this->add(array(
            'name' => 'user_given_name',
            'required' => true,
            'filters' => array(
                array('name' => 'StripTags'),
                array('name' => 'StringTrim'),
                array('name' => 'HtmlEntities'),
            ),
            'validators' => array(                      
                        array('name' => 'Alpha','options' => array('allowWhiteSpace' => true,),),
                        array('name' => 'StringLength','options' => array('encoding' => 'UTF-8', 'min' => 1,'max' => 100,),),
            ),
        )); 

        $this->add(array(
            'name' => 'user_email',
            'required' => true,
            'filters' => array(
                array('name' => 'StripTags'),
                array('name' => 'StringTrim'),
                array('name' => 'HtmlEntities'),

            ),
            'validators' => array(
                            array('name' => 'EmailAddress'),
                            array('name' => 'StringLength', 'options' => array('encoding' => 'UTF-8', 'min' => 1, 'max' => 200,),
                            array('name' => 'Db\RecordExists', 'options' => array('table' => 'y2m_user','field' => 'user_email',  'adapter' => $dbAdapter),),
                ),
            ),
        ));

        $this->add(array(
            'name' => 'user_password',
            'required' => true,
            'filters' => array(
                array('name' => 'StripTags'),
                array('name' => 'StringTrim'),
                array('name' => 'HtmlEntities'),
            ),
            'validators' => array(
                        array('name' => 'StringLength','options' => array('encoding' => 'UTF-8', 'min' => 3,'max' => 60,),
                ),
            ),
        ));

        $this->add(array(
            'name' => 'user_retype_password',
            'required' => true,
            'filters' => array(
                array('name' => 'StripTags'),
                array('name' => 'StringTrim'),
                array('name' => 'HtmlEntities'),
            ),
            'validators' => array(
                        array('name' => 'StringLength','options' => array('encoding' => 'UTF-8', 'min' => 3,'max' => 60,),),
                        array('name' => 'identical','options' => array('token' => 'user_password'),)    

                 ),
        ));



    }
}

3 个答案:

答案 0 :(得分:0)

我认为问题在于您将适配器分配给传递给构造函数的值,构造函数终止后会将其销毁。您必须将适配器存储在类中。请尝试以下方法。在构造函数的顶部添加行

$this->dbAdapter=$dbAdapter;  // this will store the value passed to the constructor as a member of the class.  

然后在过滤器中使用$ this-&gt; dbAdapter而不是$ dbAdapter。

答案 1 :(得分:0)

试试这个

use Zend\Db\Adapter\AdapterInterface;
use Zend\Db\Adapter\Adapter;
class TestForm extends Form
{
    protected $adapter;
    public function __construct(AdapterInterface $dbAdapter)
    {
        $this->adapter =$dbAdapter;
                parent::__construct("Test Form");


}

}

答案 2 :(得分:0)

您可以在inputfilter中使用服务管理器。

配置/自动加载/ global.php

return array(
    'service_manager' =>   array(
        'invokables' =>       array(
            'RegisterStepFirstFilter' => 'User\Form\RegisterStepFirstFilter',
        )
    )
)

RegisterStepFirstFilter输入过滤器类

<?php
namespace User\Form;

use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterAwareTrait;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorAwareTrait;

class RegisterStepFirstFilter extends InputFilter implements ServiceLocatorAwareInterface, InputFilterAwareInterface
{
    use ServiceLocatorAwareTrait, InputFilterAwareTrait;

    public function getInputFilter() {         
        if (!$this->inputFilter) {
            $this->add(array(
                'name' => 'user_given_name',
                'required' => true,
                'filters' => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                    array('name' => 'HtmlEntities'),
                ),
                'validators' => array(                      
                            array('name' => 'Alpha','options' => array('allowWhiteSpace' => true,),),
                            array('name' => 'StringLength','options' => array('encoding' => 'UTF-8', 'min' => 1,'max' => 100,),),
                ),
            )); 

            $this->add(array(
                'name' => 'user_email',
                'required' => true,
                'filters' => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                    array('name' => 'HtmlEntities'),

                ),
                'validators' => array(
                                array('name' => 'EmailAddress'),
                                array('name' => 'StringLength', 'options' => array('encoding' => 'UTF-8', 'min' => 1, 'max' => 200,),
                                array('name' => 'Db\RecordExists', 'options' => array('table' => 'y2m_user','field' => 'user_email',  'adapter' => 
                                    $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter')),),
                    ),
                ),
            ));

            $this->add(array(
                'name' => 'user_password',
                'required' => true,
                'filters' => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                    array('name' => 'HtmlEntities'),
                ),
                'validators' => array(
                            array('name' => 'StringLength','options' => array('encoding' => 'UTF-8', 'min' => 3,'max' => 60,),
                    ),
                ),
            ));

            $this->add(array(
                'name' => 'user_retype_password',
                'required' => true,
                'filters' => array(
                    array('name' => 'StripTags'),
                    array('name' => 'StringTrim'),
                    array('name' => 'HtmlEntities'),
                ),
                'validators' => array(
                            array('name' => 'StringLength','options' => array('encoding' => 'UTF-8', 'min' => 3,'max' => 60,),),
                            array('name' => 'identical','options' => array('token' => 'user_password'),)    

                     ),
            ));

            $this->inputFilter = $inputFilter;
        }

        return $this->inputFilter;

    }
}

并在您的控制器中

$inputFilter = $this->getServiceLocator()->get('RegisterStepFirstFilter');
$form->setInputFilter($inputFilter->getInputFilter());