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

fix: HasMany new items index after validation fails; #5819

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
18 changes: 16 additions & 2 deletions src/Form/Field/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [])
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -489,6 +488,11 @@ protected function setupScriptForDefaultView($templateScript)
*/
$script = <<<EOT
var index = 0;
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;
}
$('#has-many-{$this->column}').off('click', '.add').on('click', '.add', function () {

var tpl = $('template.{$this->column}-tpl');
Expand Down Expand Up @@ -544,6 +548,11 @@ protected function setupScriptForTabView($templateScript)
});

var index = 0;
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;
}
$('#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);
Expand Down Expand Up @@ -589,6 +598,11 @@ protected function setupScriptForTableView($templateScript)
*/
$script = <<<EOT
var index = 0;
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;
}
$('#has-many-{$this->column}').on('click', '.add', function () {

var tpl = $('template.{$this->column}-tpl');
Expand Down