[英]zend form check record no exists in database
I have a form that check if email exists in the database within 2 tables. 我有一个表单,检查数据库中是否存在2个表中的电子邮件。
I'm using Zend_Validate_Db_NoRecordExists for both validate, but it only check the second one. 我正在使用Zend_Validate_Db_NoRecordExists进行验证,但它只检查第二个。
Any idea why it's not working? 知道为什么它不起作用吗?
class Application_Form_ReferUser extends Zend_Form
{
public $email, $freeDownload, $buyNow;
public function init()
{
$this->setName('referUser');
$EmailExists = new Zend_Validate_Db_NoRecordExists(
array(
'table' => 'referrals',
'field' => 'email'
)
);
$EmailExists2 = new Zend_Validate_Db_NoRecordExists(
array(
'table' => 'users',
'field' => 'email'
)
);
$EmailExists->setMessage('This e-mail is already taken');
$EmailExists2->setMessage('This e-mail is already taken');
$this->email = $this->createElement('text', 'email')
->setLabel('Email')
->addValidator($EmailExists)
->addValidator($EmailExists2)
->addValidator('EmailAddress')
->setRequired(true);
$this->freeDownload = $this->createElement('button', 'btn_free_download')
->setLabel('Free Download')
->setAttrib('type', 'submit');
$this->buyNow = $this->createElement('button', 'btn_buy_now')
->setLabel('Buy Now')
->setAttrib('type', 'submit');
$this->addElements(array($this->email, $this->freeDownload, $this->buyNow));
$elementDecorators = array(
'ViewHelper'
);
$this->setElementDecorators($elementDecorators);
}
}
uses this: 用这个:
addValidators (array $ validators, $ files = null)
ejem: ejem:
$this->addValidators(array($EmailExists,$EmailExists2,'EmailAddress'));
I work myself 我自己工作
$this ->addElement($email = new Zend_Form_Element_Text('email'));
$email->setLabel('E-mail')
->addValidator('Db_NoRecordExists', false, array('table' => 'table1', 'field' => 'email'))
->addValidator('Db_NoRecordExists', false, array('table' => 'table2', 'field' => 'email'))
->addValidator('EmailAddress', false);
Try some thing like below (untested one), 试试下面的东西(未经测试的),
$emailNotEmpty = new Zend_Validate_NotEmpty();
$emailNotEmpty->setMessage('You must enter a email address.');
$emailFormat = new Zend_Validate_Regex('/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/');
$emailFormat->setMessage('You must enter a valid email address.');
$emailUnique1 = new Zend_Validate_Db_NoRecordExists('referrals', 'email');
$emailUnique1->setMessage('This email address is already registered.');
$emailUnique2 = new Zend_Validate_Db_NoRecordExists('users', 'email');
$emailUnique2->setMessage('This email address is already registered.');
$email = new Zend_Form_Element_Text('email', array(
'label' => 'Email Address:',
'class' => 'text-size text hastip',
'title' => $qtips_messages['key_email'],
'tabindex' => '2',
'required' => true,
'value'=> '',
'filters' => array('StringTrim'),
'validators' => array(
array($emailNotEmpty, true),
array($emailFormat, true),
array($emailUnique1,true),
array($emailUnique2,true),
),
'decorators' => $this->requiredElementDecorators,
'description' => '<img src="../../images/star.png" alt="required" />',
));
$this->addElement($email);
I don't think you add same validator multiple times on an element. 我不认为你在元素上多次添加相同的验证器。 Check class
Zend_Form_Element
addValidator()
line 1153. According to the code $this->_validators[$name] = $validator;
检查类
Zend_Form_Element
addValidator()
第1153行。根据代码$this->_validators[$name] = $validator;
when we add same validator to an element multiple times the former will be overridden. 当我们多次向元素添加相同的验证器时,前者将被覆盖。
I think you have to create a custom form validator or use isValid()
separately on the element and validate it. 我认为你必须创建一个自定义表单验证器或分别在元素上使用
isValid()
并验证它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.