I added a built-in select list field to a content type but left the allowed values field setting empty, since I'ld like to get the possible options to choose from dynamically.
To be precise I'ld like to offer the editors a list of files inside a certain directory to choose from. This list can grow and I don't like to touch the content type and adjust the allowed values field setting every time a new file gets added to that directory. (Editing it afterwards may even not be possible since after having values for that field in the database when nodes got added you are not allowed to change field settings anymore.)
So here I am, having the list of files added to the form widget's options array, but now I'm unable to save the selected value as it's not in the list of allowed values.
The value you selected is not a valid choice.
OK I thought, let's simply uncheck the element validation:
unset($form['field_MYFIELD']['widget']['#element_validate']). As far as I can remember this worked in D7. Makes the problem even bigger.
TypeError: Argument 1 passed to Drupal\Core\Field\WidgetBase::massageFormValues() must be of the type array, string given, called in /Users/Shared/www/Documents/MYREPO/web/core/lib/Drupal/Core/Field/WidgetBase.php on line 370 in Drupal\Core\Field\WidgetBase->massageFormValues() (line 505 of core/lib/Drupal/Core/Field/WidgetBase.php).
Is there a simple way to unset this element validation here? Or should I introduce another custom select list field and save the chosen value into a hidden textfield, simply?