Zend表格验证范围日期 [英] Zend Form Validate Range Date

查看:109
本文介绍了Zend表格验证范围日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁给我一个手,为Zend框架创建一个自定义验证器,它检查日期是否在一个范围内?



示例:

  dateGT = 2011-09-05 
dateLT = 2011-07-05

如果表单字段设置为:
dateFieldForm = 2011-08-15
我期望验证器返回true!



,如果表单域设置为:
dateFieldForm = 2011-10-15
我期望验证器返回false!

解决方案

对不起,前面的大码:

  php 

/ ** @see Zend_Validate_Abstract * /
require_once'Zend / Validate / Abstract.php';

/ **
* @category Zend
* @package Zend_Validate
* @copyright版权所有(c)2005-2009 Zend Technologies USA Inc.(http: /www.zend.com)
* @license http://framework.zend.com/license/new-bsd新的BSD许可证
* /
class My_Validate_DateCompare扩展Zend_Validate_Abstract
{
/ **
*错误代码
* @const string
* /
const NOT_SAME ='notSame';
const MISSING_TOKEN ='missingToken';
const NOT_LATER ='notLater';
const NOT_EARLIER ='notEarlier';
const NOT_BETWEEN ='notBetween';

/ **
*错误消息
* @var数组
* /
protected $ _messageTemplates = array(
self :: NOT_SAME =>日期'%value%'不匹配所需的,
self :: NOT_BETWEEN =>日期不在有效范围内,
self :: NOT_LATER => ;日期'%值%'不晚于所需的,
self :: NOT_EARLIER =>日期'%值%'不早于必需,
self :: MISSING_TOKEN =>'没有提供匹配的日期',
);

/ **
* @var array
* /
protected $ _messageVariables = array(
'token'=>'_tokenString'
);

/ **
*要验证
* @var string的原始令牌
* /
protected $ _tokenString;
保护$ _token;
protected $ _compare;

/ **
*设置验证器选项
*
* @param mixed $ token
* @param mixed $ compare
* return void
* /
public function __construct($ token = null,$ compare = null)
{
if(null!== $ token){
$ this-> setToken($ token);
$ this-> setCompare($ compare);
}
}

/ **
*设置要比较的令牌
*
* @param mixed $ token
* @return Zend_Validate_Identical
* /
public function setToken($ token)
{
$ this-> _tokenString =(string)$ token;
$ this-> _token = $ token;
return $ this;
}

/ **
*检索令牌
*
* @return string
* /
public function getToken )
{
return $ this-> _token;
}

/ **
*设置比较哪个比较
*
* @param mixed $ compare
* @return Zend_Validate_Identical
* /
public function setCompare($ compare)
{
$ this-> _compareString =(string)$ compare;
$ this-> _compare = $ compare;
return $ this;
}

/ **
*检索比较
*
* @return string
* /
public function getCompare( )
{
return $ this-> _compare;
}

/ **
*由Zend_Validate_Interface定义
*
*当且仅当设置了令牌并且提供的值
*匹配该令牌。
*
* @param mixed $ value
* @return boolean
* /
public function isValid($ value)
{
$ this-> _setValue((string)$ value);
$ token = $ this-> getToken();

if($ token === null){
$ this-> _error(self :: MISSING_TOKEN);
返回false;
}

$ date1 = new Zend_Date($ value);
$ date2 = new Zend_Date($ token);

//以后
if($ this-> getCompare()=== true){
if($ date1-> compare($ date2) 0 || $ date1-> equals($ date2)){
$ this-> _error(self :: NOT_LATER);
返回false;
}
//不早于
} elseif($ this-> getCompare()=== false){
if($ date1-> compare($ date2) > 0 || $ date1-> equals($ date2)){
$ this-> _error(self :: NOT_EARLIER);
返回false;
}
//完全匹配
} elseif($ this-> getCompare()=== null){
if(!$ date1-> equals($ date2 )){
$ this-> _error(self :: NOT_SAME);
返回false;
}
//在范围
} else {
$ date3 = new Zend_Date($ this-> getCompare());

if($ date1-> compare($ date2)< 0 || $ date1-> compare($ date3)> 0){
$ this-> _error (self :: NOT_BETWEEN);
返回false;
}
}

//日期有效
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')); //之间的日期
$ element-> addValidator(new My_Validate_DateCompare('startdate',true)); // not later
$ element-> addValidator(new My_Validate_DateCompare('startdate',false)); //不早于

使用全局设置的日期格式(存储在Zend_Registry(Locale))。



还可以自定义每个案例的错误消息。



最新更新:修正错误的默认可选参数它应该是NULL而不是True。
将消息更改为不那么混乱。
某些格式和空格。


who gives me a hand to create a custom validator for Zend Framework, which checks that a date is in to a range?

Example:

dateGT = 2011-09-05
dateLT = 2011-07-05

if the form field is set to: dateFieldForm = 2011-08-15 I expect the validator returns true!

and if the form field is set to: dateFieldForm = 2011-10-15 I expect the validator returns false!

解决方案

Sorry, large code ahead:

<?php

/** @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;
    }
}

Usage:

$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

Uses the globally set date format (stored in Zend_Registry('Locale')).

It is also advisable to customize the error messages per case.

Latest update: fixed wrong default optional parameter which should be NULL instead of True. Changed the messages to be less confusing. Some formatting and whitespace.

这篇关于Zend表格验证范围日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆