PHP-Zend MultiCheckbox:设置最大选择
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PHP-Zend MultiCheckbox:设置最大选择,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1702字,纯文字阅读大概需要3分钟。
内容图文
在这里,我再次提出一个简单的问题.
是否有一个现有的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;
}
}
内容总结
以上是互联网集市为您收集整理的PHP-Zend MultiCheckbox:设置最大选择全部内容,希望文章能够帮你解决PHP-Zend MultiCheckbox:设置最大选择所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。