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

Update HasMany.php #5801

Open
wants to merge 2 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
69 changes: 29 additions & 40 deletions src/Form/Field/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class HasMany extends Field
*
* @var \Closure
*/
protected $builder = null;
protected $builder;

/**
* Form data.
Expand All @@ -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',
];

/**
Expand All @@ -78,7 +78,6 @@ class HasMany extends Field
/**
* Create a new HasMany field instance.
*
* @param $relationName
* @param array $arguments
*/
public function __construct($relationName, $arguments = [])
Expand All @@ -87,21 +86,19 @@ 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;
}
}

/**
* Get validator for this field.
*
* @param array $input
*
* @return bool|\Illuminate\Contracts\Validation\Validator
*/
public function getValidator(array $input)
Expand All @@ -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]);
Expand Down Expand Up @@ -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;
}
Expand All @@ -180,8 +176,6 @@ public function getValidator(array $input)
/**
* Set distinct fields.
*
* @param array $fields
*
* @return $this
*/
public function distinctFields(array $fields)
Expand All @@ -193,8 +187,6 @@ public function distinctFields(array $fields)

/**
* Append distinct rules.
*
* @param array $rules
*/
protected function appendDistinctRules(array &$rules)
{
Expand Down Expand Up @@ -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)
{
Expand All @@ -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
*/
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -398,9 +387,9 @@ public function useTable()
/**
* Build Nested form for related data.
*
* @throws \Exception
*
* @return array
*
* @throws \Exception
*/
protected function buildRelatedForms()
{
Expand All @@ -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;
}

Expand Down Expand Up @@ -580,21 +569,23 @@ 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 = <<<EOT
var index = 0;
$('#has-many-{$this->column}').on('click', '.add', function () {

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}
Expand Down Expand Up @@ -645,17 +636,17 @@ public function disableDelete()
/**
* Render the `HasMany` field.
*
* @throws \Exception
*
* @return \Illuminate\View\View
*
* @throws \Exception
*/
public function render()
{
if (!$this->shouldRender()) {
return '';
}

if ($this->viewMode == 'table') {
if ('table' == $this->viewMode) {
return $this->renderTable();
}

Expand All @@ -668,19 +659,17 @@ 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,
]);
}

/**
* Render the `HasMany` field for table style.
*
* @throws \Exception
*
* @return mixed
*/
protected function renderTable()
{
Expand Down Expand Up @@ -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,
]);
}
}