Readonly attribute under condition

Support General Readonly attribute under conditionResolved

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #24620
    ben06ben06
    Participant

    Hi,

    I created a password type metabox.
    How can I add the attribute 'readonly' only if a condition is approved ?

    
    'id'         => $prefix . 'id',
    'name'       => "Lorem",
    'type'       => 'password',
    'readonly'   => true,       //Under conditions
    

    Thank you

    #24633
    Long NguyenLong Nguyen
    Moderator

    Hi Ben,

    If the condition happens on loading, you can just check the condition when registering the meta box

    if( condition ) {
        $mark = true
    }
    $prefix = '123';
    ...
    'readonly' => $mark,
    

    If the condition happens on live (click/type), you need to use some JavaScript code to add the readonly attribute after binding an action. Follow this topic for more information https://stackoverflow.com/questions/18690463/how-to-add-the-readonly-attribute-with-jquery-or-javascript-but-following-the-w.

    #24638
    ben06ben06
    Participant

    Hi,

    Thank you for your answer.
    Where do I have to add the condition ?
    The condition is : if post_status = publish {$mark = true}

    
    $meta_boxes[] = [
            'title'      => esc_html__( 'Signature', 'text-domain' ),
            'id'         => 'signature',
            'post_types' => ['docs'],
            'context'    => 'normal',
            'priority'   => 'high',
            'fields'     => [
                [
                    'id'              => $prefix . 'signature_appli',
                    'name'            => "Signature",
                    'type'            => 'password',
                ],
            ],
            'validation' => array(
                'messages' => array(
                    'signature_appli' => array(
                        'required'   => 'You have to sign.',
                    ),
                ),
            ),
            'visible' => array(
                'when' => array(
                     array( 'post_status','=', 'attente_app'),
                     array( 'post_status','=', 'publish')
                 ),
                 'relation' => 'or'
            ),
        ];
    
    #24642
    Long NguyenLong Nguyen
    Moderator

    Hi,

    Before declaring the variable $meta_box, just like the variable $prefix that I've noted above.

    $mark = false;
    if( condition ) {
        $mark = true
    }
    $prefix = '123';
    $meta_boxes[] = [
            'title'      => esc_html__( 'Signature', 'text-domain' ),
            'id'         => 'signature',
            'post_types' => ['docs'],
            'context'    => 'normal',
            'priority'   => 'high',
            ...
    
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.