AdvancedCustomFields / acf

Advanced Custom Fields
http://advancedcustomfields.com/
835 stars 171 forks source link

ACF checkbox field not getting updated in wordPress rest API #830

Open raguvaranrceo opened 1 year ago

raguvaranrceo commented 1 year ago

My user edit rest api call working fine with ACF version 6.1.4. From 6.1.5 the check box field not getting update.

Response: { "code": "rest_invalid_param", "message": "Invalid parameter(s): acf", "data": { "status": 400, "params": { "acf": "acf[test][0] is not one of 0, 0, and 0." }, "details": { "acf": { "code": "rest_not_in_enum", "message": "acf[test][0] is not one of 0, 0, and 0.", "data": null } } } }

lgladdy commented 1 year ago

Hey @raguvaranrceo,

Could you please send us a screenshot of your checkbox field configuration in the field group? We made some changes in ACF 6.1.5 to support folks who use values like:

0: Zero
1: One
2: Two

But it seems like it must be causing some issues with your configuration.

raguvaranrceo commented 1 year ago

Hi, Thanks for your response. Kindly Find the screenshot link below. https://postimg.cc/cthCvVzt

lgladdy commented 1 year ago

Hey @raguvaranrceo,

Thanks so much for your bug report here. We've confirmed this is an issue when the checkbox doesn't contain any keys in the field values.

Attached is a patched version of the file: includes/fields/class-acf-field-checkbox.php, if you unzip this and drop it into your ACF install that should get you back up and running until we release the fix for this in our next release.

class-acf-field-checkbox.php.zip

Alternatively, you can manually update the get_rest_schema function in this file to:

        /**
         * Return the schema array for the REST API.
         *
         * @param array $field
         * @return array
         */
        public function get_rest_schema( array $field ) {
            $schema = array(
                'type'     => array( 'integer', 'string', 'array', 'null' ),
                'required' => isset( $field['required'] ) && $field['required'],
                'items'    => array(
                    'type' => array( 'string', 'integer' ),
                ),
            );

            if ( isset( $field['default_value'] ) && '' !== $field['default_value'] ) {
                $schema['default'] = $field['default_value'];
            }

            // If we allow custom values, nothing else to do here.
            if ( ! empty( $field['allow_custom'] ) ) {
                return $schema;
            }

            /**
             * If a user has defined keys for the checkboxes,
             * we should use the keys for the available options to POST to,
             * since they are what is displayed in GET requests.
             */
            $checkbox_keys = array_map(
                'strval',
                array_diff(
                    array_keys( $field['choices'] ),
                    array_values( $field['choices'] )
                )
            );

            // Support users passing integers for the keys as well.
            if ( empty( $checkbox_keys ) ) {
                $schema['items']['enum'] = $field['choices'];
            } else {
                $checkbox_keys           = array_merge( $checkbox_keys, array_map( 'intval', array_keys( $field['choices'] ) ) );
                $schema['items']['enum'] = $checkbox_keys;
            }

            return $schema;
        }

    }
raguvaranrceo commented 1 year ago

Hey, Many thanks for the quick fix.!.