Support

Account

Home Forums Add-ons Repeater Field Disabled/ReadOnly Field in repeater

Solving

Disabled/ReadOnly Field in repeater

  • Hi guys and girls,

    Hopefully, you can help me with this one. I currently have a repeater field that holds 2 sub-fields, Note and User. I have managed to get the User field to populate with the current user exactly as I wanted it. I have also managed to get any User field to be disabled/readonly once it is saved. So, if i save the post and come back, I cannot change the User related to the note, IDEAL! What I cannot figure out is how I can make that same User field dropdown disabled when it is created by clicking “Add Row”.

    I do not want the user to be able to pick any option other than their own name. I know I could use load_field or load_value to limit the options to just their user but it would be much easier to disable the field.

  • Hi,

    It would have been more simple to help if you posted the code you used to get the User field to populate and to disabled/readonly the sub-field once it is saved.

    To put you on the path (to be confirmed depending on your code) :

    1) Add a specific class to the sub-field in ACF back-end (like “always_disabled”) and add some javascript by using this selector to always disabled the field (see in ACF/WP documentation how to add it properly).

    2) Use the “prepare_field”/”load_value” functions to populate your sub-field value (as you probably done) and to set the field to “readonly” (required to avoid weird reaction of ACF when trying to save a JS disabled input)

    3) Save the sub-field value by using “update_value” / “save_post” or other function (because the disabled and “readonly” sub-field will not be saved automatically).

    It is not necessarily the ultimate solution, but it’s work for me. Hope it helps, send you code if you want more details =).

  • Hi
    You can use this code :

    function disable_message_load_field( $field ) {
    $field[‘readonly’] = 1;
    return $field;
    }
    add_filter(‘acf/load_field/name=sub_field_1’, ‘disable_message_load_field’);
    add_filter(‘acf/load_field/name=sub_field_2’, ‘disable_message_load_field’);

    to make your field read only. please note that disabled field are not saved and it is better to use radonly to keep your data.

    for inserting current user info in sub field you can use this code:

    global $current_user;
    get_currentuserinfo();
    $message_sender = $current_user->display_name;
    update_field(‘message_sender ‘, $message_sender, $post_id);

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

The topic ‘Disabled/ReadOnly Field in repeater’ is closed to new replies.