对于我的学校项目,我正在开发一个数据库管理应用程序。这是我的第一个真正的 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。
有人知道我的程序的解决方案吗?也许使用数组?
提前致谢,
约里特
更新
@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());
点击提交按钮后它不会显示消息。我还应该改变什么?感谢您的帮助!