diff --git a/classes/helpers/FrmFieldGridHelper.php b/classes/helpers/FrmFieldGridHelper.php index d41e0c47c8..bc692635ce 100644 --- a/classes/helpers/FrmFieldGridHelper.php +++ b/classes/helpers/FrmFieldGridHelper.php @@ -60,6 +60,24 @@ class FrmFieldGridHelper { */ private $section_is_open = false; + /** + * True if the first field in the row had frm_first, indicating an intentional group. + * + * Example: Two fields grouped in one row. + * - First field has frm_first → is_group_start = true. + * - Second field has no frm_first, but is_group_start is true → stays in the same row. + * + * @var bool + */ + private $is_group_start = false; + + /** + * True if the first field in the row was a section (divider). + * + * @var bool + */ + private $is_section_start = false; + /** * @param bool $nested */ @@ -155,14 +173,25 @@ public function maybe_begin_field_wrapper() { * @return bool */ private function should_first_close_the_active_field_wrapper() { - if ( false === $this->parent_li || ! empty( $this->section_helper ) ) { + if ( false === $this->parent_li ) { + return false; + } + + // Close the current row before a section if it would overflow the grid. + // @see https://github.com/Strategy11/formidable-pro/issues/3820 + if ( 'divider' === $this->field->type && ! $this->section_is_open ) { + return $this->current_list_size + $this->section_size > 12 || $this->is_frm_first || ! $this->is_group_start; + } + + if ( ! empty( $this->section_helper ) ) { return false; } if ( 'end_divider' === $this->field->type ) { return false; } - return ! $this->can_support_current_layout() || $this->is_frm_first; + + return ! $this->can_support_current_layout() || $this->is_frm_first || ( $this->is_section_start && ! $this->is_group_start ); } /** @@ -173,6 +202,8 @@ private function begin_field_wrapper() { $this->parent_li = true; $this->current_list_size = 0; $this->current_field_count = 0; + $this->is_group_start = $this->is_frm_first; + $this->is_section_start = 'divider' === $this->field->type; } /**