Validation for required date input

Permalink
Hi All,

I am having some issues validation that a date picker value is required. Please see the code below for an example:

Form item:
<tr>
                        <td><?php echo $form->label("eDateStart", "Start Date"); ?></td>
                        <td><?php echo $dtt->datetime('eDateStart', $event->dtStartDate->format('Y-m-d H:i')); ?></td>
                    </tr>


Controller Code:
if ($this->isPost()) {
            $val = Loader::helper('validation/form');
            $val->setData($_POST);
            $val->addRequired('eHeading', 'The event heading is required');
            $val->addRequired('eDescription', 'The event description is required');
            $val->addRequired('eDateStart', 'The event start date is required');
            $val->addRequired('eDateEnd', 'The event end date is required');
            if ($val->test()) {
              // submit for saving
            } else {
                $this->set('error', $val->getError()->getList() ); 
            }


The issue is that event when my date item has a value, the validator thinks it is empty

 
jordanlev replied on at Permalink Reply
jordanlev
Many of the more complicated widgets (such as the date/time picker) actually write their values to a different hidden field in the dom. I'd do a little debugging, and add temporarily this to your controller function:
print_r($_POST);
die();

...then view the source of the page to see a list of all the items in the POST array. Perhaps there's one that jumps out at you as being related to the datetime picker, and you should use that field's name instead for the validation code?
jaconel007 replied on at Permalink Reply
Made use of my chrome dev tools to inspect the input box to retrieve the name, simply added the name into the validator chain... and validation work... :)