Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
259 views
in Technique[技术] by (71.8m points)

zend framework2 - zf2 validation: How can I validate dependent fields?

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'.

How can i handle this in zf2?

I also want to note that I am using 'Date validator'.

Please help me anybody.

FlightDataForm.php

<?php

namespace FcFlightForm;

use ZendFormForm;
use ZendFormElement;

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'          => 'ZendFormFieldset',
        'options'       => array(
            'legend'        => 'App Dep',
        ),
        'elements'      => array(
            array(
                'spec' => array(
                    'name' => 'apDepTime',
                    'type' => 'ZendFormElementText',
                    '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'          => 'ZendFormFieldset',
        'options'       => array(
            'legend'        => 'App Arr',
        ),
        'elements'      => array(
            //apArrTime
            array(
                'spec' => array(
                    'name' => 'apArrTime',
                    'type' => 'ZendFormElementText',
                    'attributes' => array(
                        'required' => true,
                        'maxlength' => '5',
                        'id' => 'apArrTime',
                    ),
                    'options' => array(
                        'label' => 'Time',
                        'hint' => 'HH:MM',
                        'description' => 'UTC',
                    ),
                ),
            ),
        ),
    ));

    $this->add(new ElementCsrf('csrf'));

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

}
}

FlightDataFilter.php

<?php
namespace FcFlightFilter;

use ZendInputFilterFactory as InputFactory;
use ZendInputFilterInputFilter;
use ZendInputFilterInputFilterAwareInterface;
use ZendInputFilterInputFilterInterface;
use ZendDbAdapterAdapter;

class FlightDataFilter implements InputFilterAwareInterface
{

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

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

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

public $apDepTime;
public $apArrTime;

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

/**
 * @return endDbAdapterAdapter
 */
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 endInputFilterInputFilter|endInputFilterInputFilterInterface
 */
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(
                            endValidatorCallback::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;
}
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use the ZendValidatorCallback validator for this

The first parameter passed to your callback is the value of the input to which the validator is applied.

The second parameter is an array of your other form values keyed by input names and represents the context in which you want to compare values.

As a simple example...

    $apArrInputFilter->add($factory->createInput(array(
        'name' => 'apArrTime',
        'required' => true,
        'validators' => array(
                array(
                    'name' => 'Callback',
                    'options' => array(
                        'messages' => array(
                            endValidatorCallback::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;
                        },
                    ),
                ),
        ),
    )));

Obviously you'll need to write code to your needs for the actual date comparison, but that should get you started.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...