gpt4 book ai didi

php - Zend 框架 : Require one out of three values

转载 作者:可可西里 更新时间:2023-10-31 23:54:23 25 4
gpt4 key购买 nike

对于我的学校项目,我正在开发一个数据库管理应用程序。这是我的第一个真正的 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());

点击提交按钮后不会显示消息。我还应该改变什么?感谢您的帮助!

最佳答案

要考虑的一个好的解决方案是 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 - Zend 框架 : Require one out of three values,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6682314/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com