Skip to content

DependsOnNot - added ability to set property as array #183

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 13 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
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ A container for grouping fields that depend on other field values. Dependencies

### Versions

- install v1.2.x for Laravel v5.8 or v6.x and Nova 2.x
- install v1.1.2 for Laravel v5.7 and Nova v1.x
- install v1.2.x for Laravel v5.8 or v6.x and Nova 2.x
- install v1.1.2 for Laravel v5.7 and Nova v1.x

<br />

Expand Down Expand Up @@ -72,7 +72,7 @@ class Page extends Resource
The package supports four kinds of dependencies:

1. `->dependsOn('field', 'value')`
2. `->dependsOnNot('field', 'value')`
2. `->dependsOnNot('field', 'value')` or `->dependsOnNot('field', ['value1', 'value2', 'value3'])`
3. `->dependsOnEmpty('field')`
4. `->dependsOnNotEmpty('field')`
5. `->dependsOnNullOrZero('field')`
Expand All @@ -88,7 +88,7 @@ NovaDependencyContainer::make([
->dependsOn('field3', 'value3')
```

The fields used as dependencies can be of any Laravel Nova field type. Currently only two relation field types are supported, `BelongsTo` and `MorphTo`.
The fields used as dependencies can be of any Laravel Nova field type. Currently only two relation field types are supported, `BelongsTo` and `MorphTo`.

Here is an example using a checkbox:

Expand Down Expand Up @@ -120,7 +120,7 @@ When the `Post` resource with `id` 2 is being selected, a `Boolean` field will a

A [BelongsToMany](https://nova.laravel.com/docs/2.0/resources/relationships.html#belongstomany) setup is similar to that of a [BelongsTo](https://nova.laravel.com/docs/2.0/resources/relationships.html#belongsto).

The `dependsOn` method should be pointing to the name of the intermediate table. If it is called `role_user`, the setup should be
The `dependsOn` method should be pointing to the name of the intermediate table. If it is called `role_user`, the setup should be

```php
BelongsToMany::make('Roles')
Expand Down
2 changes: 1 addition & 1 deletion dist/js/field.js

Large diffs are not rendered by default.

224 changes: 112 additions & 112 deletions resources/js/components/FormField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,143 +15,143 @@
</template>

<script>
import {FormField, HandlesValidationErrors} from 'laravel-nova'
import {FormField, HandlesValidationErrors} from 'laravel-nova'

export default {
mixins: [FormField, HandlesValidationErrors],
export default {
mixins: [FormField, HandlesValidationErrors],

props: ['resourceName', 'resourceId', 'field'],
props: ['resourceName', 'resourceId', 'field'],

mounted() {
this.registerDependencyWatchers(this.$root, function() {
this.updateDependencyStatus();
});
},
mounted() {
this.registerDependencyWatchers(this.$root, function () {
this.updateDependencyStatus();
});
},

data() {
return {
dependencyValues: {},
dependenciesSatisfied: false,
}
},
data() {
return {
dependencyValues: {},
dependenciesSatisfied: false,
}
},

methods: {

// @todo: refactor entire watcher procedure, this approach isn't maintainable ..
registerDependencyWatchers(root, callback) {
callback = callback || null;
root.$children.forEach(component => {
if (this.componentIsDependency(component)) {

// @todo: change `findWatchableComponentAttribute` to return initial state(s) of current dependency.
let attribute = this.findWatchableComponentAttribute(component),
initial_value = component.field.value; // @note: quick-fix for issue #88

component.$watch(attribute, (value) => {
// @todo: move to reactive factory
if (attribute === 'selectedResource') {
value = (value && value.value) || null;
}
this.dependencyValues[component.field.attribute] = value;
// @todo: change value as argument for `updateDependencyStatus`
this.updateDependencyStatus()
}, {immediate: true});

// @todo: move to initial state
// @note quick-fix for issue #88
if (attribute === 'fieldTypeName') {
initial_value = component.field.resourceLabel;
}
methods: {

// @todo: replace with `updateDependencyStatus(initial_value)` and let it resolve dependency state
this.dependencyValues[component.field.attribute] = initial_value;
}
// @todo: refactor entire watcher procedure, this approach isn't maintainable ..
registerDependencyWatchers(root, callback) {
callback = callback || null;
root.$children.forEach(component => {
if (this.componentIsDependency(component)) {

this.registerDependencyWatchers(component)
});
// @todo: change `findWatchableComponentAttribute` to return initial state(s) of current dependency.
let attribute = this.findWatchableComponentAttribute(component),
initial_value = component.field.value; // @note: quick-fix for issue #88

if (callback !== null) {
callback.call(this);
}
},

// @todo: not maintainable, move to factory
findWatchableComponentAttribute(component) {
let attribute;
switch(component.field.component) {
case 'belongs-to-many-field':
case 'belongs-to-field':
attribute = 'selectedResource';
break;
case 'morph-to-field':
attribute = 'fieldTypeName';
break;
default:
attribute = 'value';
}
return attribute;
},
component.$watch(attribute, (value) => {
// @todo: move to reactive factory
if (attribute === 'selectedResource') {
value = (value && value.value) || null;
}
this.dependencyValues[component.field.attribute] = value;
// @todo: change value as argument for `updateDependencyStatus`
this.updateDependencyStatus()
}, {immediate: true});

// @todo: move to initial state
// @note quick-fix for issue #88
if (attribute === 'fieldTypeName') {
initial_value = component.field.resourceLabel;
}

componentIsDependency(component) {
if (component.field === undefined) {
return false;
// @todo: replace with `updateDependencyStatus(initial_value)` and let it resolve dependency state
this.dependencyValues[component.field.attribute] = initial_value;
}

for (let dependency of this.field.dependencies) {
// #93 compatability with flexible-content, which adds a generated attribute for each field
if (component.field.attribute === (this.field.attribute + dependency.field)) {
return true;
}
}
this.registerDependencyWatchers(component)
});

if (callback !== null) {
callback.call(this);
}
},

// @todo: not maintainable, move to factory
findWatchableComponentAttribute(component) {
let attribute;
switch (component.field.component) {
case 'belongs-to-many-field':
case 'belongs-to-field':
attribute = 'selectedResource';
break;
case 'morph-to-field':
attribute = 'fieldTypeName';
break;
default:
attribute = 'value';
}
return attribute;
},

componentIsDependency(component) {
if (component.field === undefined) {
return false;
},
}

// @todo: align this method with the responsibility of updating the dependency, not verifying the dependency "values"
updateDependencyStatus() {
for (let dependency of this.field.dependencies) {
for (let dependency of this.field.dependencies) {
// #93 compatability with flexible-content, which adds a generated attribute for each field
if (component.field.attribute === (this.field.attribute + dependency.field)) {
return true;
}
}

// #93 compatability with flexible-content, which adds a generated attribute for each field
let dependencyValue = this.dependencyValues[(this.field.attribute + dependency.field)];
if (dependency.hasOwnProperty('empty') && !dependencyValue) {
this.dependenciesSatisfied = true;
return;
}
return false;
},

if (dependency.hasOwnProperty('notEmpty') && dependencyValue) {
this.dependenciesSatisfied = true;
return;
}
// @todo: align this method with the responsibility of updating the dependency, not verifying the dependency "values"
updateDependencyStatus() {
for (let dependency of this.field.dependencies) {

if (dependency.hasOwnProperty('nullOrZero') && 1 < [undefined, null, 0, '0'].indexOf(dependencyValue) ) {
this.dependenciesSatisfied = true;
return;
}
// #93 compatability with flexible-content, which adds a generated attribute for each field
let dependencyValue = this.dependencyValues[(this.field.attribute + dependency.field)];
if (dependency.hasOwnProperty('empty') && !dependencyValue) {
this.dependenciesSatisfied = true;
return;
}

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

if (dependency.hasOwnProperty('value') && dependencyValue == dependency.value) {
this.dependenciesSatisfied = true;
return;
}
if (dependency.hasOwnProperty('nullOrZero') && -1 < [undefined, null, 0, '0'].indexOf(dependencyValue)) {
this.dependenciesSatisfied = true;
return;
}

this.dependenciesSatisfied = false;
},
if (dependency.hasOwnProperty('not') && -1 === dependency.not.indexOf(dependencyValue)) {
this.dependenciesSatisfied = true;
return;
}

fill(formData) {
if (this.dependenciesSatisfied) {
_.each(this.field.fields, field => {
if (field.fill) {
field.fill(formData)
}
})
if (dependency.hasOwnProperty('value') && dependencyValue == dependency.value) {
this.dependenciesSatisfied = true;
return;
}
}

this.dependenciesSatisfied = false;
},

fill(formData) {
if (this.dependenciesSatisfied) {
_.each(this.field.fields, field => {
if (field.fill) {
field.fill(formData)
}
})
}
}

}
}
</script>
12 changes: 7 additions & 5 deletions src/NovaDependencyContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,17 @@ public function dependsOn($field, $value)
}

/**
* Adds a dependency for not
* Adds a dependency for a field being NOT equal to a list of values
*
* @param $field
* @return NovaDependencyContainer
* @param $value
* @return $this
*/
public function dependsOnNot($field, $value)
public function dependsOnNot($field, $values)
{
return $this->withMeta([
'dependencies' => array_merge($this->meta['dependencies'], [
array_merge($this->getFieldLayout($field), ['not' => $value])
array_merge($this->getFieldLayout($field), ['not' => (array) $values])
])
]);
}
Expand Down Expand Up @@ -168,7 +169,8 @@ public function resolveForDisplay($resource, $attribute = null)
continue;
}

if (array_key_exists('not', $dependency) && $resource->{$dependency['property']} != $dependency['not']) {
// inverted
if (array_key_exists('not', $dependency) && !in_array($resource->{$dependency['property']}, (array) $dependency['not'], true)) {
$this->meta['dependencies'][$index]['satisfied'] = true;
continue;
}
Expand Down