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

Unchecked checkbox in repeating group won't save

Open scottnelle opened this issue 8 years ago • 2 comments

If you create a checkbox with a default value true (checked) within a repeating field group, Fieldmanager fails to save the checkbox state when unchecked, even if 'save_empty' is set true.

You can work around this issue by setting 'false_value' to 'no', however setting 'false_value' to 0 does not work around the issue.

A test implementation follows. The desired outcome is that when you uncheck an instance of the checkbox 'in_repeating_group' and save the post, the fact that the box was not checked should be saved.

function ai_fm_checkbox_test() {
    $fm = new Fieldmanager_Group( array(
        'name' => 'checkbox-test',
        'serialize_data' => false,
        'add_to_prefix' => false,
        'children' => array(
            'not_in_group' => new Fieldmanager_Checkbox( array(
                'label' => __( 'Not in a group', 'ai' ),
                'default_value' => true,
                'save_empty' => true,
                'description' => __( 'This saves fine', 'ai' ),
            ) ),
            'group' => new Fieldmanager_Group( array(
                'label' => __( 'Group', 'ai' ),
                'children' => array(
                    'in_group' => new Fieldmanager_Checkbox( array(
                        'label' => __( 'In a group', 'ai' ),
                        'default_value' => true,
                        'save_empty' => true,
                        'description' => __( 'This saves fine', 'ai' ),
                    ) ),
                ),
            ) ),
            'repeating_group' => new Fieldmanager_Group( array(
                'label' => __( 'Repeating Group', 'ai' ),
                'limit' => 0,
                'children' => array(
                    'in_repeating_group' => new Fieldmanager_Checkbox( array(
                        'label' => __( 'In a group', 'ai' ),
                        'default_value' => true,
                        'save_empty' => true,
                        'description' => __( 'This checkbox does not save if unchecked', 'ai' ),
                    ) ),
                    'text' => new Fieldmanager_TextField( array(
                        'label' => __( 'Text field', 'ai' ),
                        'description' => __( 'Even if the group is not empty, an unchecked checkbox does not save.', 'ai' ),
                    ) ),
                ),
            ) ),
        ),
    ) );
    $fm->add_meta_box( __( 'Unsaved checkbox test', 'ai' ), array( 'post' ), 'normal', 'default' );
}
add_action( 'fm_post_post', 'ai_fm_checkbox_test' );

scottnelle avatar Mar 16 '16 19:03 scottnelle

This appears to be similar to the bug reported in #553, @dlh01 created #557 to resolve.

gfargo avatar Jul 04 '17 18:07 gfargo

When I set 'unchecked_value' => '0' in the test implementation, the "unchecked" state is saved as expected. It's possible that that started working after #557.

Has the ship sailed on using '1' and '0' for the checked_value and unchecked_value in Fieldmanager_Checkbox? That would make the database representation unambiguous.

dlh01 avatar Mar 10 '21 01:03 dlh01