wordpress-fieldmanager icon indicating copy to clipboard operation
wordpress-fieldmanager copied to clipboard

Don't force `first_empty` for repeatable select fields if the first option is already empty

Open dlh01 opened this issue 7 years ago • 6 comments

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().

dlh01 avatar Feb 08 '18 18:02 dlh01

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',
	),

jmolinanesn avatar Apr 22 '24 20:04 jmolinanesn

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 avatar Apr 23 '24 07:04 dlh01

@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.

jmolinanesn avatar Apr 23 '24 11:04 jmolinanesn

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?

theMikeD avatar Apr 23 '24 12:04 theMikeD

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 avatar Apr 23 '24 12:04 theMikeD

@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?

dlh01 avatar Apr 23 '24 17:04 dlh01