title: zf2 validation: How can I validate dependent fields?
body: <p>How can I set a input filter which is dependent from another input field.
  I want to verify that the 'apDepTime' field is more than 'apArrTime'.</p>
  
  <p>How can i handle this in zf2?</p>
  
  <p>I also want to note that I am using 'Date validator'.</p>
  
  <p>Please help me anybody.</p>
  
  <p><strong>FlightDataForm.php</strong></p>
  
  <pre><code>&lt;?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-&gt;_formName = $name;
      }
  
      parent::__construct($this-&gt;_formName);
  
      //Fieldset Ap Dep
      $this-&gt;add(array(
          'name'          =&gt; 'apDep',
          'type'          =&gt; 'Zend\Form\Fieldset',
          'options'       =&gt; array(
              'legend'        =&gt; 'App Dep',
          ),
          'elements'      =&gt; array(
              array(
                  'spec' =&gt; array(
                      'name' =&gt; 'apDepTime',
                      'type' =&gt; 'Zend\Form\Element\Text',
                      'attributes' =&gt; array(
                          'required' =&gt; true,
                          'maxlength' =&gt; '5',
                          'id' =&gt; 'apDepTime',
                      ),
                      'options' =&gt; array(
                          'label' =&gt; 'Time',
                          'hint' =&gt; 'HH:MM',
                          'description' =&gt; 'UTC',
                      ),
                  ),
              ),
          ),
      ));
  
      //Fieldset Ap Arr
      $this-&gt;add(array(
          'name'          =&gt; 'apArr',
          'type'          =&gt; 'Zend\Form\Fieldset',
          'options'       =&gt; array(
              'legend'        =&gt; 'App Arr',
          ),
          'elements'      =&gt; array(
              //apArrTime
              array(
                  'spec' =&gt; array(
                      'name' =&gt; 'apArrTime',
                      'type' =&gt; 'Zend\Form\Element\Text',
                      'attributes' =&gt; array(
                          'required' =&gt; true,
                          'maxlength' =&gt; '5',
                          'id' =&gt; 'apArrTime',
                      ),
                      'options' =&gt; array(
                          'label' =&gt; 'Time',
                          'hint' =&gt; 'HH:MM',
                          'description' =&gt; 'UTC',
                      ),
                  ),
              ),
          ),
      ));
  
      $this-&gt;add(new Element\Csrf('csrf'));
  
      //Submit button
      $this-&gt;add(array(
          'name' =&gt; 'submitBtn',
          'attributes' =&gt; array(
              'type' =&gt; 'submit',
              'value' =&gt; 'Add',
          ),
      ));
  
  }
  }
  </code></pre>
  
  <p><strong>FlightDataFilter.php</strong></p>
  
  <pre><code>&lt;?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-&gt;dbAdapter = $dbAdapter;
  }
  
  /**
   * @return \Zend\Db\Adapter\Adapter
   */
  public function getDbAdapter()
  {
      return $this-&gt;dbAdapter;
  }
  
  /**
   * Array to Object
   *
   * @param array $data
   */
  public function exchangeArray(array $data)
  {
      $this-&gt;apDepTime = (isset($data['apDep']['apDepTime'])) ? $data['apDep']['apDepTime'] : null;
      $this-&gt;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-&gt;inputFilter) {
  
          $inputFilter = new InputFilter();
          $factory = new InputFactory();
  
          $flightNumberInputFilter = new InputFilter();
  
          $flightNumberInputFilter-&gt;add($factory-&gt;createInput(array(
              'name' =&gt; 'flightNumberIdIcao',
              'required' =&gt; true,
          )));
  
          $apDepInputFilter = new InputFilter();
  
          $apDepInputFilter-&gt;add($factory-&gt;createInput(array(
              'name' =&gt; 'apDepTime',
              'required' =&gt; true,
              'validators' =&gt; array(
                  array(
                      'name' =&gt; 'Date',
                      'options' =&gt; array(
                          'format' =&gt; 'H:i',
                      ),
                  ),
              ),
          )));
  
          $inputFilter-&gt;add($apDepInputFilter, 'apDep');
  
          $apArrInputFilter = new InputFilter();
  
          $apArrInputFilter-&gt;add($factory-&gt;createInput(array(
              'name' =&gt; 'apArrTime',
              'required' =&gt; true,
              'validators' =&gt; array(
                  array(
                      'name' =&gt; 'Date',
                      'options' =&gt; array(
                          'format' =&gt; 'H:i',
                      ),
                  ),
                  array(
                      'name' =&gt; 'Callback',
                      'options' =&gt; array(
                          'messages' =&gt; array(
                              \Zend\Validator\Callback::INVALID_VALUE =&gt; 'The arrival time is less than the departure time',
                          ),
                          'callback' =&gt; 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 &gt; $apArrTime;
                          },
                      ),
                  ),
              ),
          )));
  
          $inputFilter-&gt;add($apArrInputFilter, 'apArr');
  
          $this-&gt;inputFilter = $inputFilter;
      }
  
      return $this-&gt;inputFilter;
  }
  }
  </code></pre>