3

上面的 URL 不是答案,我已经通过数据库适配器

我在过滤器表单类中调用“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 框架表单类进行注册。我已经定义了表单类和类过滤器并在控制器中调用它们。

除了“Db\RecordExists”之外,一切都运行良好。该表单没有针对数据库广告检查唯一的电子邮件,它只是提交者。但是其他过滤器和验证器工作正常

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

                 ),
        ));



    }
}
4

3 回答 3

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


}

}
于 2013-11-21T16:08:57.183 回答
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());
于 2015-02-15T09:00:06.240 回答
0

我认为问题在于您将适配器分配给传递给构造函数的值,一旦构造函数终止,该值就会被破坏。您必须将适配器存储在类中。试试下面的。在构造函数的顶部添加该行

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

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

于 2013-07-26T23:16:43.807 回答