2013-02-19 60 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