diff --git a/classes/controllers/FrmAbilitiesFieldsController.php b/classes/controllers/FrmAbilitiesFieldsController.php index 147b0323a6..75d0a8faad 100644 --- a/classes/controllers/FrmAbilitiesFieldsController.php +++ b/classes/controllers/FrmAbilitiesFieldsController.php @@ -76,16 +76,17 @@ private static function register_list_fields_ability() { 'description' => __( 'Field type (text, textarea, radio, checkbox, dropdown, email, number, date, file, etc.)', 'formidable' ), ), 'default_value' => array( - 'type' => 'string', - 'description' => __( 'Default value for the field', 'formidable' ), + 'type' => array( 'string', 'array', 'object', 'null' ), + 'description' => __( 'Default value for the field. An array or object when the field holds several values, such as Address.', 'formidable' ), ), 'options' => array( - 'type' => 'array', - 'description' => __( - 'Field choices: strings, or {"label", "value"} objects when using separate values. Star rating and scale choices are numbers.', + 'type' => array( 'array', 'object' ), + 'description' => __( + 'Field choices: strings, numbers (star rating, scale), or {"label", "value"} objects when using separate values, or an object keyed by option key.', 'formidable' ), - 'items' => array( 'type' => array( 'string', 'number', 'object' ) ), + 'items' => array( 'type' => array( 'string', 'number', 'object' ) ), + 'additionalProperties' => array( 'type' => array( 'string', 'number', 'object' ) ), ), 'field_order' => array( 'type' => 'integer', diff --git a/tests/phpunit/fields/test_FrmAbilitiesFieldsController.php b/tests/phpunit/fields/test_FrmAbilitiesFieldsController.php index 647d7b496d..61f1910865 100644 --- a/tests/phpunit/fields/test_FrmAbilitiesFieldsController.php +++ b/tests/phpunit/fields/test_FrmAbilitiesFieldsController.php @@ -110,6 +110,59 @@ public function test_a_field_round_trips_through_create_list_update_delete() { $this->assertArrayNotHasKey( $created['field_key'], $after, 'A deleted field should no longer be listed.' ); } + /** + * Choice options keyed by option text, array defaults and a NULL default are what Formidable + * can store, so list-fields must not reject them, or one such field fails the whole form. + * + * @return void + */ + public function test_list_fields_accepts_string_keyed_options_and_array_default_values() { + $select_id = FrmField::create( + array( + 'form_id' => $this->form->id, + 'type' => 'select', + 'name' => 'Location', + 'default_value' => '', + 'options' => array( + '1' => array( + 'label' => '', + 'value' => '', + ), + 'Asheville' => array( + 'label' => 'Asheville', + 'value' => 'asheville', + ), + 'other_1' => 'Other', + ), + ) + ); + $address_id = FrmField::create( + array( + 'form_id' => $this->form->id, + 'type' => 'address', + 'name' => 'Address', + 'default_value' => array( + 'line1' => '1 Main St', + 'city' => 'Asheville', + ), + ) + ); + $text_id = FrmField::create( + array( + 'form_id' => $this->form->id, + 'type' => 'text', + 'name' => 'No default', + ) + ); + + $listed = $this->execute( 'list-fields', array( 'form_id' => $this->form->id ) ); + + $this->assertNotWPError( $listed, 'list-fields should validate for string-keyed options, array default values and a NULL default.' ); + $this->assertArrayHasKey( FrmField::getOne( $select_id )->field_key, $listed ); + $this->assertArrayHasKey( FrmField::getOne( $address_id )->field_key, $listed ); + $this->assertArrayHasKey( FrmField::getOne( $text_id )->field_key, $listed ); + } + /** * @return void */