Skip to content

Commit 9c16f38

Browse files
committed
added dependsOnIn and dependsOnNotIn support
1 parent 6d86e24 commit 9c16f38

File tree

6 files changed

+114
-60
lines changed

6 files changed

+114
-60
lines changed

composer.json

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
11
{
2-
"name": "alexwenzel/nova-dependency-container",
3-
"description": "A Laravel Nova 4 form container for grouping fields that depend on other field values.",
4-
"keywords": [
5-
"laravel",
6-
"nova",
7-
"nova-4",
8-
"field"
9-
],
10-
"authors": [
11-
{
12-
"name": "alexwenzel",
13-
"email": "alexander.wenzel.berlin@gmail.com"
2+
"name": "alexwenzel/nova-dependency-container",
3+
"description": "A Laravel Nova 4 form container for grouping fields that depend on other field values.",
4+
"keywords": [
5+
"laravel",
6+
"nova",
7+
"nova-4",
8+
"field"
9+
],
10+
"authors": [
11+
{
12+
"name": "alexwenzel",
13+
"email": "alexander.wenzel.berlin@gmail.com"
14+
},
15+
{
16+
"name": "Epartment E-commerce",
17+
"email": "support@epartment.nl"
18+
}
19+
],
20+
"license": "MIT",
21+
"require": {
22+
"php": "^8.0",
23+
"laravel/framework": "^9.0",
24+
"laravel/nova": "^4.0"
1425
},
15-
{
16-
"name": "Epartment E-commerce",
17-
"email": "support@epartment.nl"
18-
}
19-
],
20-
"license": "MIT",
21-
"require": {
22-
"php": "^8.0",
23-
"laravel/framework": "^9.0",
24-
"laravel/nova": "^4.0"
25-
},
26-
"autoload": {
27-
"psr-4": {
28-
"Alexwenzel\\DependencyContainer\\": "src/"
29-
}
30-
},
31-
"extra": {
32-
"laravel": {
33-
"providers": [
34-
"Alexwenzel\\DependencyContainer\\FieldServiceProvider"
35-
]
36-
}
37-
},
38-
"config": {
39-
"sort-packages": true
40-
},
41-
"minimum-stability": "dev",
42-
"prefer-stable": true
26+
"autoload": {
27+
"psr-4": {
28+
"Alexwenzel\\DependencyContainer\\": "src/"
29+
}
30+
},
31+
"extra": {
32+
"laravel": {
33+
"providers": [
34+
"Alexwenzel\\DependencyContainer\\FieldServiceProvider"
35+
]
36+
}
37+
},
38+
"config": {
39+
"sort-packages": true
40+
},
41+
"minimum-stability": "dev",
42+
"prefer-stable": true
4343
}

dist/js/field.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
{
2-
"private": true,
3-
"scripts": {
4-
"dev": "npm run development",
5-
"development": "mix",
6-
"watch": "mix watch",
7-
"watch-poll": "mix watch -- --watch-options-poll=1000",
8-
"hot": "mix watch --hot",
9-
"prod": "npm run production",
10-
"production": "mix --production",
11-
"nova:install": "npm --prefix='../../vendor/laravel/nova' ci"
12-
},
13-
"devDependencies": {
14-
"@vue/compiler-sfc": "^3.2.22",
15-
"laravel-mix": "^6.0.41",
16-
"lodash": "^4.17.21",
17-
"postcss": "^8.3.11",
18-
"vue-loader": "^16.8.3"
19-
}
2+
"private": true,
3+
"scripts": {
4+
"dev": "npm run development",
5+
"development": "mix",
6+
"watch": "mix watch",
7+
"watch-poll": "mix watch -- --watch-options-poll=1000",
8+
"hot": "mix watch --hot",
9+
"prod": "npm run production",
10+
"production": "mix --production",
11+
"nova:install": "npm --prefix='../../vendor/laravel/nova' ci"
12+
},
13+
"devDependencies": {
14+
"@vue/compiler-sfc": "^3.2.22",
15+
"laravel-mix": "^6.0.41",
16+
"lodash": "^4.17.21",
17+
"postcss": "^8.3.11",
18+
"vue-loader": "^16.8.3"
19+
}
2020
}

readme.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,15 @@ class Page extends Resource
5757

5858
### Dependencies
5959

60-
The package supports four kinds of dependencies:
60+
The package supports this kinds of dependencies:
6161

6262
1. `->dependsOn('field', 'value')`
6363
2. `->dependsOnNot('field', 'value')`
6464
3. `->dependsOnEmpty('field')`
6565
4. `->dependsOnNotEmpty('field')`
6666
5. `->dependsOnNullOrZero('field')`
67+
6. `->dependsOnIn('field', [array])`
68+
7. `->dependsOnNotIn('field', [array])`
6769

6870
These dependencies can be combined by chaining the methods on the `DependencyContainer`:
6971

resources/js/components/FormField.vue

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,16 @@ export default {
136136
return;
137137
}
138138
139+
if (dependency.hasOwnProperty('in') && dependency.in.includes(dependencyValue)) {
140+
this.dependenciesSatisfied = true;
141+
return;
142+
}
143+
144+
if (dependency.hasOwnProperty('notin') && !dependency.notin.includes(dependencyValue)) {
145+
this.dependenciesSatisfied = true;
146+
return;
147+
}
148+
139149
if (dependency.hasOwnProperty('value') && dependencyValue == dependency.value) {
140150
this.dependenciesSatisfied = true;
141151
return;

src/DependencyContainer.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,38 @@ public function dependsOnNullOrZero($field)
112112
]);
113113
}
114114

115+
/**
116+
* Adds a dependency for in
117+
*
118+
* @param $field
119+
* @param $array
120+
* @return $this
121+
*/
122+
public function dependsOnIn($field, $array)
123+
{
124+
return $this->withMeta([
125+
'dependencies' => array_merge($this->meta['dependencies'], [
126+
array_merge($this->getFieldLayout($field), ['in' => $array])
127+
])
128+
]);
129+
}
130+
131+
/**
132+
* Adds a dependency for not in
133+
*
134+
* @param $field
135+
* @param $array
136+
* @return $this
137+
*/
138+
public function dependsOnNotIn($field, $array)
139+
{
140+
return $this->withMeta([
141+
'dependencies' => array_merge($this->meta['dependencies'], [
142+
array_merge($this->getFieldLayout($field), ['notin' => $array])
143+
])
144+
]);
145+
}
146+
115147
/**
116148
* Get layout for a specified field. Dot notation will result in {field}.{property}. If no dot was found it will
117149
* result in {field}.{field}, as it was in previous versions by default.
@@ -173,6 +205,16 @@ public function resolveForDisplay($resource, $attribute = null)
173205
continue;
174206
}
175207

208+
if (array_key_exists('in', $dependency) && in_array($resource->{$dependency['property']}, $dependency['in'])) {
209+
$this->meta['dependencies'][$index]['satisfied'] = true;
210+
continue;
211+
}
212+
213+
if (array_key_exists('notin', $dependency) && !in_array($resource->{$dependency['property']}, $dependency['notin'])) {
214+
$this->meta['dependencies'][$index]['satisfied'] = true;
215+
continue;
216+
}
217+
176218
if (array_key_exists('value', $dependency)) {
177219
if (is_array($resource)) {
178220
if (isset($resource[$dependency['property']]) && $dependency['value'] == $resource[$dependency['property']]) {

0 commit comments

Comments
 (0)