Validation of field 1 or field 2

Support General Validation of field 1 or field 2

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #7717
    Infolu OfficialInfolu Official
    Participant

    Hello, I have 2 fields of type text.
    Each field is displayed using the MB 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.

    #7751
    Truong GiangTruong 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 OfficialInfolu Official
    Participant

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

    #7755
    Tan NguyenTan 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 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.