Select / Select advanced read-only/disabled

Support › General › Select / Select advanced read-only/disabled

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #38248
    Jean-Michel JunodJean-Michel Junod
    Participant

    Metabox AIO v.1.16.4
    Using Metabox Builder for custom fields for USER.
    Problems with read-only/disabled and saving values:

    Select field:
    read-only: it is still possible to change value and save it
    disabled: current value is lost after saving

    Select advanced (same):
    read-only: it is still possible to change value and save it
    disabled: current value is lost after saving

    Input field also loose value after saving if "disabled".

    Feature request for input/select fields:
    - property "protected" or "locked" that would replace the field value with just a <div class="rwmb-input">[value]</div> or something that would prevent user to inspect/edit page html and update disable/readonly attributes

    #38257
    sherwin_flightsherwin_flight
    Participant

    You can use some JavaScript to remove the disabled attribute when you save, and then the values will be saved as expected.

    But I think a read only/locked option would be great. I'm struggling with a similar issue right now as well, only with clone groups that i want to be read only.

    #38260
    Long NguyenLong Nguyen
    Moderator

    Hi,

    For the select field, you can use the setting disable to prevent the user from selecting the option and set the default value for the field to save the field value to the database. For example:

    [
        'name'     => __( 'Select', 'your-text-domain' ),
        'id'       => 'select_67b3inmhfgs',
        'type'     => 'select',
        'options'  => [
            'a' => __( 'A', 'your-text-domain' ),
            1   => __( '1', 'your-text-domain' ),
            2   => __( '2', 'your-text-domain' ),
            3   => __( '3', 'your-text-domain' ),
        ],
        'std'      => 2,
        'disabled' => true,
    ],
    #50222
    Ruth HutchinsRuth Hutchins
    Participant

    Hi wanted to check if there is an update on this?

    I have a select field which I do not want to be changed directly from the admin interface. But I do want the value to be visible.

    I set disabled = true as a custom attribute, which partially works, in the sense that the select box becomes visible but grayed out/can't be changed.

    However, then if I update anything else and submit, the select field is cleared. I do not want it to go back to a default value. I want it to keep its current value. What's the best way to do this?

    #50223
    PeterPeter
    Moderator

    Hello Ruth,

    Thanks for reaching out.

    The default value of the select field will be applied to a new post. If you want to change the value of a post, you can try:
    - remove disable attribute in the field settings
    - edit a post and select another value > save post
    - add the disable attribute again

    If it doesn't work, can you please share a screen record of the issue on your site?

    #50225
    Ruth HutchinsRuth Hutchins
    Participant

    Hey - That would probably work but wouldn't be a good UX for the users!

    What I ended up doing was leaving the field with disabled=true and adding a hidden input with the current value:

    `
    ... {
    add_filter( 'rwmb_status_end_html', array( $this, 'add_status_hidden_input' ), 10, 3 );
    }
    ...
    public function add_status_hidden_input( string $html, mixed $field, mixed $value ) {
    $html .= '<input type="hidden" name="status" value="' . esc_attr( $value ) . '">';
    return $html;
    }
    `

    Is there a reference for the allowed keys in $field[]? I think $field['id'] would work instead of hard coding the field name? $field['name'] is the label, right?

    #50226
    PeterPeter
    Moderator

    Hello,

    Yes, you can use the field setting ID as $field['id']. Here is the list of field settings https://docs.metabox.io/field-settings/

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.