Support

Account

Home Forums Bug Reports acf/prepare_field not disabling if coupled with conditional logic

Helping

acf/prepare_field not disabling if coupled with conditional logic

  • I have a small bit of code that will disable some fields if a value is set.

    My use case for example, is allowing my client to fill out a field and once it’s saved with a value, to prevent them ever changing its value again.

    I use the following in the functions.php

    add_filter('acf/prepare_field/key=field_57f39abd0f7d5', 'disable_acf_prepare_field');
    
    function disable_acf_prepare_field( $field ) {
      if( $field['value'] ) {
          $field['disabled'] = true;
      }
      return $field;
    }

    This works as expected and the fields are disabled after saving a value.

    However, if I then update the field rules to have conditional logic based on another radio button, the field is no longer disabled, regardless of having a value or not.

    Removing the conditional logic brings this functionality back to a working state.

    WordPress: 4.7.3
    ACF Pro: 5.5.11

  • Conditional logic does two things. It hides the field from view and disables the field so that it is not submitted. The conditional logic is seeing that the field should be shown and enabled and removing the disabled state in the JavaScript for conditional logic.

    If you want this field to be visible even though it cannot be edited, then I don’t think there is any solution. The only think I can think of is to make it read only as well.

    If you can get away with removing the field completely once it’s filled in you can return false from your filter and this will remove the field from display. Another option is that you can actually change the field type in your filter and change it to a message field, if you do this then there will be no field do edit.

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘acf/prepare_field not disabling if coupled with conditional logic’ is closed to new replies.