2

I'm new to zend framework 2 and I have a question on comparing two inputs in the factory-backed form. My scenario is like following:

I want to compare two inputs, for example, $startDate and $endDate. I want to validate that $startDate is always less than $endDate. How I'm going to do this? For example:

$inputFilter->add($factory->createInput(array(
                'name'     => 'startDate',
                'required' => true,
                'validators' => array(
                    array(
                        'name'    => 'LessThan',
                        'options' => array(
                            'max'      => $endDate,
                        ),
                    ),
                ),
            )));

FYI, I'm following the Album tutorial and the $inputFilter is created in the classTable.php.

Thanks

Nandakumar V
  • 4,317
  • 4
  • 27
  • 47
Ewe Tek Min
  • 855
  • 10
  • 19
  • or is that other way to achieve this? Thanks... – Ewe Tek Min Jul 25 '13 at 03:38
  • You could use the Callback validator in a similar manner to this answer -> http://stackoverflow.com/questions/17529906/zf2-validation-how-can-i-validate-dependent-fields/17532467#17532467 – Crisp Jul 26 '13 at 16:07

2 Answers2

4

Thanks to Crisp! I solved it with something similar:

$inputFilter->add($factory->createInput(array(
            'name'     => 'startDate',
            'required' => true,
            'name'     => 'Callback',
                'options' => array(
                    'message' => array( 
                        Callback::INVALID_VALUE => 'Invalid period is given.',
                    ),
                    'callback' => function($value, $context=array()) {
                        return $value < $context['endDate'];
                    },
                ),
            )));
Ewe Tek Min
  • 855
  • 10
  • 19
0

The above answer probably be correct, but there might be some syntax or Callback error might be occur. The reason is, We generally use Callback Validation function in Models InputFilters, Not in a forms definition section(as of Zend Framework version 2.2.1).

This call back script part should come inside Model - InputFilters, Please refer this link: https://stackoverflow.com/a/19263037/2190889

As per this Url reference, date validation part works perfectly.

Community
  • 1
  • 1
KumarA
  • 1,368
  • 3
  • 18
  • 41