diff --git a/src/Form.php b/src/Form.php index f61e7990..1358a22c 100644 --- a/src/Form.php +++ b/src/Form.php @@ -586,7 +586,7 @@ public function update($id, $data = null) $this->updateRelation($this->relations); }); - if (($result = $this->callSaved()) instanceof Response) { + if (($result = $this->callUpdated()) instanceof Response) { return $result; } diff --git a/src/Form/Concerns/HasHooks.php b/src/Form/Concerns/HasHooks.php index 9689bf26..364349f7 100644 --- a/src/Form/Concerns/HasHooks.php +++ b/src/Form/Concerns/HasHooks.php @@ -9,7 +9,7 @@ trait HasHooks { /** - * Supported hooks: submitted, editing, saving, saved, deleting, deleted. + * Supported hooks: submitted, editing, saving, saved, updated, deleting, deleted. * * @var array */ @@ -134,6 +134,18 @@ public function saved(Closure $callback) return $this->registerHook('saved', $callback); } + /** + * Set updated callback. + * + * @param Closure $callback + * + * @return $this + */ + public function updated(Closure $callback) + { + return $this->registerHook('updated', $callback); + } + /** * @param Closure $callback * @@ -185,7 +197,7 @@ protected function callSaving() } /** - * Callback after saving a Model. + * Callback after saving a Model (new model). * * @return mixed|null */ @@ -194,6 +206,16 @@ protected function callSaved() return $this->callHooks('saved'); } + /** + * Callback after updating a Model. + * + * @return mixed|null + */ + protected function callUpdated() + { + return $this->callHooks('updated'); + } + /** * Call hooks when deleting. * diff --git a/src/Form/EmbeddedForm.php b/src/Form/EmbeddedForm.php index 61b74555..15984695 100644 --- a/src/Form/EmbeddedForm.php +++ b/src/Form/EmbeddedForm.php @@ -159,12 +159,16 @@ public function setOriginal($data) */ public function prepare($input) { - foreach ($input as $key => $record) { - $this->setFieldOriginalValue($key); - $input[$key] = $this->prepareValue($key, $record); - } + if (is_array($input)) { + foreach ($input as $key => $record) { + $this->setFieldOriginalValue($key); + $input[$key] = $this->prepareValue($key, $record); + } - return $input; + return $input; + } else { + return []; + } } /** diff --git a/src/Form/Field/Date.php b/src/Form/Field/Date.php index 9174259f..6eb0e7a7 100644 --- a/src/Form/Field/Date.php +++ b/src/Form/Field/Date.php @@ -58,7 +58,8 @@ public function render() $this->options['allowInputToggle'] = true; $this->check_format_options(); - $this->script = "flatpickr('{$this->getElementClassSelector()}',".json_encode($this->options).');'; + $name= $this->elementName ?: $this->formatName($this->column); + $this->script = "flatpickr('input[name=\"{$name}\"]',".json_encode($this->options).');'; $this->prepend(''); $this->style('max-width', '160px'); diff --git a/src/Form/Field/HasMany.php b/src/Form/Field/HasMany.php index b87c6cc0..3af2c9f9 100644 --- a/src/Form/Field/HasMany.php +++ b/src/Form/Field/HasMany.php @@ -516,7 +516,10 @@ protected function setupScriptForDefaultView($templateScript) index++; var tpl = document.querySelector('template.{$this->column}-tpl').innerHTML; + var script = `{$templateScript}`; + tpl = tpl.replace(/{$defaultKey}/g, index); + script = script.replace(/{$defaultKey}/g, index); var clone = htmlToElement(tpl); addRemoveHasManyListener{$this->column}(clone.querySelector('.remove')); document.querySelector('.has-many-{$this->column}-forms').appendChild(clone); @@ -525,7 +528,7 @@ protected function setupScriptForDefaultView($templateScript) addHasManyTab{$this->column}(index); } - {$templateScript} + eval(script); return false; }); diff --git a/src/Form/Field/Number.php b/src/Form/Field/Number.php index eaef4159..a4139f7c 100644 --- a/src/Form/Field/Number.php +++ b/src/Form/Field/Number.php @@ -25,8 +25,9 @@ public function render() empty($this->attributes['readonly']) && empty($this->attributes['disabled']) ) { + $name= $this->elementName ?: $this->formatName($this->column); $this->script = <<getElementClassSelector()}')); + new NumberInput(document.querySelector('input[name=\"{$name}\"]')); JS; } diff --git a/src/Form/Field/Select.php b/src/Form/Field/Select.php index da3ac940..54e27d2c 100644 --- a/src/Form/Field/Select.php +++ b/src/Form/Field/Select.php @@ -348,7 +348,8 @@ public function render() $configs = json_encode($configs); if (!$this->native && $this->allowedChoicesJs()) { - $this->script .= 'var '.$this->choicesObjName()." = new Choices('{$this->getElementClassSelector()}',{$configs});"; + $name= $this->elementName ?: $this->formatName($this->column); + $this->script .= 'var '.$this->choicesObjName()." = new Choices('select[name=\"{$name}\"]',{$configs});"; $this->script .= $this->additional_script; }