Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Form Element date example

Hi Good luck with my English, i am French

I just start with Phalcon, it's great, i found many hellp in tutorials, but i need example to create Date field (Birthday date) in a form how to use Form element date and having date in DD/MM/YYYY format

Thanks



3.2k
Accepted
answer
edited May '17

I had the solution but it was hidden behind another error

in my BaseForm.php


    // Inintialise date field
    protected function setFieldDate($table, $field, $mandatory = false)
    {
        $validate = array();

        $dateField = new Date($field);
        $dateField->setLabel($this->_($table . ucfirst($field)));
        $dateField->setFilters(array('striptags', 'string'));
        $dateField->setAttribute('placeholder', $this->_('help_' . $table . ucfirst($field)));
        if ($mandatory)
        {
            $dateField->setAttribute('required', 'true');
            $dateField->setAttribute('aria-required', 'true');
            array_push($validate, new PresenceOf(array(
                    'message' => $this->_($table . ucfirst($field) . 'Required')
            )));
        }
        if (sizeof($validate) > 0) $dateField->addValidators($validate);

        return $dateField;
    }

in my personForm.php


        $birthdate = $this->setFieldDate("person", "birthdate", false);
        $this->add($birthdate);

in my view


        <div class='form-group'>
            <div class="col-md-3 offset-3 form-label">
                {{ form.label('birthdate') }}
            </div>
            <div class="col-md-2">
                {{ form.render('birthdate', ["class": "form-control"]) }}
                {{ form.messages('birthdate') }}
            </div>
        </div>

Default setters and getters in Model and it work

Date field



3.2k

Solved

I just add validator in my setFieldDate m├ęthod to control manal imputs