Phalcon Forms component attributes support in version 4.2 shifts ARIA responsibility to developers
0 reputation · 04 Oct 2020, 06:54 UTC
0 reputation · 04 Oct 2020, 06:54 UTC
Determine whether Phalcon’s Forms component should automatically generate sensible ARIA attributes (such as aria‑label) for form elements when the optional attributes array is not supplied, preserving backward compatibility while improving out‑of‑the‑box accessibility.
Since version 4.2 introduced the attributes array, developers must manually add ARIA properties; otherwise no ARIA markup is rendered, leaving accessibility to individual implementation and risking inconsistent screen‑reader support.
Should the framework inject a default aria‑label derived from the field’s label or name when attributes are empty? What fallback strategy should be used for elements without a visible label? How can this behavior be configured or disabled per project?
32775 reputation · 04 Oct 2020, 12:04 UTC
Phalcon's Forms component in version 4.2 does not automatically inject ARIA attributes when the optional attributes array is omitted; the developer must supply them.
Automatically deriving an aria‑label from the field's label or name could override intentional accessibility attributes set by the developer and would break backward compatibility for projects that rely on the current behaviour.
getLabel();
if ($label !== null) {
$attributes['aria-label'] = $label;
} elseif ($this->getName() !== null) {
$attributes['aria-label'] = $this->getName();
}
}
return $attributes;
}
}
Because the behaviour is implemented in a custom element class, you can enable or disable it per project simply by choosing whether to use the custom element or the default Phalcon element. No global switch exists.
Do you already have a custom base form element class in your project? If you do, you can add the fallback logic there; otherwise you will need to create one as shown above.
Use comments to ask for clarification. Post a solution as an answer.
32,775 reputation · 04 Oct 2020, 13:28 UTC
When extending a form element to add a fallback aria-label, first retrieve the existing attributes via parent::getAttributes() (or $this->getAttributes() if you haven’t overridden it yet). Then, only if the returned array is empty, merge in the fallback:
public function getAttributes()
{
$attributes = parent::getAttributes();
if (empty($attributes)) {
$label = $this->getLabel();
if ($label === null) {
$label = $this->getName();
}
if ($label !== null) {
$attributes['aria-label'] = $label;
}
}
return $attributes;
}
This approach preserves any explicitly supplied attributes (such as class or id) while still providing a sensible default when the developer omits the attributes array.