Skip to content

DependsOn array values for matching multiple different values #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Page extends Resource

The package supports two kinds of dependencies:

1. `->dependsOn('field', 'value')`
1. `->dependsOn('field', 'value')` or `->dependsOn('field', ['value1', 'value2'])` (using 'or' matching)
2. `->dependsOnNotEmpty('field')`

These dependencies can be combined by chaining the methods on the `NovaDependencyContainer`:
Expand Down
2 changes: 1 addition & 1 deletion dist/js/field.js

Large diffs are not rendered by default.

17 changes: 13 additions & 4 deletions resources/js/components/FormField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,19 @@
this.dependenciesSatisfied = false;
return;
}

if(dependency.hasOwnProperty('value') && this.dependencyValues[dependency.field] !== dependency.value) {
this.dependenciesSatisfied = false;
return;

if(dependency.hasOwnProperty('values')) {
let valuesSatisfied = false;
for (let value of dependency.values) {
// Use coercion as form values are always string
if (this.dependencyValues[dependency.field] == value) {
valuesSatisfied = true;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a nice optimization here might be to return as soon as we find a value satisfied. There is no point to check other conditions if one matches. Also you could instead use the js includes method to make this even cleaner instead of a local variable (valuesSatisfied) and the for loop

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ragingdave that’s basically the only problem I have with this package. When chaining multiple dependsOn conditions, it will always resolve in an or statement, when dependencies get resolved as soon as one statement resolves to be true.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I agree with you, and might have even said so in an issue about this same topic, that level of improvement for this package seems a very far ways off. I mean this with no disrepect, but clearly you don't have the time for that big of an upgrade, and quite frankly neither do I. Perhaps this particular PR, would be valuable to further improve the 1.x line, with perhaps the query builder like syntax/chaining being something for a v2 or beyond.

}
}
if ( ! valuesSatisfied ) {
this.dependenciesSatisfied = false;
return;
}
}
}

Expand Down
14 changes: 9 additions & 5 deletions src/NovaDependencyContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ public function __construct($fields, $attribute = null, $resolveCallback = null)
* Adds a dependency
*
* @param $field
* @param $value
* @param $values
* @return $this
*/
public function dependsOn($field, $value)
public function dependsOn($field, $values)
{
return $this->withMeta([
'dependencies' => array_merge($this->meta['dependencies'], [['field' => $field, 'value' => $value]])
'dependencies' => array_merge($this->meta['dependencies'], [['field' => $field, 'values' => (array) $values]])
]);
}

Expand Down Expand Up @@ -74,8 +74,12 @@ public function resolveForDisplay($resource, $attribute = null)
$this->meta['dependencies'][$index]['satisfied'] = true;
}

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