Skip to content
Open
13 changes: 7 additions & 6 deletions classes/controllers/FrmAbilitiesFieldsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,17 @@
'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',
Expand Down Expand Up @@ -564,7 +565,7 @@
*
* @return array
*/
public static function normalize_field_type( $field ) {

Check failure on line 568 in classes/controllers/FrmAbilitiesFieldsController.php

View workflow job for this annotation

GitHub Actions / Psalm

MissingPureAnnotation

classes/controllers/FrmAbilitiesFieldsController.php:568:25: MissingPureAnnotation: normalize_field_type must be marked @psalm-pure to aid security analysis, run with --alter --issues=MissingPureAnnotation to fix this (see https://psalm.dev/363)
if ( empty( $field['type'] ) ) {
return $field;
}
Expand Down Expand Up @@ -598,7 +599,7 @@
*
* @return array
*/
public static function apply_field_input( $prepared, $field ) {

Check failure on line 602 in classes/controllers/FrmAbilitiesFieldsController.php

View workflow job for this annotation

GitHub Actions / Psalm

MissingPureAnnotation

classes/controllers/FrmAbilitiesFieldsController.php:602:25: MissingPureAnnotation: apply_field_input must be marked @psalm-pure to aid security analysis, run with --alter --issues=MissingPureAnnotation to fix this (see https://psalm.dev/363)
foreach ( $field as $option => $value ) {
if ( 'field_options' === $option && is_array( $value ) ) {
$prepared['field_options'] = array_merge( (array) $prepared['field_options'], $value );
Expand Down Expand Up @@ -748,7 +749,7 @@
*
* @return array
*/
private static function maybe_enable_separate_values( $prepared, $field ) {

Check failure on line 752 in classes/controllers/FrmAbilitiesFieldsController.php

View workflow job for this annotation

GitHub Actions / Psalm

MissingPureAnnotation

classes/controllers/FrmAbilitiesFieldsController.php:752:26: MissingPureAnnotation: maybe_enable_separate_values must be marked @psalm-pure to aid security analysis, run with --alter --issues=MissingPureAnnotation to fix this (see https://psalm.dev/363)
$explicitly_set = isset( $field['separate_value'] ) || isset( $field['field_options']['separate_value'] );

if ( empty( $prepared['options'] ) || ! is_array( $prepared['options'] ) || $explicitly_set ) {
Expand Down Expand Up @@ -782,7 +783,7 @@
*
* @return array
*/
private static function merge_field_options_with_existing( $field, $field_data ) {

Check failure on line 786 in classes/controllers/FrmAbilitiesFieldsController.php

View workflow job for this annotation

GitHub Actions / Psalm

MissingPureAnnotation

classes/controllers/FrmAbilitiesFieldsController.php:786:26: MissingPureAnnotation: merge_field_options_with_existing must be marked @psalm-pure to aid security analysis, run with --alter --issues=MissingPureAnnotation to fix this (see https://psalm.dev/363)
if ( isset( $field_data['field_options'] ) && is_array( $field_data['field_options'] ) ) {
$field_data['field_options'] = array_merge( (array) $field->field_options, $field_data['field_options'] );
}
Expand Down Expand Up @@ -842,7 +843,7 @@
*
* @return array
*/
public static function prepare_field_for_response( $field ) {

Check failure on line 846 in classes/controllers/FrmAbilitiesFieldsController.php

View workflow job for this annotation

GitHub Actions / Psalm

MissingPureAnnotation

classes/controllers/FrmAbilitiesFieldsController.php:846:25: MissingPureAnnotation: prepare_field_for_response must be marked @psalm-pure to aid security analysis, run with --alter --issues=MissingPureAnnotation to fix this (see https://psalm.dev/363)
return array(
'id' => (int) $field->id,
'field_key' => (string) $field->field_key,
Expand Down
53 changes: 53 additions & 0 deletions tests/phpunit/fields/test_FrmAbilitiesFieldsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call to an undefined method test_FrmAbilitiesFieldsController::assertArrayHasKey()


The method you are trying to call is not defined, which can result in a fatal error.

$this->assertArrayHasKey( FrmField::getOne( $address_id )->field_key, $listed );

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call to an undefined method test_FrmAbilitiesFieldsController::assertArrayHasKey()


The method you are trying to call is not defined, which can result in a fatal error.

$this->assertArrayHasKey( FrmField::getOne( $text_id )->field_key, $listed );

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call to an undefined method test_FrmAbilitiesFieldsController::assertArrayHasKey()


The method you are trying to call is not defined, which can result in a fatal error.

}

/**
* @return void
*/
Expand Down
Loading