2013-02-19 109 views
3

Hiho, 我想验证zf2 表单中的日期字段。我设置'格式'选项来获得我需要的格式。 但每次我验证它,我都会得到一个错误。 验证程序是这样的:在zendframework2中验证日期

  $inputFilter->add($factory->createInput(array(
      'name' => 'user_data_birth', 
      'required' => false, 
      'validators' => array(
       array(
       'name' => 'Date', 
       'options' => array(
        'format' => 'd.m.Y', 
        'locale' => 'de', 
        'messages' => array(
         \Zend\Validator\Date::INVALID => 'Das scheint kein gültiges Datum zu sein.', 
         \Zend\Validator\Date::INVALID_DATE => 'Das scheint kein gültiges Datum zu sein. (Invalid Date)', 
         \Zend\Validator\Date::FALSEFORMAT => 'Das Datum ist nicht im richtigen Format.', 
         ), 
        ), 
       ), 
       array(
        'name' => 'NotEmpty', 
        'options' => array(
        'messages' => array(
         \Zend\Validator\NotEmpty::IS_EMPTY => 'Bitte geben Sie das Datum an' 
         ), 
        ), 
       ) 
      ), 
     ))); 

但每次都遇到一个错误的日期格式错误。

+0

你能提供给你问题的日期? – 2013-02-19 19:38:51

+0

报告的验证错误也是必需的。 – Ocramius 2013-02-19 19:39:30

+0

提出问题的日期如下:09.06.1982(与9.6.1982相同)。这是来自一个表格。 验证错误是: >输入似乎不是有效日期 或重新加载我的自定义错误之一。所以它似乎会切换错误信息。 – 2013-02-20 13:17:29

回答

0

您是否尝试过其他格式,如基本:'Y-m-d'? 结果是什么?

+0

我解决了它。我不使用ValidatorFactory。相反,我确认日期seperatly,这工作正常。 – 2013-04-03 09:05:36

13

可以解决的问题,开始日期应小于结束日期的验证,使用如下回调函数:

  $inputFilter->add($factory->createInput(array(
       'name' => 'end_date', 
       'required' => true,     
       'filters' => array(
         array('name' => 'StripTags'), 
         array('name' => 'StringTrim'), 
       ), 
       'validators' => array(
        array(
         'name' => 'Callback', 
         'options' => array(
          'messages' => array(
            \Zend\Validator\Callback::INVALID_VALUE => 'The end date should be greater than start date', 
          ), 
          'callback' => function($value, $context = array()) {          
           $startDate = \DateTime::createFromFormat('d-m-Y', $context['start_date']); 
           $endDate = \DateTime::createFromFormat('d-m-Y', $value); 
           return $endDate >= $startDate; 
          }, 
         ), 
        ),       
       ), 
     ))); 

使用上面的代码,我已经解决了我的问题。我希望这有帮助。

+0

使用匿名函数会中断缓存。我建议使用班级 – Hooli 2016-03-25 10:04:16

0

如果我们只需要以一种形式使用此功能,则CallBack功能是一种非常方便的方式。大多数情况下,我们需要在多个地方使用它。我做了一些研究并编写了这个自定义验证器。这个验证器比较两个字符串;我添加了另一个技巧,看看我们是否需要这些字符串是不同的或相同的。

如果我们正在更改密码;那么旧密码和新密码会有所不同,同时我们需要将新密码验证与新密码相同。只要将不同的参数更改为“真”或“假”,就可以将此验证程序用于这两种情况。

Form Fields 
user_password 
new_user_password 
new_user_password_verify 

创建应用程序的\ src \应用\验证了新的验证StringCompare.php

<?php 

namespace Application\Validator; 

class StringCompare extends \Zend\Validator\AbstractValidator { 

    const SAME = 'same'; 
    const DIFFERENT = 'different'; 

    protected $messageTemplates = array(
     self::SAME => "Both the words are the same", 
     self::DIFFERENT => "Both the words are different", 
    ); 

    protected $messageVariables = array(
     'compareWith' => array('options' => 'compareWith'), 
     'different' => array('options' => 'different'), 
    ); 

    protected $options = array(
     'compareWith' => "", 
     'different' => true, 
     'encoding' => 'UTF-8', 
    ); 

    public function __construct($options = array()) { 
     parent::__construct($options); 
    } 

    public function getCompareWith() { 
     return $this->options[ 'compareWith' ]; 
    } 

    public function getDifferent() { 
     return $this->options[ 'different' ]; 
    } 

    public function isValid($value, $context=array()) { 

     $compareWith = $this->getCompareWith(); 
     $different = $this->getDifferent(); 

     $returnValue = $value == $context[$compareWith]; 
     if ($different) { 
      $returnValue = !$returnValue; 
     if (!$returnValue) { 
      $this->error(self::SAME); 
      } 
     } else { 
     if (!$returnValue) { 
     $this->error(self::DIFFERENT); 
     } 
    } 
     return $returnValue; 


    } 

} 

以下添加到窗体过滤

$this->add (array (
    'name' => 'new_user_password', 
    'required' => true, 
    'filters' => array (
     array ( 
      'name' => 'StringTrim', 
     ), 
    ), 
    'validators' => array (
     array (
      'name' => 'StringLength', 
      'options' => array (
       'min' => 8, 
       'max' => 20, 
      ) 
     ), 
     array (
      'name' => 'Application\Validator\StringCompare', 
      'options' => array (
       'compareWith' => 'user_password', 
       'different' => true, 
      ), 
     ), 
    ) 
)); 

在你的控制器执行表单验证我们通过。 如果我们使用不同='真',验证程序确保两个值是不同的,如果我们使用'假',那么它确保字符串是相同的。

1

需要更改日期元素中的验证器。如果你将格式传递给元素,你会好起来的。你也可以从元素中获取验证器。

$date = new Element\DateTime('test'); 
$date->setFormat('d.m.Y'); 

$validators = $date->getValidators() 
// apply you changes