From cfe3885fc5ed833c1530bfff9fdfbd729047505f Mon Sep 17 00:00:00 2001 From: Alex Oleynik Date: Thu, 14 Sep 2023 18:29:24 +0300 Subject: [PATCH 1/3] fix: HasMany new items index after validation fails; --- src/Form/Field/HasMany.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/Form/Field/HasMany.php b/src/Form/Field/HasMany.php index e8f4b09ed8..d6dd8f885e 100644 --- a/src/Form/Field/HasMany.php +++ b/src/Form/Field/HasMany.php @@ -489,6 +489,11 @@ protected function setupScriptForDefaultView($templateScript) */ $script = <<column} input._remove_[name^="{$this->column}[new_"]'); +if (old_new_inputs.length) { + var old_new_input_key_index = old_new_inputs.last().attr('name').match(/\[new_(\d+)\]/); + index += old_new_input_key_index ? old_new_input_key_index[1] : 0; +} $('#has-many-{$this->column}').off('click', '.add').on('click', '.add', function () { var tpl = $('template.{$this->column}-tpl'); @@ -544,6 +549,11 @@ protected function setupScriptForTabView($templateScript) }); var index = 0; +var old_new_inputs = $('#has-many-{$this->column} input._remove_[name^="{$this->column}[new_"]'); +if (old_new_inputs.length) { + var old_new_input_key_index = old_new_inputs.last().attr('name').match(/\[new_(\d+)\]/); + index += old_new_input_key_index ? old_new_input_key_index[1] : 0; +} $('#has-many-{$this->column} > .header').off('click', '.add').on('click', '.add', function(){ index++; var navTabHtml = $('#has-many-{$this->column} > template.nav-tab-tpl').html().replace(/{$defaultKey}/g, index); @@ -589,6 +599,11 @@ protected function setupScriptForTableView($templateScript) */ $script = <<column} input._remove_[name^="{$this->column}[new_"]'); +if (old_new_inputs.length) { + var old_new_input_key_index = old_new_inputs.last().attr('name').match(/\[new_(\d+)\]/); + index += old_new_input_key_index ? old_new_input_key_index[1] : 0; +} $('#has-many-{$this->column}').on('click', '.add', function () { var tpl = $('template.{$this->column}-tpl'); From 16e432d2e18471801feaea3abf0e16b771745ef3 Mon Sep 17 00:00:00 2001 From: Alex Oleynik Date: Thu, 14 Sep 2023 18:35:47 +0300 Subject: [PATCH 2/3] chore: StyleCI fix; --- src/Form/Field/HasMany.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Form/Field/HasMany.php b/src/Form/Field/HasMany.php index d6dd8f885e..8926beaa81 100644 --- a/src/Form/Field/HasMany.php +++ b/src/Form/Field/HasMany.php @@ -78,7 +78,7 @@ class HasMany extends Field /** * Create a new HasMany field instance. * - * @param $relationName + * @param $relationName * @param array $arguments */ public function __construct($relationName, $arguments = []) @@ -270,7 +270,6 @@ protected function resetInputKey(array &$input, array $column) * in the HasMany relation, has many data/field set, $set is field set in the below */ foreach ($input[$this->column] as $index => $set) { - /* * foreach the field set to find the corresponding $column */ From 67266f55dc0e308fcff2cb66e048554e6c409d87 Mon Sep 17 00:00:00 2001 From: Alex Oleynik Date: Thu, 14 Sep 2023 18:56:26 +0300 Subject: [PATCH 3/3] chore: use of REMOVE_FLAG_CLASS in selectors; --- src/Form/Field/HasMany.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Form/Field/HasMany.php b/src/Form/Field/HasMany.php index 8926beaa81..e08ec46322 100644 --- a/src/Form/Field/HasMany.php +++ b/src/Form/Field/HasMany.php @@ -488,7 +488,7 @@ protected function setupScriptForDefaultView($templateScript) */ $script = <<column} input._remove_[name^="{$this->column}[new_"]'); +var old_new_inputs = $('#has-many-{$this->column} input.{$removeClass}[name^="{$this->column}[new_"]'); if (old_new_inputs.length) { var old_new_input_key_index = old_new_inputs.last().attr('name').match(/\[new_(\d+)\]/); index += old_new_input_key_index ? old_new_input_key_index[1] : 0; @@ -548,7 +548,7 @@ protected function setupScriptForTabView($templateScript) }); var index = 0; -var old_new_inputs = $('#has-many-{$this->column} input._remove_[name^="{$this->column}[new_"]'); +var old_new_inputs = $('#has-many-{$this->column} input.{$removeClass}[name^="{$this->column}[new_"]'); if (old_new_inputs.length) { var old_new_input_key_index = old_new_inputs.last().attr('name').match(/\[new_(\d+)\]/); index += old_new_input_key_index ? old_new_input_key_index[1] : 0; @@ -598,7 +598,7 @@ protected function setupScriptForTableView($templateScript) */ $script = <<column} input._remove_[name^="{$this->column}[new_"]'); +var old_new_inputs = $('#has-many-{$this->column} input.{$removeClass}[name^="{$this->column}[new_"]'); if (old_new_inputs.length) { var old_new_input_key_index = old_new_inputs.last().attr('name').match(/\[new_(\d+)\]/); index += old_new_input_key_index ? old_new_input_key_index[1] : 0;