Zf2 Form Validator DateTime dateInvalidDate

Tries to use a DateTime form element in ZF2 and cannot use 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 in a .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 render HTML5 elements. Using Chrome, an HTML5 DateTimeLocal field appears with a calendar and a time section.

When I run $ form-> isValid () I get: (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)

      

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

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

I tried to manually glue them together by adding the: 00 second part of the string to the time, but with no effect.

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

So the question is:

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

+3


source to share


2 answers


Try the following:

$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 are getting an error because the date and time string / format being passed is different from the expected default date and time format. Try playing with 'format' => 'Y-m-d\TH:i'

to get results.



Taken directly from Zend documentation . It's 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);

      

+1


source


My original problem was validation. Stanimir's suggestion really helped, and the dateTimeLocal format makes a big difference in pointing me in the right direction. The whole problem was the meaning of the format.

My main problem was that when filling in the \ Zend \ Form \ Element \ Time field, the format was H: i: s, but the HTML5 form only represented H: i. (also due to my setting "format" which is ok) So when I filled out the form, the DB field returned H: i: s, which filled out the form correctly, but it was not completed on submission, UNLESS I edited the Time field ...

SO: The answer to this question is basically make sure the format represented by [and $ form-> bind ($ object), $ form-> setData ($ post), etc.] EXACTLY matches the definition of the form element [H : i! = H: i: s] and when pulling from the database format according to the desired setting.



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

      

Once it is the same everything is fine and you can split the DateTime fields into separate dates and times (or use DateTime as above). Sounds simple, but it was a headache to get right.

0


source







All Articles