Title: Complex field with relations, adding extra rows to db
Last modified: September 1, 2016

---

# Complex field with relations, adding extra rows to db

 *  Resolved [martnick](https://wordpress.org/support/users/martnick/)
 * (@martnick)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/complex-field-with-relations-adding-extra-rows-to-db/)
 * Hello, I have been using your plugin for quite some time now. And started making
   some advanced things with it. I have a complex field with selects, that hide 
   and show different fields dependent on the selected value. But in the DB it saves
   even the hidden fields as rows.
 *  Some code:
    Creating a theme option container
 *     ```
       $roles = carbon_get_theme_option('soscenter_roles','complex');
   
       $dynamic_roles_container = Container::make('theme_options','Plugin Roles Options')->set_page_parent('Plugin Options');
       ```
   
 * Going through roles
 *     ```
       for($i = 0; $i < count($roles); $i++) {
       	$dynamic_roles_container->add_fields(generate_fields_array($roles[$i]['soscenter_role_name'], $roles[$i]['soscenter_display_name']));
       }
       ```
   
 *     ```
       function generate_fields_array($role_code, $role_display) {
       	$field_types = array(
       		'text' => 'Text Field',
       		'select' => 'Select');
   
       	/*
       	 * Make a field with role name as seperator text
       	 * Add complex field to which we will add fields later
       	 * Adding select with field options to it
       	 */
       	$seperator = Field::make('separator',$role_code . 'role_separator',$role_display);
       	$complex_part = Field::make('complex',$role_code . '_add_field','Role Fields');
       	$complex_part_fields_array = array();
       	$types_select = Field::make('select',$role_code . '_field_type','Field Type')->add_options($field_types);
   
       	$complex_part_fields_array[] = $types_select;
   
       	/*
       	 * Going through field options
       	 */
       	foreach($field_types as $key => $value) {
       		$temp_field_array = array();
   
       		/*
       		 * Creating different fields to handle a specific role field
       		 */
       		switch($key) {
       			case 'text':
       				$field_code = $role_code . '_' . $key . '_';
       				$temp_field_array[] = Field::make('text',$field_code . 'label','Label');
       				$temp_field_array[] = Field::make('text',$field_code . 'code','Code Name')->set_required(true);
       				$temp_field_array[] = Field::make('text',$field_code . 'placeholder','Placeholder Value');
       				$temp_field_array[] = Field::make('text',$field_code . 'default_value','Default Value');
       			break;
       			case 'select':
       				$field_code = $role_code . '_' . $key . '_';
       				$temp_field_array[] = Field::make('text',$field_code . 'label','Label');
       				$temp_field_array[] = Field::make('text',$field_code . 'code','Code Name')->set_required(true);
       				$temp_field_array[] = Field::make('complex',$field_code .'options')->add_fields(array(
       					Field::make('text',$field_code . 'option_key', 'Key')->set_width(50)->set_required(true),
       					Field::make('text',$field_code . 'option_value', 'Value')->set_width(50)->set_required(true),
       				));
       			break;
       		}
   
       		/*
       		 * Making fields conditions to be displayed only on the specific select value
       		 */
       		$relations_array = array(
       			'relation' => 'AND',
       			array(
       				'field' 	=> $role_code . '_field_type',
       				'value' 	=> $key,
       				'compare' 	=> '=',
       			)
       		);
   
       		foreach($temp_field_array as $field_to_be_added) {
       			$field_to_be_added->set_conditional_logic($relations_array);
       			$complex_part_fields_array[] = $field_to_be_added;
       		}
       	}
   
       	/*
       	 * Returns the specific field generated for role
       	 */
       	return array(
       		$seperator,
       		$complex_part->add_fields($complex_part_fields_array));
       }
       ```
   
 * And for a test role in the db it is added like this:
    test_add_field_-test_field_type_0
   test_add_field_-test_text_label_0 test_add_field_-test_text_code_0 test_add_field_-
   test_text_placeholder_0 test_add_field_-test_text_default_value_0 test_add_field_-
   test_select_label_0 test_add_field_-test_select_code_0
 * [https://wordpress.org/plugins/carbon-fields/](https://wordpress.org/plugins/carbon-fields/)

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Plugin Author [htmlBurger](https://wordpress.org/support/users/htmlburger/)
 * (@htmlburger)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/complex-field-with-relations-adding-extra-rows-to-db/#post-7681753)
 * Hi [@martnick](https://wordpress.org/support/users/martnick/),
 * This behavior is by design. The Conditional functionality is meant to be used
   only to improve User Experience.
 * Imagine the following use-case.
 * You need to add Credentials options for a service, which has both “Sandbox” and“
   Live” environments, e.g. PayPal. For both of these environments, you will have
   two options “Client ID” and “Secret Key”. In addition to these, you have another
   option “Choose Environment”.
 *     ```
       function crb_environment_conditional_logic( $environment ) {
           return array(
               array(
                   'field' => 'paypal_environment',
                   'value' => $environment,
               ),
           ),
       }
   
       Container::make( 'theme_options', 'PayPal Settings' )
           ->add_fields( array(
               Field::make( 'select', 'paypal_environment', 'Choose Environment' )
                   ->add_options( array(
                       'live'    => 'Live',
                       'sandbox' => 'Sandbox',
                   ) ),
   
               Field::make( 'text', 'paypal_live_clientid', 'Live Client ID' )
                   ->set_conditional_logic( crb_environment_conditional_logic( 'live' ) ),
               Field::make( 'text', 'paypal_live_secret_key', 'Live Secret Key' )
                   ->set_conditional_logic( crb_environment_conditional_logic( 'live' ) ),
   
               Field::make( 'text', 'paypal_sandbox_clientid', 'Sandbox Client ID' )
                   ->set_conditional_logic( crb_environment_conditional_logic( 'sandbox' ) ),
               Field::make( 'text', 'paypal_sandbox_secret_key', 'Sandbox Secret Key' )
                   ->set_conditional_logic( crb_environment_conditional_logic( 'sandbox' ) ),
           ) );
       ```
   
 * So, basically if you change the Environment, only the associated Credentials 
   options will be displayed. However, if the Conditional Logic stores only the 
   visible fields in Database, the Administrator will have to update the fields 
   every time He wants to test in Sandbox and again when switching to Live, which
   will be inconvenient.
 * In addition, you will still have to perform a check in your code to get the correct
   Environment Credentials. Not storing the invisible options it Database won’t 
   affect your code.
 * —-
 * However, given your example, it makes more sense to use two Complex Groups instead
   of conditional logic. This will look like this:
 *     ```
       Container::make( 'theme_options', 'Plugin Roles Options' )
           ->set_page_parent('Plugin Options');
           ->add_fields( array(
               Field::make( 'complex', 'role_add_field', 'Role Fields' )
                   ->add_fields( 'Text', array(
                       Field::make('text', $field_code . 'label', 'Label'),
                       Field::make('text', $field_code . 'code', 'Code Name')->set_required(true),
                       Field::make('text', $field_code . 'placeholder', 'Placeholder Value'),
                       Field::make('text', $field_code . 'default_value', 'Default Value'),
                   ) )
                   ->add_fields( 'Select', array(
                       Field::make('text', $field_code . 'label', 'Label'),
                       Field::make('text', $field_code . 'code', 'Code Name')->set_required(true),
                       Field::make('complex', $field_code .'options')->add_fields(array(
                           Field::make('text', $field_code . 'option_key', 'Key')->set_width(50)->set_required(true),
                           Field::make('text', $field_code . 'option_value', 'Value')->set_width(50)->set_required(true),
                   ) ),
           ) )
       ```
   
 * For more information about Multiple Complex Groups, please head to the Documentation–
   [https://carbonfields.net/docs/complex-field-multiple-groups/](https://carbonfields.net/docs/complex-field-multiple-groups/)
 * Let us know if this solves your issue.
 *  Thread Starter [martnick](https://wordpress.org/support/users/martnick/)
 * (@martnick)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/complex-field-with-relations-adding-extra-rows-to-db/#post-7681754)
 * Thanks for the fast response, I will play around with this, and will contact 
   you as soon as possible on my progress. Cheers.
 *  Thread Starter [martnick](https://wordpress.org/support/users/martnick/)
 * (@martnick)
 * [9 years, 9 months ago](https://wordpress.org/support/topic/complex-field-with-relations-adding-extra-rows-to-db/#post-7681934)
 * Hey guys, sorry for the late response. Everything is great now. Thank you once
   again!

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Complex field with relations, adding extra rows to db’ is closed to new
replies.

 * ![](https://s.w.org/plugins/geopattern-icon/carbon-fields_cacbcc.svg)
 * [Carbon Fields](https://wordpress.org/plugins/carbon-fields/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/carbon-fields/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/carbon-fields/)
 * [Active Topics](https://wordpress.org/support/plugin/carbon-fields/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/carbon-fields/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/carbon-fields/reviews/)

## Tags

 * [database](https://wordpress.org/support/topic-tag/database/)
 * [Saving](https://wordpress.org/support/topic-tag/saving/)
 * [showing](https://wordpress.org/support/topic-tag/showing/)

 * 3 replies
 * 2 participants
 * Last reply from: [martnick](https://wordpress.org/support/users/martnick/)
 * Last activity: [9 years, 9 months ago](https://wordpress.org/support/topic/complex-field-with-relations-adding-extra-rows-to-db/#post-7681934)
 * Status: resolved