Skip to content
Closed
Show file tree
Hide file tree
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
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,11 @@ class Page extends Resource
The package supports four kinds of dependencies:

1. `->dependsOn('field', 'value')`
2. `->dependsOnNot('field', 'value')`
3. `->dependsOnEmpty('field')`
4. `->dependsOnNotEmpty('field')`
5. `->dependsOnNullOrZero('field')`
2. `->dependsOnIn('field', ['value', 'value'])`
3. `->dependsOnNot('field', 'value')`
4. `->dependsOnEmpty('field')`
5. `->dependsOnNotEmpty('field')`
6. `->dependsOnNullOrZero('field')`

These dependencies can be combined by chaining the methods on the `NovaDependencyContainer`:

Expand All @@ -86,6 +87,7 @@ NovaDependencyContainer::make([
->dependsOn('field1', 'value1')
->dependsOnNotEmpty('field2')
->dependsOn('field3', 'value3')
->dependsOnIn('field4', ['value4', 'value5', 'value6'])
```

The fields used as dependencies can be of any Laravel Nova field type. Currently only two relation field types are supported, `BelongsTo` and `MorphTo`.
Expand Down
2 changes: 1 addition & 1 deletion dist/js/field.js

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion resources/js/components/FormField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,16 @@
return;
}

if (dependency.hasOwnProperty('not') && dependencyValue != dependency.not) {
if (dependency.hasOwnProperty('not') && dependencyValue !== dependency.not) {
this.dependenciesSatisfied = true;
return;
}

if (dependency.hasOwnProperty('in') && dependency.in.includes(dependencyValue)) {
this.dependenciesSatisfied = true;
return;
}

if (dependency.hasOwnProperty('value') && dependencyValue == dependency.value) {
this.dependenciesSatisfied = true;
return;
Expand Down
20 changes: 20 additions & 0 deletions src/NovaDependencyContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,21 @@ public function dependsOnNot($field, $value)
]);
}

/**
* Adds a dependency for in
*
* @param $field
* @return NovaDependencyContainer
*/
public function dependsOnIn($field, $array)
{
return $this->withMeta([
'dependencies' => array_merge($this->meta['dependencies'], [
array_merge($this->getFieldLayout($field), ['in' => $array])
])
]);
}

/**
* Adds a dependency for not empty
*
Expand Down Expand Up @@ -173,6 +188,11 @@ public function resolveForDisplay($resource, $attribute = null)
continue;
}

if (array_key_exists('in', $dependency) && in_array($resource->{$dependency['property']}, $dependency['in'])) {
$this->meta['dependencies'][$index]['satisfied'] = true;
continue;
}

if (array_key_exists('value', $dependency)) {
if ($dependency['value'] == $resource->{$dependency['property']}) {
$this->meta['dependencies'][$index]['satisfied'] = true;
Expand Down