日期验证器,使用 Zend Framework 验证日期是否大于或等于今天

发布于 2024-08-21 00:52:43 字数 296 浏览 7 评论 0原文

$form = new Zend_Form();

$mockDate = new Zend_Form_Element_Text('mock');

$mockDate->addValidator(???????);

$form->addElements(array($mockDate));

$result = $form->isValid();

if ($result) echo "YES!!!";
else echo "NO!!!";

假设该元素采用日期格式。如何确定给定的日期大于或等于今天?

$form = new Zend_Form();

$mockDate = new Zend_Form_Element_Text('mock');

$mockDate->addValidator(???????);

$form->addElements(array($mockDate));

$result = $form->isValid();

if ($result) echo "YES!!!";
else echo "NO!!!";

Assumption that the element is in a date format. How do I determine that the date given is greater than or equal to today?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

请恋爱 2024-08-28 00:52:44
class My_Validate_DateGreaterThanToday extends Zend_Validate_Abstract
{
    const DATE_INVALID = 'dateInvalid';

    protected $_messageTemplates = array(
        self::DATE_INVALID => "'%value%' is not greater than today"
    );

    public function isValid($value) {
        $this->_setValue($value);

        $date = new Zend_Date($value);
        $date->addDay(1);
        $now = new Zend_Date();

        // expecting $value to be YYYY-MM-DD
        if ($now->isLater($date)) {
            $this->_error(self::DATE_INVALID);
            return false;
        }

        return true;
    }
}

这更好,因为使用标准化的 Zend_Date 方法来检查日期,另一个 awsner 用户使用可以评估为不可预测值的字符串比较......

class My_Validate_DateGreaterThanToday extends Zend_Validate_Abstract
{
    const DATE_INVALID = 'dateInvalid';

    protected $_messageTemplates = array(
        self::DATE_INVALID => "'%value%' is not greater than today"
    );

    public function isValid($value) {
        $this->_setValue($value);

        $date = new Zend_Date($value);
        $date->addDay(1);
        $now = new Zend_Date();

        // expecting $value to be YYYY-MM-DD
        if ($now->isLater($date)) {
            $this->_error(self::DATE_INVALID);
            return false;
        }

        return true;
    }
}

that's better because uses standardized Zend_Date methods to check dates, the other awsner user a string comparison that can evaluate to impredictable values...

不离久伴 2024-08-28 00:52:43

您可以创建一个简单的验证器来执行此操作:

class My_Validate_DateGreaterThanToday extends Zend_Validate_Abstract
{
    const DATE_INVALID = 'dateInvalid';

    protected $_messageTemplates = array(
        self::DATE_INVALID => "'%value%' is not greater than or equal today"
    );

    public function isValid($value)
    {
        $this->_setValue($value);

        $today = date('Y-m-d');

        // expecting $value to be YYYY-MM-DD
        if ($value < $today) {
            $this->_error(self::DATE_INVALID);
            return false;
        }

        return true;
    }
}

并将其添加到元素中:

$mockDate->addValidator(new My_Validate_DateGreaterThanToday());

您可能想使用 Zend_Date 用于日期本地化和其他好处。

要创建自定义验证,请查看 Zend 的编写验证器的手册。

You can create a simple validator to do this:

class My_Validate_DateGreaterThanToday extends Zend_Validate_Abstract
{
    const DATE_INVALID = 'dateInvalid';

    protected $_messageTemplates = array(
        self::DATE_INVALID => "'%value%' is not greater than or equal today"
    );

    public function isValid($value)
    {
        $this->_setValue($value);

        $today = date('Y-m-d');

        // expecting $value to be YYYY-MM-DD
        if ($value < $today) {
            $this->_error(self::DATE_INVALID);
            return false;
        }

        return true;
    }
}

And add it to the element:

$mockDate->addValidator(new My_Validate_DateGreaterThanToday());

You probably want to check the date with Zend_Date for localization of dates and further benefits.

For creating custom validates, take a look at writing validators from Zend´s manual.

梦行七里 2024-08-28 00:52:43

这个问题相当老了。在当前版本的 ZF2 中,无需编写新的验证器。只需添加一个过滤器/验证器,如下所示:

public function getInputFilter()
{
    if(!$this->inputFilter){
        $inputFilter = new InputFilter();
        $inputFilter->add(array(
            'name' => 'mock',
            'validators' => array(
                array('name' => 'Date'),
                array(
                    'name' => 'GreaterThan',
                    'options' => array(
                        'min' => date('Y-m-d'),
                    ),
                ),
            ),
        ));
        $this->inputFilter = $inputFilter;
    }
    return $this->inputFilter;
}

它就可以完成工作。还有一个名为“inclusive”的选项,如果设置为“true”(在 GreaterThan 的“选项”中),将允许“今天”成为有效日期

The question is rather old. In the current version of ZF2 it is not necessary to write new validators. Simply add a filter / validator like this:

public function getInputFilter()
{
    if(!$this->inputFilter){
        $inputFilter = new InputFilter();
        $inputFilter->add(array(
            'name' => 'mock',
            'validators' => array(
                array('name' => 'Date'),
                array(
                    'name' => 'GreaterThan',
                    'options' => array(
                        'min' => date('Y-m-d'),
                    ),
                ),
            ),
        ));
        $this->inputFilter = $inputFilter;
    }
    return $this->inputFilter;
}

And it does the job. There is also an option called 'inclusive' which if set to 'true' (in the 'options' of GreaterThan), will allow 'today' to be a valid date

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文