alleyinteractive / wordpress-fieldmanager

Custom field types for WordPress
Other
532 stars 101 forks source link

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

Open dlh01 opened 6 years ago

dlh01 commented 6 years ago

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

jmolinanesn commented 4 months ago

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',
    ),
dlh01 commented 4 months ago

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

jmolinanesn commented 4 months ago

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

theMikeD commented 4 months ago

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 commented 4 months ago

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?

dlh01 commented 4 months ago

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