Adding a new validSelectAttribute to the Zend-form Select Element


#1

Hi,
I’m a junior programmer who is fairly new to zend-expressive.
I have a zend-form with a Select dropdownlist with multiple set, I also wanted to be able to search through the list.
I run into an issue where the framework was stripping away the attribute (‘searchable’) from the form Select element, because it was not listed as a part of the zend-form SelectForm.php:
protected $validSelectAttributes = [
‘name’ => true,
‘autocomplete’ => true,
‘autofocus’ => true,
‘disabled’ => true,
‘form’ => true,
‘multiple’ => true,
‘required’ => true,
‘size’ => true,
],

When I add the searchable attribute directly to the files in the framework like this (‘searchable’ => true). It works, but that is breaching the OOP principle of encapsulating and hiding properties by declaring them private. I tried to create a custom select from the Element\Select, but there is no method to addValidSelectAttributes.

I concluded that for the sake of time and finishing the task, I’ll use JavaScript to add the searchable attribute.

Is there away to do this easily in Zend?

Thanks for your help.


#2
  1. It is not listed because it is not an existing or valid HTML attribute.
  2. You can add custom attributes with the method addValidAttribute. More infos in the documentation: https://docs.zendframework.com/zend-form/helper/abstract-helper/#allowing-framework-specific-attributes

#3

Thanks for the answer, I looked at the zend framework and it does have the addValidAttribute() and the addValidAttributePrefix(). So some reason the zend-expressive framework I’m using for work doesn’t have those. I’m assuming it designed that way by choice of the seniors who set it up. I did manage to add the searchable attribute through jQuery.