PHP-Zend MultiCheckbox:设置最大选择
作者:互联网
在这里,我再次提出一个简单的问题.
是否有一个现有的zend验证器来为用户可以选择的框设置最大值.
我希望他们选择不超过3个框.
我已经在网上搜索过,唯一发现的是在form元素的isValid函数中设置了一个错误.但是然后我有一个问题,错误显示每个选定的框. (这样4次或更多次)或者也许有人知道如何处理此问题?如果我只能显示一次此错误,那么我的问题也将得到解决.
感谢您的帮助.
解决方法:
您可以使用我的验证器,它根据值的数量进行检查.我完全出于相同的目的-验证multiselect中选定值的最大和最小数目:
<?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-form-element,zend-form,zend-validate,zend-framework,php 来源: https://codeday.me/bug/20191102/1988240.html