Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Field value is wiped on post save if Field attributes are disabled => disabled #346

Closed
kynetiv opened this issue Jun 3, 2015 · 6 comments

Comments

@kynetiv
Copy link

kynetiv commented Jun 3, 2015

I have a field with the attributes set to readonly and disabled:

$allow_ad_sync->add_field( array(
                'name' => __( 'Last Modified', 'cmb2' ),
                'desc' => __( 'Time last modified in LDAP', 'cmb2' ),
                'id'   => $prefix . 'last_modified',
                'type' => 'text_date_timestamp',
                'date_format' => 'm-d-Y @ h:i:s',
                'on_front' => false, // Optionally designate a field to wp-admin only
                'show_on_cb' => array( $this, 'upd_cbm2_is_user_admin' ), // function should return a bool value,
                'attributes'  => array(
                    'readonly' => 'readonly',
                    'disabled' => 'disabled',
                ),
            ) );

I populate the field value through a separate script and just wish to display it to users without giving them the ability to update it ( readonly and disabled ).

This issue here is that because it isn't passed along in the $_POST data ( I believe because it is marked as disabled in the DOM ) the value is set to null in CMB2_Field.php on line 429:
https://github.com/WebDevStudios/CMB2/blob/master/includes/CMB2_Field.php#L429

/**
     * Process $_POST data to save this field's value
     * @since  2.0.3
     * @param  array $data_to_save $_POST data to check
     * @return bool                Result of save
     */
    public function save_field_from_data( $data_to_save ) {

        $meta_value = isset( $data_to_save[ $this->id( true ) ] )
            ? $data_to_save[ $this->id( true ) ]
            : null;

        return $this->save_field( $meta_value );
    }

Setting it to null is then set as the value and wipes my field's value. Instead there probably should be a check for disabled fields and not apply any update. The benefit of adding a conditional here would disallow tampering with the DOM (although there are probably other checks for that already).

As a workaround I've added a hook to the override filter here:
https://github.com/WebDevStudios/CMB2/blob/master/includes/CMB2_Field.php#L290
for my specific field_id on the filter (I may do one more generic for disabled fields in general, but right now I only have the one field):

add_filter( 'cmb2_override_{_my_prefix_and_field_id}_meta_remove', array($this, 'upd_dont_remove_disabled_field'), 10, 4 );

    public function upd_dont_remove_disabled_field($override, $args, $field_args, $field_object) {
        $override = true;
        return $override;
    }

This override filter above fixes the issue but disabled fields probably shouldn't be updated at all or require a filter like this.

@kynetiv kynetiv changed the title Field values are wiped on post save if Field attributes are disabled => disabled Field value are wiped on post save if Field attributes are disabled => disabled Jun 3, 2015
@kynetiv kynetiv changed the title Field value are wiped on post save if Field attributes are disabled => disabled Field value is wiped on post save if Field attributes are disabled => disabled Jun 3, 2015
@jtsternberg
Copy link
Member

Some good thoughts here. As an alternative, you can set the 'default'attribute to the value of the field (saved elsewhere/different meta key). Then that value will always display even though it doesn't get saved. the 'default' field attribute can also take a callback, so you can fetch that value at the last minute.

@modemlooper
Copy link

modemlooper commented Jun 21, 2016

I'm having this issue on repeatable group fields. I have a group of fields and if they choose a specific option from the select js disables other fields. The fields have default values but they get wiped out on save.

I think i might have to resort to hidden fields so the value can't be changed

@jtsternberg
Copy link
Member

readonly and disabled attributes are used for specific and varied purposes, and I'm not sure there is a good "standard" solution that should be applied here, but I am open to ideas.

@modemlooper hidden fields could work, but you could also possibly remove the disabled attribute via JS when saving. That may do it. You could also use a filter like @kynetiv.

Unless you guys have a specific idea as to how this be handled by default (different than it is), I think we should close this issue.

@jamesgol
Copy link
Contributor

I submitted that PR last year that covers the situation.

@jtsternberg
Copy link
Member

If anyone is up for submitting a PR to introduce a field property 'save_field', that could be a decent solution. (see more)

jamesgol added a commit to jamesgol/CMB2 that referenced this issue Jun 28, 2016
Keeps specific fields from being saved.  Defaults to true, if set to false field will be skipped.

Related to CMB2#346 and CMB2#500
jtsternberg added a commit that referenced this issue Jun 28, 2016
 #500

For disabling the saving of a field. Useful if you want to display the
value of another field, or use a disabled/read-only field.

See example in example-functions.php
jamesgol added a commit to jamesgol/CMB2 that referenced this issue Jun 28, 2016
@jtsternberg
Copy link
Member

#674

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants