wordpress-fieldmanager
wordpress-fieldmanager copied to clipboard
Don't force `first_empty` for repeatable select fields if the first option is already empty
See #150. Sometimes it's helpful to manually include an option with a value of ''
so you can still label the default behavior. For example:
'limit' => 5,
'children' => [
'post_id' => new \Fieldmanager_Autocomplete( 'Post', [
'datasource' => new \Fieldmanager_Datasource_Post(),
] ),
'links_to' => new \Fieldmanager_Select( 'Links To', [
'options' => [
'' => 'Post permalink',
'url' => 'Custom URL,
],
] ),
'url' => new \Fieldmanager_Link( [
'display_if' => [
'src' => 'links_to',
'value' => 'url',
],
] ),
]
Currently, another blank option without a label will be added to the front of the <select>
.
One workaround is to include 'default_value' => ''
, which will cause the manually included empty value to be selected by default. Or, use a \Fieldmanager_Radios()
.
Hi @dlh01
This one should help to keep that empty option hidden, right?
'first_empty' => false
I'm doing this, but the empty option is always visible inside the dropdown:
'default_value' => array( 'default' ),
'first_empty' => false,
'options' => array(
'default' => 'Default',
'is-mini-compact' => 'Mini Compact',
'compact' => 'Compact',
),
Hey @jmolinanesn!
Is your field in a repeatable group? When a select field is part of a repeatable group, first_empty
is forced to be true
. That logic was added in #274.
If the field isn't part of a repeatable group, can you share the complete field code (anonymized if necessary)?
@dlh01 thanks for your feedback.
Yes, that field exists inside a repeatable group. Since the 'required' attribute does not work on a repeatable group either, there's no way to avoid the user saving empty values for that field.
Repeaters seems pretty broken...this and the required
field doesn't work either. It seems structural. Is this something that can be fixed, or is it just a known thing and that's that?
The problem this causes is that, when the empty value is selected and the panel is then saved, it crashes the WP admin and wipes the content of that option. Do you have any suggestions around this @dlh01 that can at least prevent the crash?
@theMikeD Can you provide the full field definition and let us know what version of Fieldmanager you're using?
Also, I assume that a "crash" in this case is a fatal error. If so, what's the error?