gpt4 book ai didi

php - zend 表单验证

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:01:47 25 4
gpt4 key购买 nike

我想知道 Zend_Form 如何验证输入,我的意思是它如何知道要验证哪些输入字段。我查看了 php globals($_POST, $_GET) 并且没有看到任何设置为标识符的东西(例如)以了解如何验证。谁能给我推荐这方面的指南?

最佳答案

嗯,找出答案的最佳选择是查看 code of Zend_Form :

/**
* Validate the form
*
* @param array $data
* @return boolean
*/
public function isValid($data)
{
if (!is_array($data)) {
require_once 'Zend/Form/Exception.php';
throw new Zend_Form_Exception(__METHOD__ . ' expects an array');
}
$translator = $this->getTranslator();
$valid = true;
$eBelongTo = null;

if ($this->isArray()) {
$eBelongTo = $this->getElementsBelongTo();
$data = $this->_dissolveArrayValue($data, $eBelongTo);
}
$context = $data;
foreach ($this->getElements() as $key => $element) {
if (null !== $translator && $this->hasTranslator()
&& !$element->hasTranslator()) {
$element->setTranslator($translator);
}
$check = $data;
if (($belongsTo = $element->getBelongsTo()) !== $eBelongTo) {
$check = $this->_dissolveArrayValue($data, $belongsTo);
}
if (!isset($check[$key])) {
$valid = $element->isValid(null, $context) && $valid;
} else {
$valid = $element->isValid($check[$key], $context) && $valid;
$data = $this->_dissolveArrayUnsetKey($data, $belongsTo, $key);
}
}
foreach ($this->getSubForms() as $key => $form) {
if (null !== $translator && !$form->hasTranslator()) {
$form->setTranslator($translator);
}
if (isset($data[$key]) && !$form->isArray()) {
$valid = $form->isValid($data[$key]) && $valid;
} else {
$valid = $form->isValid($data) && $valid;
}
}

$this->_errorsExist = !$valid;

// If manually flagged as an error, return invalid status
if ($this->_errorsForced) {
return false;
}

return $valid;
}

简而言之,Zend_Form 将遍历表单中所有已配置的元素,并将它们与您传递给它的数组中的值进行比较。如果匹配,它将根据配置的验证器验证该单个值。

关于php - zend 表单验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5457027/

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