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 Type

Ok there is getName and getLabel for form elements, but is there something for getType()?

I am using a loop and when it is a checkbox I want it to do something else.



33.8k
Accepted
answer

You'll have to use this:

if (strcmp($myCheckbox->getAttribute('type'), 'check') === 0) {
    // Do stuff...
}

I am unable to replicate this instruction in Phalcon 2.0.8:

{% for element in my_form %}
    {{ element.getAttribute('type')|json_encode }}
    {% if element.getAttribute('type') == 'hidden' %}
        {{ element.render() }}
    {% endif %}
{% endfor %}

The getAttribute('type') returns null for every element in my form.



726

My solution is:

    public function beforeValidation($data, $entity, $messages)
    {
        $elements = $this->getElements();
        foreach ($elements as $element){
            $is_checkbox = is_a($element, 'Phalcon\Forms\Element\Check');
            if ($is_checkbox){
                // do stuff
            }
        }
    }