Readonly attribute under condition
- This topic has 3 replies, 2 voices, and was last updated 4 years, 8 months ago by
Long Nguyen.
-
AuthorPosts
-
February 24, 2021 at 11:55 PM #24620
ben06
ParticipantHi,
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 conditionsThank you
February 25, 2021 at 11:25 AM #24633Long Nguyen
ModeratorHi 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
readonlyattribute 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.February 25, 2021 at 5:33 PM #24638ben06
ParticipantHi,
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' ), ];February 25, 2021 at 10:17 PM #24642Long Nguyen
ModeratorHi,
Before declaring the variable
$meta_box, just like the variable$prefixthat 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', ... -
AuthorPosts
- You must be logged in to reply to this topic.