Skip to content
Open
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
24 changes: 23 additions & 1 deletion classes/controllers/FrmFieldsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public static function load_single_field( $field_object, $values, $form_id = 0 )
}

if ( ! isset( $field ) && is_object( $field_object ) ) {
$field_object->parent_form_id = $values['id'] ?? $field_object->form_id;
$field_object->parent_form_id = self::get_form_id_from_field_or_form( $field_object, $values );
$field = FrmFieldsHelper::setup_edit_vars( $field_object );
}

Expand All @@ -205,6 +205,28 @@ public static function load_single_field( $field_object, $values, $form_id = 0 )
require FrmAppHelper::plugin_path() . '/classes/views/frm-forms/add_field.php';
}

/**
* Get the parent form id for the field.
*
* @since x.x
*
* @param object $field_object The field object.
* @param array $values Either form or field values.
*
* @return int
*/
private static function get_form_id_from_field_or_form( $field_object, $values ) {
$form_id = absint( $field_object->form_id );

if ( isset( $values['form_key'] ) ) {
return $values['id'] ?? $form_id;
}
Comment on lines +221 to +223
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

?? doesn't guard against $values['id'] === 0.

The DB-query path on line 227 deliberately uses a ternary to catch falsy results (including '0' from the DB). Line 222 uses ??, which only falls back on null / unset — so a $values['id'] of 0 (e.g. when the AJAX request carries no valid form ID) is returned as-is instead of falling back to $form_id.

🛡️ Proposed fix
-		return $values['id'] ?? $form_id;
+		return ( $values['id'] ?? 0 ) ?: $form_id;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if ( isset( $values['form_key'] ) ) {
return $values['id'] ?? $form_id;
}
if ( isset( $values['form_key'] ) ) {
return ( $values['id'] ?? 0 ) ?: $form_id;
}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@classes/controllers/FrmFieldsController.php` around lines 221 - 223, In
FrmFieldsController replace the null-coalescing return that uses $values['id']
?? $form_id with a falsy-checking fallback so a value of '0' or 0 falls back to
$form_id; specifically change the return in the block that checks
isset($values['form_key']) (the line returning $values['id'] ?? $form_id) to use
a ternary or equivalent falsy check (e.g. return $values['id'] ? $values['id'] :
$form_id) so it matches the DB-query path behavior.


$parent_form_id = absint( FrmDb::get_var( 'frm_forms', array( 'id' => $form_id ), 'parent_form_id' ) );

return $parent_form_id ? $parent_form_id : $form_id;
}

/**
* @since 3.0
*
Expand Down