diff --git a/src/Form/Field/HasMany.php b/src/Form/Field/HasMany.php index e8f4b09ed8..e35b2212e7 100644 --- a/src/Form/Field/HasMany.php +++ b/src/Form/Field/HasMany.php @@ -29,7 +29,7 @@ class HasMany extends Field * * @var \Closure */ - protected $builder = null; + protected $builder; /** * Form data. @@ -54,8 +54,8 @@ class HasMany extends Field */ protected $views = [ 'default' => 'admin::form.hasmany', - 'tab' => 'admin::form.hasmanytab', - 'table' => 'admin::form.hasmanytable', + 'tab' => 'admin::form.hasmanytab', + 'table' => 'admin::form.hasmanytable', ]; /** @@ -78,7 +78,6 @@ class HasMany extends Field /** * Create a new HasMany field instance. * - * @param $relationName * @param array $arguments */ public function __construct($relationName, $arguments = []) @@ -87,12 +86,12 @@ public function __construct($relationName, $arguments = []) $this->column = $relationName; - if (count($arguments) == 1) { + if (1 == count($arguments)) { $this->label = $this->formatLabel(); $this->builder = $arguments[0]; } - if (count($arguments) == 2) { + if (2 == count($arguments)) { list($this->label, $this->builder) = $arguments; } } @@ -100,8 +99,6 @@ public function __construct($relationName, $arguments = []) /** * Get validator for this field. * - * @param array $input - * * @return bool|\Illuminate\Contracts\Validation\Validator */ public function getValidator(array $input) @@ -111,8 +108,7 @@ public function getValidator(array $input) } $input = Arr::only($input, $this->column); - - /** unset item that contains remove flag */ + /* unset item that contains remove flag */ foreach ($input[$this->column] as $key => $value) { if ($value[NestedForm::REMOVE_FLAG_NAME]) { unset($input[$this->column][$key]); @@ -159,8 +155,8 @@ public function getValidator(array $input) foreach ($rules as $column => $rule) { foreach (array_keys($input[$this->column]) as $key) { $newRules["{$this->column}.$key.$column"] = $rule; - if (isset($input[$this->column][$key][$column]) && - is_array($input[$this->column][$key][$column])) { + if (isset($input[$this->column][$key][$column]) + && is_array($input[$this->column][$key][$column])) { foreach ($input[$this->column][$key][$column] as $vkey => $value) { $newInput["{$this->column}.$key.{$column}$vkey"] = $value; } @@ -180,8 +176,6 @@ public function getValidator(array $input) /** * Set distinct fields. * - * @param array $fields - * * @return $this */ public function distinctFields(array $fields) @@ -193,8 +187,6 @@ public function distinctFields(array $fields) /** * Append distinct rules. - * - * @param array $rules */ protected function appendDistinctRules(array &$rules) { @@ -242,10 +234,9 @@ protected function formatValidationAttribute($input, $label, $column) /** * Reset input key for validation. * - * @param array $input * @param array $column $column is the column name array set * - * @return void. + * @return void */ protected function resetInputKey(array &$input, array $column) { @@ -270,7 +261,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 */ @@ -320,9 +310,8 @@ public function prepare($input) /** * Build a Nested form. * - * @param string $column - * @param \Closure $builder - * @param null $model + * @param string $column + * @param null $model * * @return NestedForm */ @@ -362,7 +351,7 @@ protected function getKeyName() /** * Set view mode. * - * @param string $mode currently support `tab` mode. + * @param string $mode currently support `tab` mode * * @return $this * @@ -398,9 +387,9 @@ public function useTable() /** * Build Nested form for related data. * - * @throws \Exception - * * @return array + * + * @throws \Exception */ protected function buildRelatedForms() { @@ -427,7 +416,7 @@ protected function buildRelatedForms() */ if ($values = old($this->column)) { foreach ($values as $key => $data) { - if ($data[NestedForm::REMOVE_FLAG_NAME] == 1) { + if (1 == $data[NestedForm::REMOVE_FLAG_NAME]) { continue; } @@ -580,13 +569,15 @@ protected function setupScriptForTableView($templateScript) $removeClass = NestedForm::REMOVE_FLAG_CLASS; $defaultKey = NestedForm::DEFAULT_KEY_NAME; - /** + /* * When add a new sub form, replace all element key in new sub form. * * @example comments[new___key__][title] => comments[new_{index}][title] * * {count} is increment number of current sub form count. */ + $this->column = $this->formatId($this->column); + $script = <<column}').on('click', '.add', function () { @@ -594,7 +585,7 @@ protected function setupScriptForTableView($templateScript) var tpl = $('template.{$this->column}-tpl'); index++; - + console.log("{$this->column}"); var template = tpl.html().replace(/{$defaultKey}/g, index); $('.has-many-{$this->column}-forms').append(template); {$templateScript} @@ -645,9 +636,9 @@ public function disableDelete() /** * Render the `HasMany` field. * - * @throws \Exception - * * @return \Illuminate\View\View + * + * @throws \Exception */ public function render() { @@ -655,7 +646,7 @@ public function render() return ''; } - if ($this->viewMode == 'table') { + if ('table' == $this->viewMode) { return $this->renderTable(); } @@ -668,10 +659,10 @@ public function render() $this->setupScript($script); return parent::fieldRender([ - 'forms' => $this->buildRelatedForms(), - 'template' => $template, + 'forms' => $this->buildRelatedForms(), + 'template' => $template, 'relationName' => $this->relationName, - 'options' => $this->options, + 'options' => $this->options, ]); } @@ -679,8 +670,6 @@ public function render() * Render the `HasMany` field for table style. * * @throws \Exception - * - * @return mixed */ protected function renderTable() { @@ -725,11 +714,11 @@ protected function renderTable() $this->view = $this->views[$this->viewMode]; return parent::fieldRender([ - 'headers' => $headers, - 'forms' => $this->buildRelatedForms(), - 'template' => $template, + 'headers' => $headers, + 'forms' => $this->buildRelatedForms(), + 'template' => $template, 'relationName' => $this->relationName, - 'options' => $this->options, + 'options' => $this->options, ]); } }