php – Zend Framework:需要三个值中的一个
作者:互联网
对于我的学校项目,我正在研究数据库管理应用程序.
这是我第一个真正的Zend Framework应用程序.
现在,我现在已经根据需要设置了3个值,邮政编码,电子邮件和电话.
像这样需要它们(例子):
$mail = $this->createElement('text', 'mail');
$mail->setLabel('E-mail:')
->setAttrib('size', 50)->addValidator('StringLength', false, array(6, 40))->addValidator('EmailAddress', true)
->setRequired(true);
$telephone = $this->createElement('text', 'telephone');
$telephone->setLabel('Telephone:')
->setAttrib('size', 50)->addValidator('StringLength', false, array(10, 10))
->setRequired(true);
我怎样才能只需要其中一个?
我正在使用Zend_Form,并与Displaygroups合作.
有人知道我的程序的解决方案吗?也许使用数组?
提前致谢,
JorritK
UPDATE
@ brady.vitrano
好的,我的代码的第一部分现在看起来像这样:
<?php
class Application_Form_Validate_ContactMethodSelected
extends Zend_Validate_Abstract
{
const INVALID = 'invalid';
protected $_messageTemplates = array(
self::INVALID => 'Must select at least one contact method'
);
public function isValid($value, $context = array())
{
// You need to use your element names, consider making these dynamic
$checkFields = array('telefoon','mobiel','mail');
// Check if all are empty
foreach ( $checkFields as $field ) {
if (isset($context[$field]) && !empty($context[$field])) {
// Only one value needs to return true..skip the rest
return true;
}
}
// All were empty, set your own error message
$this->_error(self::INVALID);
return false;
}
}
class Application_Form_Nieuwkandidaat extends Zend_Form
{
public function init()
{
$this->setMethod('post');
$DB = Zend_Db_Table::getDefaultAdapter();
$telefoon = $this->createElement('text', 'telefoon');
$telefoon->setLabel('Telefoon:')
->setAttrib('size', 50)->addValidator('StringLength', false,array(10,10));
$telefoon->addValidator(new Application_Form_Validate_ContactMethodSelected());
$mobiel = $this->createElement('text', 'mobiel');
$mobiel->setLabel('Mobiel:')
->setAttrib('size', 50)->addValidator('StringLength', false,array(10,10));
$mobiel->addValidator(new Application_Form_Validate_ContactMethodSelected());
$mail = $this->createElement('text', 'mail');
$mail->setLabel('E-mail:')
->setAttrib('size', 50)->addValidator('StringLength', false,array(6,40))->addValidator('EmailAddress', true);
$mail->addValidator(new Application_Form_Validate_ContactMethodSelected());
点击提交按钮后,它不会显示消息.我还应该改变什么呢?谢谢你的帮助!
解决方法:
一个好的解决方案是元素的write a custom validator.
在您的isValid方法中,您必须根据其他值的$context进行检查.就像是:
编辑
/** /library/Application/Form/Validate/ContactMethodSelected.php **/
class Application_Form_Validate_ContactMethodSelected
extends Zend_Validate_Abstract
{
const INVALID = 'invalid';
protected $_messageTemplates = array(
self::INVALID => 'Must select at least one contact method'
);
public function isValid($value, $context = array())
{
// You need to use your element names, consider making these dynamic
$checkFields = array('phone','email','address');
// Check if all are empty
foreach ( $checkFields as $field ) {
if (isset($context[$field]) && !empty($context[$field])) {
// Only one value needs to return true..skip the rest
return true;
}
}
// All were empty, set your own error message
$this->_error(self::INVALID);
return false;
}
}
现在,您必须告诉元素使用该验证器.因此,在表单init()方法中进行更改.
$mail->addValidator(new Application_Form_Validate_ContactMethodSelected());
$telephone->addValidator(new Application_Form_Validate_ContactMethodSelected());
不要忘记:一旦拥有自己的自定义验证器,就必须从每个元素中删除isRequired().
EDIT2
您必须将自定义验证程序设置为链中的第一个验证程序,并在失败时中断.此外,您必须将setAllowEmpty()设置为false.
$telefoon = $this->createElement('text', 'telefoon');
$telefoon->setLabel('Telefoon:')
->setAttrib('size', 50)->setAllowEmpty(false)
->addValidator(new Application_Form_Validate_ContactMethodSelected(),true)
->addValidator('StringLength', false,array(10,10));
$mobiel = $this->createElement('text', 'mobiel');
$mobiel->setLabel('Mobiel:')
->setAttrib('size', 50)->setAllowEmpty(false)
->addValidator(new Application_Form_Validate_ContactMethodSelected(),true)
->addValidator('StringLength', false,array(10,10));
$mail = $this->createElement('text', 'mail');
$mail->setLabel('E-mail:')
->setAttrib('size', 50)->setAllowEmpty(false)
->addValidator(new Application_Form_Validate_ContactMethodSelected(),true)
->addValidator('StringLength', false,array(6,40))->addValidator('EmailAddress', true);
接下来,您必须使用以下内容更新isValid方法:
public function isValid($value, $context = array())
{
// You need to use your element names, consider making these dynamic
$checkFields = array('telefoon','mobiel','mail');
// Check if all are empty
foreach ( $checkFields as $field ) {
if (isset($context[$field]) && !empty($context[$field])) {
if (!empty($value)) {
// This is the element with content... validate as true
return true;
}
// we are going to return false and no error
// to break validation chain on other empty values
// This is a quick hack, don't have time to invest in this
return false;
}
}
// All were empty, set your own error message
$this->_error(self::INVALID);
return false;
}
现在,您将不得不为使用此验证器的代码添加另一个条件.我们必须要求表单没有任何错误消息.
if ($form->isValid($_POST) || !count($form->getMessages())) {
/** Valid, now you can process **/
} else {
/** Not valid **/
}
标签:php,require,zend-framework,zend-form,requiredfieldvalidator 来源: https://codeday.me/bug/20190713/1444766.html