Ruben
Ruben

Reputation: 5095

Form element validation based on other form element

I'm rendering a form to add a class (course) to a database. The class has a certain starttime and endttime. Both are time of day fields. I created a fieldset for the class:

<?php
namespace Admin\Form;

use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterInterface;
use Zend\InputFilter\InputFilterProviderInterface;

class ArtClassFieldset extends Fieldset implements InputFilterProviderInterface
{
    public function __construct()
    {
        parent::__construct('artclass');

        $this->add(array(
            'name'          => 'dayofweek',
            'type'          => 'Zend\Form\Element\Select',
            'options'       => array(
                'label'             => 'Day of week:',
                'value_options'     => array(
                    1           => 'Monday',
                    2           => 'Tuesday',
                    3           => 'Wednesday',
                    4           => 'Thursday',
                    5           => 'Friday',
                    6           => 'Saturday',
                ),
            ),
        ));

        $this->add(array(
                'name' => 'starttime',
                'type' => 'Zend\Form\Element\Time',
                'options' => array(
                    'label' => 'Start time:',
                    'format' => 'H:ia',
                ),
            )
        );

        $this->add(array(
                'name' => 'endtime',
                'type' => 'Zend\Form\Element\Time',
                'options' => array(
                    'label' => 'End time:',
                    'format' => 'H:ia',
                ),
            )
        );

        $this->add(array(
                'name' => 'teacher',
                'type' => 'Admin\Form\TeacherSelectorFieldset',
                'options' => array(
                    'label' => 'Teacher:',
                )
            )
        );
    }

    public function getInputFilterSpecification()
    {
        return array(
                'dayofweek' => array(
                    'required' => true,
                    'filters' => array(
                        array('name' => 'Int'),
                    ),
                    'validators' => array(
                        array(
                            'name' => 'Between',
                            'break_chain_on_failure' => true,
                            'options' => array(
                                'min' => 1,
                                'max' => 6, 
                            ),
                        ),
                    ),
                ),
                'starttime' => array(
                    'required' => true,
                ),
                'endtime' => array(
                    'required' => true,
                ),
                'teacher' => array(
                ),
        );
    }
}

In my Form class I simply add this fieldset to my form:

<?php 
namespace Admin\Form;

use Zend\Form\Form;

class ArtClassAdd extends Form
{
    public function __construct()
    {
        parent::__construct("artclass-add");
        $this->setAttribute('action', '/admin/artclass/add');
        $this->setAttribute('method', 'post');

        $this->add(array(
                'type' => 'Admin\Form\ArtClassFieldset',
                'options' => array('use_as_base_fieldset' => true)
            )
        );

        $this->add(array(
                'name' => 'submit',
                'attributes' => array(
                    'type' => 'submit',
                    'value' => 'Save'                   
                )
            )
        );
    }
}

The format of the two time fields is 'H:ia' so that means I will get something like '11:00am'. What I would like to do now is to validate that the starttime is before the endtime. The question is how do I do that? I'm thinking I should probably use Zend\Validator\Callback, but not sure.

Upvotes: 0

Views: 658

Answers (1)

radnan
radnan

Reputation: 1289

You're on the right track. You have to use the Callback validator. Use something like this for the endtime input spec:

return array(
    'endtime' => array(
        'required' => true,
        'validators' => array(
            array(
                'name' => 'Callback',
                'options' => array(
                    'callback' => function($value, $context)
                    {
                        $endtime = DateTime::createFromFormat'H:ia', $value);
                        $starttime = DateTime::createFromFormat('H:ia', $context['starttime']);
                        return $endtime > $starttime;
                    }
                ),
            ),
        ),
    ),
);

Upvotes: 2

Related Questions