3
votes

Trying to use a DateTime Form element in ZF2 and cannot valid the form.

$inputFilter->add(array(
                'name'     => 'event_datetime',
                'required' => true,
                'filters'  => array(
                        array('name' => 'StringTrim'),
                ),
                'validators' => array(
                        array(
                                'name'    => 'StringLength',
                                'options' => array(
                                        'encoding' => 'UTF-8',
                                        'min'      => 0,
                                        'max'      => 20,
                                ),
                        ),
                ),
        ));

Using this on the .phtml file.

    <?php $formElement = $form->get('event_datetime');?>
<dt><?php echo $this->formLabel($formElement);?></dt>
<dd><?php echo $this->formDateTimeLocal($formElement);?>
<?php echo $this->formElementErrors($formElement);?>

NOTE: using formDateTimeLocal instead of formDateTime as the latter does not show the HTML5 elements. Using Chrome the HTML5 DateTimeLocal field appears with a calendar and Time section.

When running $form->isValid() I receive: (var_dump($form->getMessages()))

array (size=1) 'event_datetime' => array (size=1) 'dateInvalidDate' => string 'The input does not appear to be a valid date' (length=44)

The getRequest->getPost() = public 'event_datetime' => string '2015-08-10T03:00' (length=16)

I've tried to split this field into 2: a Date and a Time field as separate variables. This works correctly for the Date BUT not for the Time element. Reading around I've noticed this: ZF2 validating date and time format PT_BR always The input does not appear to be a valid date which does not help as I need the time component. (obviously I have looked at more than just 1 link but my rep on SO allows only 1 url in post.) I've also read that Chrome and Opera cut off the 'seconds' part of the time field.... How to I validate either a \Zend\Form\Element\DateTime field or just the \Zend\Form\Element\Time for field...

I've tried to manually glue these together, add the :00 seconds part of the string to Time but to no effect.

If I set the input filter to 'required' => false I still receive the dateInvalidDate validator for for attempts: DateTime and Time...

So, the question is:

How do I validate a DateTime or Time field using Zf2 form elements and inputFilters. Following the Docs and example don't seem to work for me and manually creating the Time string also has the same issue.

2

2 Answers

1
votes

Try this:

$inputFilter->add(array(
            'type' => 'Zend\Form\Element\DateTimeLocal',
            'name'     => 'event_datetime',
            'required' => true,
            'options' => array(
                'label'  => 'Appointment Date',
                'format' => 'Y-m-d\TH:i'
            ),
            'filters'  => array(
                    array('name' => 'StringTrim'),
            ),
            'validators' => array(
                    array(
                            'name'    => 'StringLength',
                            'options' => array(
                                    'encoding' => 'UTF-8',
                                    'min'      => 0,
                                    'max'      => 20,
                            ),
                    ),
            ),
    ));

You get the error, because the datetime string/format you pass is different than the expected datetime format by default. Try playing with 'format' => 'Y-m-d\TH:i' to get the result.

Taken directly from Zend documentation. It's all the same, but with a different element.

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

$time = new Element\Time('time');
$time
->setLabel('Time')
->setAttributes(array(
    'min'  => '00:00:00',
    'max'  => '23:59:59',
    'step' => '60', // seconds; default step interval is 60 seconds
))
->setOptions(array(
    'format' => 'H:i:s'
));

$form = new Form('my-form');
$form->add($time);
0
votes

My original issue was validation. The suggestion by Stanimir did help and the dateTimeLocal format has great in pointing me in the right direction. The whole issue was with the 'format' value.

My main problem was that when populating the \Zend\Form\Element\Time field the format was H:i:s but the HTML5 form only submitted H:i. (also due to my 'format' setting which is OK) So, when populating the form the DB field returned H:i:s which populated the form correctly BUT on submission failed IF I didn't edit the Time field.

THEREFORE: the answer to this questions is basically make sure the format submitted [and $form->bind($object), $form->setData($post) etc] is EXACTLY the same as the form element definition [H:i != H:i:s] and when pulling from database format to correspond to your required setting.

var_dump($form->get('valid_to_time')->getFormat());
var_dump($form->get('valid_to_time')->getValue());

Once this is the same all will be well and you can split DateTime fields into individual Date and Time (or use DateTime as above). Sounds simple but this was a headache to get right.