Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support saving blank options for radio fields and checkbox field #1358

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion classes/controllers/FrmFieldsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ public static function import_options() {
$field = FrmFieldsHelper::setup_edit_vars( $field );
$opts = FrmAppHelper::get_param( 'opts', '', 'post', 'wp_kses_post' );
$opts = explode( "\n", rtrim( $opts, "\n" ) );
$opts = array_map( 'trim', $opts );

$separate = FrmAppHelper::get_param( 'separate', '', 'post', 'sanitize_text_field' );
$field['separate_value'] = ( $separate === 'true' );
Expand Down
14 changes: 14 additions & 0 deletions classes/helpers/FrmAppHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -1876,6 +1876,20 @@
}
}

/**
* @since x.x
*
* @param string $value
* @return string
*/
public static function trim_if_not_blank( $value ) {
if ( trim( $value ) === '' ) {
return $value;

Check warning on line 1887 in classes/helpers/FrmAppHelper.php

View check run for this annotation

Codecov / codecov/patch

classes/helpers/FrmAppHelper.php#L1887

Added line #L1887 was not covered by tests
}

return trim( $value );
}

public static function check_selected( $values, $current ) {
$values = self::recursive_function_map( $values, 'trim' );
$values = self::recursive_function_map( $values, 'htmlspecialchars_decode' );
Expand Down
2 changes: 1 addition & 1 deletion classes/models/FrmEntryMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static function add_entry_meta( $entry_id, $field_id, $meta_key, $meta_va
}

$new_values = array(
'meta_value' => is_array( $meta_value ) ? serialize( array_filter( $meta_value, 'FrmAppHelper::is_not_empty_value' ) ) : trim( $meta_value ),
'meta_value' => is_array( $meta_value ) ? serialize( array_filter( $meta_value, 'FrmAppHelper::is_not_empty_value' ) ) : FrmAppHelper::trim_if_not_blank( $meta_value ),
'item_id' => $entry_id,
'field_id' => $field_id,
'created_at' => current_time( 'mysql', 1 ),
Expand Down
2 changes: 1 addition & 1 deletion classes/models/FrmEntryValidate.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public static function validate_field( $posted_field, &$errors, $values, $args =
}

if ( ! is_array( $value ) ) {
$value = trim( $value );
$value = FrmAppHelper::trim_if_not_blank( $value );
}

if ( $posted_field->required == '1' && FrmAppHelper::is_empty_value( $value ) ) {
Expand Down
3 changes: 2 additions & 1 deletion classes/views/frm-fields/front-end/checkbox-field.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
// Let the checked state of 'Other' fields be determined solely by FrmFieldsHelper::prepare_other_input as below.
// Without this check, one 'Other' field being checked leads to making all 'Other' fields checked on submit error
// since they all have the same value attr of 'Other'.
$checked = FrmAppHelper::check_selected( $field['value'], $field_val ) ? ' checked="checked"' : '';
$checked = is_array( $field['value'] ) ? in_array( $field_val, $field['value'], true ) : $field['value'] === $field_val;
$checked = $checked ? 'checked="checked" ' : ' ';
}

// Check if other opt, and get values for other field if needed
Expand Down
9 changes: 8 additions & 1 deletion classes/views/frm-fields/front-end/radio-field.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,14 @@
if ( ! isset( $shortcode_atts ) || ! isset( $shortcode_atts['label'] ) || $shortcode_atts['label'] ) {
?><label for="<?php echo esc_attr( $html_id ); ?>-<?php echo esc_attr( $opt_key ); ?>"><?php
}
$checked = FrmAppHelper::check_selected( $field['value'], $field_val ) ? 'checked="checked" ' : ' ';

if ( get_called_class() === 'FrmSurveys\models\fields\Radio' ) {
$checked = is_array( $field['value'] ) ? in_array( $field_val, $field['value'], true ) : $field['value'] === $field_val;
} else {
$checked = FrmAppHelper::check_selected( $field['value'], $field_val );
}

$checked = $checked ? 'checked="checked" ' : ' ';

$other_opt = false;
$other_args = FrmFieldsHelper::prepare_other_input( compact( 'field_name', 'opt_key', 'field' ), $other_opt, $checked );
Expand Down
Loading