在这里,我再次提出一个简单的问题。
是否有现有的 zend 验证器来为用户可以选择的框设置最大值。我希望他们选择不超过 3 个框。
我在网上搜索过,唯一发现的是在表单元素的 isValid 函数中设置了一个错误。但是后来我遇到了问题,每个选定的框都会显示错误。(所以 4 次或更多次)或者也许有人知道如何处理这个问题?如果我只能显示此错误一次,我的问题也将得到解决。
感谢您的帮助。
在这里,我再次提出一个简单的问题。
是否有现有的 zend 验证器来为用户可以选择的框设置最大值。我希望他们选择不超过 3 个框。
我在网上搜索过,唯一发现的是在表单元素的 isValid 函数中设置了一个错误。但是后来我遇到了问题,每个选定的框都会显示错误。(所以 4 次或更多次)或者也许有人知道如何处理这个问题?如果我只能显示此错误一次,我的问题也将得到解决。
感谢您的帮助。
您可以使用我的验证器,它会检查值的数量。我完全出于相同的目的使用 - 验证多选中所选值的最大和最小数量:
<?php
class App_Validate_ValuesNumber extends Zend_Validate_Abstract
{
const TOO_LESS = 'tooLess';
const TOO_MUCH = 'tooMuch';
protected $_type = null;
protected $_val = null;
/**
* @var array
*/
protected $_messageTemplates = array(
self::TOO_LESS => "At least %num% values required",
self::TOO_MUCH => "Not more then %num% required",
);
/**
* @var array
*/
protected $_messageVariables = array(
'num' => '_val'
);
/**
* Constructor for the integer validator
*
* @param string $type Comparison type, that should be used
* TOO_LESS means that value should be greater then items number
* TOO_MUCH means opposite
* @param int $val Value to compare items number with
*/
public function __construct($type, $val)
{
$this->_type = $type;
$this->_val = $val;
}
/**
* Defined by Zend_Validate_Interface
*
* Returns true if and only if $value is a valid integer
*
* @param string|integer $value
* @return boolean
*/
public function isValid($value)
{
// Value shoul dbe greated
if ($this->_type == self::TOO_LESS) {
if (count($value) < $this->_val) {
$this->_error(self::TOO_LESS);
return false;
}
}
// Value should be less
if ($this->_type == self::TOO_MUCH) {
if (count($value) > $this->_val) {
$this->_error(self::TOO_MUCH);
return false;
}
}
return true;
}
}
我今天刚打了这个。这是一个zend bug。http://framework.zend.com/issues/browse/ZF-11667。该问题的修复存在差异,但在 1.12 发布之前不会出现。我不想等待,所以我修补了我的 Zend_Form_Element。修复效果很好。在修复之前,我在 MultiChecks 上的错误消息对每个选中的框都重复了一次。