Support Forum
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
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.
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'
),
];
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',
...