Skip to content

Commit f524a97

Browse files
authored
Merge pull request z-song#5306 from rust17/master
当 form 参数形式为'驼峰.字段名'时,获取不到字段的值
2 parents 0123433 + 272070d commit f524a97

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

src/Form.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ public function edit($id): self
192192
$this->builder->setMode(Builder::MODE_EDIT);
193193
$this->builder->setResourceId($id);
194194

195+
$this->setRelationFieldSnakeAttributes();
196+
195197
$this->setFieldValue($id);
196198

197199
return $this;
@@ -1009,6 +1011,34 @@ protected function setFieldOriginalValue()
10091011
});
10101012
}
10111013

1014+
/**
1015+
* Determine relational column needs to be snaked.
1016+
*
1017+
* @return void
1018+
*/
1019+
protected function setRelationFieldSnakeAttributes()
1020+
{
1021+
$relations = $this->getRelations();
1022+
1023+
$this->fields()->each(function (Field $field) use ($relations) {
1024+
if ($field->getSnakeAttributes()) {
1025+
return;
1026+
}
1027+
1028+
$column = $field->column();
1029+
1030+
$column = is_array($column) ? head($column) : $column;
1031+
1032+
list($relation) = explode('.', $column);
1033+
1034+
if (!in_array($relation, $relations)) {
1035+
return;
1036+
}
1037+
1038+
$field->setSnakeAttributes($this->model::$snakeAttributes);
1039+
});
1040+
}
1041+
10121042
/**
10131043
* Set all fields value in form.
10141044
*

src/Form/Field.php

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,13 @@ class Field implements Renderable
246246
*/
247247
protected $callback;
248248

249+
/**
250+
* column is snake-casing attributes.
251+
*
252+
* @var bool
253+
*/
254+
protected $snakeAttributes = false;
255+
249256
/**
250257
* @var bool
251258
*/
@@ -379,6 +386,42 @@ public function setElementName($name): self
379386
return $this;
380387
}
381388

389+
/**
390+
* Set snake attributes to the field.
391+
*
392+
* @param bool $snakeAttributes
393+
*
394+
* @return $this
395+
*/
396+
public function setSnakeAttributes($snakeAttributes)
397+
{
398+
$this->snakeAttributes = $snakeAttributes;
399+
400+
return $this;
401+
}
402+
403+
/**
404+
* Get snake attributes of the field.
405+
*
406+
* @return bool
407+
*/
408+
public function getSnakeAttributes()
409+
{
410+
return $this->snakeAttributes;
411+
}
412+
413+
/**
414+
* Determine if a column needs to be snaked.
415+
*
416+
* @param string|array $column
417+
*
418+
* @return string|array
419+
*/
420+
protected function columnShouldSnaked($column)
421+
{
422+
return $this->getSnakeAttributes() ? Str::snake($column) : $column;
423+
}
424+
382425
/**
383426
* Fill data to the field.
384427
*
@@ -392,13 +435,13 @@ public function fill($data)
392435

393436
if (is_array($this->column)) {
394437
foreach ($this->column as $key => $column) {
395-
$this->value[$key] = Arr::get($data, $column);
438+
$this->value[$key] = Arr::get($data, $this->columnShouldSnaked($column));
396439
}
397440

398441
return;
399442
}
400443

401-
$this->value = Arr::get($data, $this->column);
444+
$this->value = Arr::get($data, $this->columnShouldSnaked($this->column));
402445

403446
$this->formatValue();
404447
}

0 commit comments

Comments
 (0)