php可指定日期范围表单,php – Zend表单验证范围日期

对不起,未来有大量代码:

/** @see Zend_Validate_Abstract */

require_once 'Zend/Validate/Abstract.php';

/**

* @category Zend

* @package Zend_Validate

* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)

* @license http://framework.zend.com/license/new-bsd New BSD License

*/

class My_Validate_DateCompare extends Zend_Validate_Abstract

{

/**

* Error codes

* @const string

*/

const NOT_SAME = 'notSame';

const MISSING_TOKEN = 'missingToken';

const NOT_LATER = 'notLater';

const NOT_EARLIER = 'notEarlier';

const NOT_BETWEEN = 'notBetween';

/**

* Error messages

* @var array

*/

protected $_messageTemplates = array(

self::NOT_SAME => "The date '%value%' does not match the required",

self::NOT_BETWEEN => "The date is not in the valid range",

self::NOT_LATER => "The date '%value%' is not later than the required",

self::NOT_EARLIER => "The date '%value%' is not earlier than required",

self::MISSING_TOKEN => 'No date was provided to match against',

);

/**

* @var array

*/

protected $_messageVariables = array(

'token' => '_tokenString'

);

/**

* Original token against which to validate

* @var string

*/

protected $_tokenString;

protected $_token;

protected $_compare;

/**

* Sets validator options

*

* @param mixed $token

* @param mixed $compare

* @return void

*/

public function __construct($token = null, $compare = null)

{

if (null !== $token) {

$this->setToken($token);

$this->setCompare($compare);

}

}

/**

* Set token against which to compare

*

* @param mixed $token

* @return Zend_Validate_Identical

*/

public function setToken($token)

{

$this->_tokenString = (string) $token;

$this->_token = $token;

return $this;

}

/**

* Retrieve token

*

* @return string

*/

public function getToken()

{

return $this->_token;

}

/**

* Set compare against which to compare

*

* @param mixed $compare

* @return Zend_Validate_Identical

*/

public function setCompare($compare)

{

$this->_compareString = (string) $compare;

$this->_compare = $compare;

return $this;

}

/**

* Retrieve compare

*

* @return string

*/

public function getCompare()

{

return $this->_compare;

}

/**

* Defined by Zend_Validate_Interface

*

* Returns true if and only if a token has been set and the provided value

* matches that token.

*

* @param mixed $value

* @return boolean

*/

public function isValid($value)

{

$this->_setValue((string) $value);

$token = $this->getToken();

if ($token === null) {

$this->_error(self::MISSING_TOKEN);

return false;

}

$date1 = new Zend_Date($value);

$date2 = new Zend_Date($token);

// Not Later

if ($this->getCompare() === true){

if ($date1->compare($date2) < 0 || $date1->equals($date2)) {

$this->_error(self::NOT_LATER);

return false;

}

// Not Earlier

} elseif ($this->getCompare() === false) {

if ($date1->compare($date2) > 0 || $date1->equals($date2)) {

$this->_error(self::NOT_EARLIER);

return false;

}

// Exact Match

} elseif ($this->getCompare() === null) {

if (!$date1->equals($date2)) {

$this->_error(self::NOT_SAME);

return false;

}

// In Range

} else {

$date3 = new Zend_Date($this->getCompare());

if ($date1->compare($date2) < 0 || $date1->compare($date3) > 0) {

$this->_error(self::NOT_BETWEEN);

return false;

}

}

// Date is valid

return true;

}

}

用法:

$element->addValidator(new My_Validate_DateCompare('startdate')); //exact match

$element->addValidator(new My_Validate_DateCompare('startdate', null)); //exact match

$element->addValidator(new My_Validate_DateCompare('startdate', 'enddate')); //between dates

$element->addValidator(new My_Validate_DateCompare('startdate', true)); //not later

$element->addValidator(new My_Validate_DateCompare('startdate', false)); //not earlier

使用全局设置的日期格式(存储在Zend_Registry(‘Locale’)中).

建议每个案例自定义错误消息.

最新更新:修复了错误的默认可选参数,该参数应为NULL而不是True.更改消息以减少混淆.一些格式和空格.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值