silverstripe / silverstripe-framework

Silverstripe Framework, the MVC framework that powers Silverstripe CMS
https://www.silverstripe.org
BSD 3-Clause "New" or "Revised" License
720 stars 823 forks source link

Form field attributes aren’t included in form schema #9124

Open kinglozzer opened 5 years ago

kinglozzer commented 5 years ago

FormField::getSchemaDataDefaults() by default returns an empty array for the field’s attributes (third-last item in the below array):

https://github.com/silverstripe/silverstripe-framework/blob/29a663c65d1efa819ffbbb5f964c33bd2f10780d/src/Forms/FormField.php#L1538-L1560

I noticed this when adding a new field to GridFieldFilterHeader, any attributes I add to the field go missing as it’s built using the form schema. Workaround is to just use ->setSchemaData() to add the attributes manually

Changing it to 'attributes' => $this->getAttributes(), has the desired effect. Edit: that won’t work because recursion https://github.com/silverstripe/silverstripe-framework/pull/9125#issuecomment-511347274

micschk commented 2 years ago

Temporary workaround (eg for placeholder attribute): $field->setSchemaData(['attributes' => ['placeholder' => 'My placeholder value']]);

GuySartorelli commented 3 months ago

I think the way forward for this is in a major release call getAttributes() from getSchemaDataDefaults(), and make sure to update any core and supported modules to not call getSchemaData() in getAttributes() (e.g. TreeDropdownField - those data attributes should be added in the template directly instead, perhaps)