Custom Fields
Re-Envisioned
Support General Validation of field 1 or field 2

This topic contains 3 replies, has 3 voices, and was last updated by  Tan Nguyen 1 year, 6 months ago.

  • Creator
    Topic
  • #7717

    Infolu
    Participant

    Hello, I have 2 fields of type text.
    Each field is displayed using the Meta Box Conditional Logic.

    How can I use validation in case the field 1 is filled it does not complain that field 2 has nothing? or on the contrary if I fill in field 2 do not complain that field 1 is empty?

    Because I want to display a third field based on the condition above or field1 or field2 is filled.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Author
    Replies
  • #7751

    Truong Giang
    Participant

    Hi there,

    Do you mean that the third field will be displayed when at least first or second field is not empty?

    Thanks.

    #7754

    Infolu
    Participant

    Hello Truong Giang, exactly the precise, display a third field only if field 1 or field 2 is with data.

    #7755

    Tan Nguyen
    Participant

    Hey Infolu,

    This can be achieve by using Compound Statements in Conditional Logic.
    https://docs.metabox.io/extensions/meta-box-conditional-logic/#compound-statements

    Here is my example, the field Address will visible when Full Name or Email has filled.

    
    add_filter( 'rwmb_meta_boxes', function ( $meta_boxes ) {
    
    	$meta_boxes[] = array(
            'id'         => 'personal',
            'title'      => 'Personal Information',
            'post_types' => 'post',
            'context'    => 'normal',
            'priority'   => 'high',
    
            'fields' => array(
                array(
                    'name'  => 'Full Name',
                    'desc'  => 'Full name',
                    'id'    => 'name',
                    'type'  => 'text',
                ),
                array(
                    'name'  => 'Email',
                    'desc'  => 'Email Address',
                    'id'    => 'email',
                    'type'  => 'email',
                ),
                array(
                	'name' => 'Address',
                	'desc' => 'Address',
                	'id' => 'Address',
                	'type' => 'text',
                	'visible' => [
                		'when' => [
                			['name', '!=', ''],
                			['email', '!=', '']
                		],
                		'relation' => 'or'
                	]
                )
            )
    	);
    
    	return $meta_boxes;
    } );
    
Viewing 3 replies - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.