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