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 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.



20.4k
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.



383

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
            }
        }
    }