Skip to content

Commit

Permalink
Fixed belongs-to/morph-to as nested fields and added missing methods …
Browse files Browse the repository at this point in the history
…in validation rules
  • Loading branch information
wize-wiz committed Oct 2, 2019
1 parent f1b170a commit ec44843
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 28 additions & 4 deletions src/NovaDependencyContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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 [];
Expand Down Expand Up @@ -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++;
}
}
Expand Down

0 comments on commit ec44843

Please sign in to comment.