Skip to content

Commit

Permalink
Merge pull request #310 from michaelbaril/master
Browse files Browse the repository at this point in the history
Fixing file validation rules
  • Loading branch information
toonvandenbos authored Jun 27, 2023
2 parents 1d7d306 + 7898b49 commit 274dc38
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
39 changes: 39 additions & 0 deletions src/Http/ScopedRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,20 @@

class ScopedRequest extends NovaRequest
{
/**
* The group's key.
*
* @var string
*/
public $group;

/**
* The original file input attributes.
*
* @var array
*/
protected $fileAttributes = [];

/**
* Create a copy of the given request, only containing the group's input
*
Expand Down Expand Up @@ -124,6 +136,11 @@ protected function getNestedFiles($iterable, $group = null)
protected function handleScopeFiles(&$files, &$input, $group)
{
$attributes = collect($files)->keyBy('original');

$this->fileAttributes = $attributes->mapWithKeys(function($attribute, $key) {
return [$attribute->name => $key];
});

$scope = [];

foreach ($this->getFlattenedFiles() as $attribute => $file) {
Expand Down Expand Up @@ -186,4 +203,26 @@ protected function isFlexibleStructure($iterable)
&& in_array('key', $keys, true)
&& in_array('attributes', $keys, true);
}

/**
* Check if the given argument is a defined file attribute.
*
* @param string $name
* @return bool
*/
public function isFileAttribute($name)
{
return $this->fileAttributes->has($name);
}

/**
* Return the actual file input attribute
*
* @param string $name
* @return string
*/
public function getFileAttribute($name)
{
return $this->fileAttributes->get($name);
}
}
12 changes: 6 additions & 6 deletions src/Layouts/Layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,13 +419,13 @@ protected function getScopedFieldRules($field, ScopedRequest $request, $specific

$rules = call_user_func([$field, $method], $request);

return collect($rules)->mapWithKeys(function ($validatorRules, $attribute) use ($key, $field) {
$key = $key.'.attributes.'.$attribute;
return collect($rules)->mapWithKeys(function($validatorRules, $attribute) use ($key, $field, $request) {
$key = $request->isFileAttribute($attribute)
? $request->getFileAttribute($attribute)
: $key.'.attributes.'.$attribute;

return [$key => $this->wrapScopedFieldRules($field, $validatorRules)];
})
->filter()
->all();
return [$key => $this->wrapScopedFieldRules($field, $validatorRules)];
})->filter()->all();
}

/**
Expand Down

0 comments on commit 274dc38

Please sign in to comment.