diff --git a/wire/core/Inputfield.php b/wire/core/Inputfield.php index 4b31fe99..7b2ef4bd 100644 --- a/wire/core/Inputfield.php +++ b/wire/core/Inputfield.php @@ -493,12 +493,31 @@ abstract class Inputfield extends WireData implements Module { */ public function setParent(InputfieldWrapper $parent) { if($this->parent && $this->parent instanceof InputfieldWrapper && $this->parent !== $parent) { - $this->parent->remove($this); + $oldRootParent = $this->getRootParent(); + if(!$oldRootParent) $oldRootParent = $this->parent; + $newRootParent = $parent->getRootParent(); + if(!$newRootParent) $newRootParent = $parent; + if($oldRootParent === $newRootParent) { + // if field staying in the same form, remove from previous parent + $this->parent->remove($this); + } } $this->parent = $parent; return $this; } + /** + * Unset any previously set parent + * + * #pw-internal + * @return $this + * + */ + public function unsetParent() { + $this->parent = null; + return $this; + } + /** * Get this Inputfield’s parent InputfieldWrapper, or NULL if it doesn’t have one. * diff --git a/wire/core/InputfieldWrapper.php b/wire/core/InputfieldWrapper.php index dc68e000..0b4f0fb9 100644 --- a/wire/core/InputfieldWrapper.php +++ b/wire/core/InputfieldWrapper.php @@ -416,6 +416,7 @@ class InputfieldWrapper extends Inputfield implements \Countable, \IteratorAggre array_push($wrappers, $inputfield); } + $inputfield->unsetParent(); $wrapper->add($inputfield); }