We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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



5.1k
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



5.1k

Solved

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