2013-07-08 27 views
16

Làm cách nào để đặt bộ lọc đầu vào phụ thuộc vào trường nhập khác. Tôi muốn xác minh rằng trường 'apDepTime' nhiều hơn 'apArrTime'.xác thực zf2: Làm cách nào để xác thực các trường phụ thuộc?

Làm cách nào tôi có thể xử lý điều này trong zf2?

Tôi cũng muốn lưu ý rằng tôi đang sử dụng 'Trình xác thực ngày'.

Hãy giúp tôi với bất kỳ ai.

FlightDataForm.php

<?php 

namespace FcFlight\Form; 

use Zend\Form\Form; 
use Zend\Form\Element; 

class FlightDataForm extends Form 
{ 
/** 
* @var string 
*/ 
protected $_formName = 'flightData'; 

/** 
* @param null $name 
* @param array $options 
*/ 
public function __construct($name = null) 
{ 
    if (!is_null($name)) { 
     $this->_formName = $name; 
    } 

    parent::__construct($this->_formName); 

    //Fieldset Ap Dep 
    $this->add(array(
     'name'   => 'apDep', 
     'type'   => 'Zend\Form\Fieldset', 
     'options'  => array(
      'legend'  => 'App Dep', 
     ), 
     'elements'  => array(
      array(
       'spec' => array(
        'name' => 'apDepTime', 
        'type' => 'Zend\Form\Element\Text', 
        'attributes' => array(
         'required' => true, 
         'maxlength' => '5', 
         'id' => 'apDepTime', 
        ), 
        'options' => array(
         'label' => 'Time', 
         'hint' => 'HH:MM', 
         'description' => 'UTC', 
        ), 
       ), 
      ), 
     ), 
    )); 

    //Fieldset Ap Arr 
    $this->add(array(
     'name'   => 'apArr', 
     'type'   => 'Zend\Form\Fieldset', 
     'options'  => array(
      'legend'  => 'App Arr', 
     ), 
     'elements'  => array(
      //apArrTime 
      array(
       'spec' => array(
        'name' => 'apArrTime', 
        'type' => 'Zend\Form\Element\Text', 
        'attributes' => array(
         'required' => true, 
         'maxlength' => '5', 
         'id' => 'apArrTime', 
        ), 
        'options' => array(
         'label' => 'Time', 
         'hint' => 'HH:MM', 
         'description' => 'UTC', 
        ), 
       ), 
      ), 
     ), 
    )); 

    $this->add(new Element\Csrf('csrf')); 

    //Submit button 
    $this->add(array(
     'name' => 'submitBtn', 
     'attributes' => array(
      'type' => 'submit', 
      'value' => 'Add', 
     ), 
    )); 

} 
} 

FlightDataFilter.php

<?php 
namespace FcFlight\Filter; 

use Zend\InputFilter\Factory as InputFactory; 
use Zend\InputFilter\InputFilter; 
use Zend\InputFilter\InputFilterAwareInterface; 
use Zend\InputFilter\InputFilterInterface; 
use Zend\Db\Adapter\Adapter; 

class FlightDataFilter implements InputFilterAwareInterface 
{ 

/** 
* @var $inputFilter 
*/ 
protected $inputFilter; 

/** 
* @var $dbAdapter 
*/ 
protected $dbAdapter; 

/** 
* @var string 
*/ 
protected $table = ''; 

public $apDepTime; 
public $apArrTime; 

/** 
* @param \Zend\Db\Adapter\Adapter $dbAdapter 
*/ 
public function __construct(Adapter $dbAdapter) 
{ 
    $this->dbAdapter = $dbAdapter; 
} 

/** 
* @return \Zend\Db\Adapter\Adapter 
*/ 
public function getDbAdapter() 
{ 
    return $this->dbAdapter; 
} 

/** 
* Array to Object 
* 
* @param array $data 
*/ 
public function exchangeArray(array $data) 
{ 
    $this->apDepTime = (isset($data['apDep']['apDepTime'])) ? $data['apDep']['apDepTime'] : null; 
    $this->apArrTime = (isset($data['apArr']['apArrTime'])) ? $data['apArr']['apArrTime'] : null; 
} 

/** 
* @return array 
*/ 
public function getArrayCopy() 
{ 
    return get_object_vars($this); 
} 

/** 
* @param InputFilterInterface $inputFilter 
* @return void|InputFilterAwareInterface 
* @throws \Exception 
*/ 
public function setInputFilter(InputFilterInterface $inputFilter) 
{ 
    throw new \Exception("Not used"); 
} 


/** 
* @return \Zend\InputFilter\InputFilter|\Zend\InputFilter\InputFilterInterface 
*/ 
public function getInputFilter() 
{ 
    if (!$this->inputFilter) { 

     $inputFilter = new InputFilter(); 
     $factory = new InputFactory(); 

     $flightNumberInputFilter = new InputFilter(); 

     $flightNumberInputFilter->add($factory->createInput(array(
      'name' => 'flightNumberIdIcao', 
      'required' => true, 
     ))); 

     $apDepInputFilter = new InputFilter(); 

     $apDepInputFilter->add($factory->createInput(array(
      'name' => 'apDepTime', 
      'required' => true, 
      'validators' => array(
       array(
        'name' => 'Date', 
        'options' => array(
         'format' => 'H:i', 
        ), 
       ), 
      ), 
     ))); 

     $inputFilter->add($apDepInputFilter, 'apDep'); 

     $apArrInputFilter = new InputFilter(); 

     $apArrInputFilter->add($factory->createInput(array(
      'name' => 'apArrTime', 
      'required' => true, 
      'validators' => array(
       array(
        'name' => 'Date', 
        'options' => array(
         'format' => 'H:i', 
        ), 
       ), 
       array(
        'name' => 'Callback', 
        'options' => array(
         'messages' => array(
          \Zend\Validator\Callback::INVALID_VALUE => 'The arrival time is less than the departure time', 
         ), 
         'callback' => function($value, $context = array()) { 
          // value of this input 
          $apArrTime = \DateTime::createFromFormat('H:i', $value); 
          // value of input to check against from context 
          $apDepTime = \DateTime::createFromFormat('H:i', $context['apDepTime']); 
          // compare times, eg.. 
          return $apDepTime > $apArrTime; 
         }, 
        ), 
       ), 
      ), 
     ))); 

     $inputFilter->add($apArrInputFilter, 'apArr'); 

     $this->inputFilter = $inputFilter; 
    } 

    return $this->inputFilter; 
} 
} 

Trả lời

29

Bạn có thể sử dụng Zend\Validator\Callback validator cho điều này

Tham số đầu tiên truyền cho gọi lại của bạn là giá trị của đầu vào mà trình xác thực được áp dụng.

Tham số thứ hai là một mảng giá trị biểu mẫu khác của bạn được khóa bằng tên đầu vào và biểu thị ngữ cảnh mà bạn muốn so sánh các giá trị.

Là một ví dụ đơn giản ...

$apArrInputFilter->add($factory->createInput(array(
     'name' => 'apArrTime', 
     'required' => true, 
     'validators' => array(
       array(
        'name' => 'Callback', 
        'options' => array(
         'messages' => array(
          \Zend\Validator\Callback::INVALID_VALUE => 'The departure time is less than the arrival time', 
         ), 
         'callback' => function($value, $context=array()) { 
          // value of this input 
          $apArrTime = $value; 
          // value of input to check against from context 
          $apDepTime = $context['apDepTime']; 
          // compare times, eg.. 
          $isValid = $apDepTime > $apArrTime; 
          return $isValid; 
         }, 
        ), 
       ), 
     ), 
    ))); 

Rõ ràng bạn sẽ cần phải viết mã yêu cầu của bạn cho việc so sánh ngày thực tế, nhưng điều đó sẽ giúp bạn bắt đầu.

+0

Crisp. Cảm ơn nhiều. Tôi mới ở ZF. Giải pháp của bạn đã giúp tôi rất nhiều. Nhưng mã của tôi không thấy $ context ['apDepTime'] vì apDepTime nằm trong một InputFilter khác ($ apDepInputFilter) và tôi không thể thấy $ this trong hàm ẩn danh. –

+0

Tôi đã cập nhật mã của mình bằng cách sử dụng quyết định của bạn. Còn lại nghĩ làm thế nào để có được giá trị apDepTime. –

0

Bạn cũng có thể sử dụng một cách tiếp cận khác. Usa một InputFilter để định nghĩa hàm isValid() của riêng bạn.

Cho bạn biết biểu mẫu sử dụng InputFilter.

$this->setInputFilter(new Filter\MyFilter()); 

Thực hiện hàm isValid() của riêng bạn.

namespace Application\Form\Filter; 

use Zend\InputFilter\InputFilter; 
use Zend\Validator; 
use Zend\Filter; 

class MyFilter extends InputFilter 
{ 
    public function isValid($context = null) 
    { 
     // add your dependent validation 
    } 

    public function __construct() 
    { 
     // Add youre form field validations 
    } 

http://theabstract.de/zf2-dynamic-form-validation/

Các vấn đề liên quan