From ec44843e9e06ba04858e6863009ccd30c6d62173 Mon Sep 17 00:00:00 2001 From: wize-wiz <9093559+wize-wiz@users.noreply.github.com> Date: Thu, 3 Oct 2019 00:07:22 +0200 Subject: [PATCH] Fixed belongs-to/morph-to as nested fields and added missing methods in validation rules --- README.md | 3 +++ src/NovaDependencyContainer.php | 32 ++++++++++++++++++++++++++++---- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 87f1eb4..c6270a0 100644 --- a/README.md +++ b/README.md @@ -141,6 +141,9 @@ NovaDependencyContainer::make([ I'm going to abuse this README for versioning ... + - v1.2.3 + - Fixed belongs-to/morph-to as nested fields [#80](https://github.com/epartment/nova-dependency-container/issues/80) + - Added missing methods in validation rules - v1.2.2 - fixed fields not resolving when using `displayUsingLabels`, `resolveUsing` or `displayUsing`. (@dbf) - fixed action fields who do not return a collection (@bsormagec) diff --git a/src/NovaDependencyContainer.php b/src/NovaDependencyContainer.php index f92bb8f..3693e4f 100644 --- a/src/NovaDependencyContainer.php +++ b/src/NovaDependencyContainer.php @@ -5,6 +5,7 @@ use Illuminate\Database\Eloquent\Model; use Laravel\Nova\Fields\Field; use Laravel\Nova\Http\Requests\NovaRequest; +use Illuminate\Support\Facades\Log; class NovaDependencyContainer extends Field { @@ -163,8 +164,19 @@ public function resolveForDisplay($resource, $attribute = null) protected function resolveAttribute($resource, $attribute) { foreach ($this->meta['fields'] as $field) { - // changed to resolveForDisplay(), resolve() will be called when displayCallback is empty. - $field->resolveForDisplay($resource); + + switch($field->component) { + // callback is set for `resolve` in relation + case 'belongs-to-field': + case 'morph-to-field': + $field->resolve($resource); + break; + // default callbacks are for resolveForDisplay, and if implemented correctly, should call `resolve` + // at the end of the chain + // @todo: at all packaged incompatible with `resolveForDisplay()` + default: + $field->resolveForDisplay($resource); + } } return []; @@ -200,11 +212,23 @@ protected function isDependenciesSatisfiedForValidationRules(NovaRequest $reques $satisfiedCounts = 0; foreach ($this->meta['dependencies'] as $index => $dependency) { - if (array_key_exists('notEmpty', $dependency) && !empty($request->has($dependency['field']))) { + + if (array_key_exists('empty', $dependency) && empty($request->has($dependency['property']))) { + $satisfiedCounts++; + + } + + if (array_key_exists('notEmpty', $dependency) && !empty($request->has($dependency['property']))) { + $satisfiedCounts++; + } + + // inverted + if (array_key_exists('nullOrZero', $dependency) && in_array($request->get($dependency['property']), [null, 0, '0'], true)) { $satisfiedCounts++; + } - if (array_key_exists('value', $dependency) && $dependency['value'] == $request->get($dependency['field'])) { + if (array_key_exists('value', $dependency) && $dependency['value'] == $request->get($dependency['property'])) { $satisfiedCounts++; } }