Skip to content
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

Added ->dependsOnIn('field', ['value1', 'value2']) and ->dependsOnNotIn('field', ['value1', 'value2']) #201

Open
wants to merge 11 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
44 changes: 23 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Page extends Resource
public function fields(Request $request)
{
return [

Select::make('Name format', 'name_format')->options([
0 => 'First Name',
1 => 'First Name / Last Name',
Expand All @@ -72,10 +72,12 @@ 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', 'value2'])`
3. `->dependsOnNot('field', 'value')`
4. `->dependsOnNotIn('field', ['value', 'value2'])`
5. `->dependsOnEmpty('field')`
6. `->dependsOnNotEmpty('field')`
7. `->dependsOnNullOrZero('field')`

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

Expand All @@ -88,7 +90,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 +122,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 All @@ -143,26 +145,26 @@ Here is an (ugly) example of a get/set mutator setup for an intermediate table u

```php
// model User
class User ... {
class User ... {

public function roles() {
return $this->belongsToMany->using(RoleUser::class)->withPivot('rules_all');
}

}

// model Role
class Role ... {
class Role ... {

public function users() {
return $this->belongsToMany->using(RoleUser::class)->withPivot('rules_all');
}

}

// intermediate table
use Illuminate\Database\Eloquent\Relations\Pivot;
class RoleUser extends Pivot {
class RoleUser extends Pivot {

protected $table 'role_user';

Expand All @@ -186,25 +188,25 @@ And now for the dependency container.
NovaDependencyContainer::make([
// pivot field rules_all
Select::make('Type', 'type_1')
->options([
/* some options */
->options([
/* some options */
])
->displayUsingLabels()
])
->dependsOn('role_user', 1)
,

NovaDependencyContainer::make([
// pivot field rules_all
Select::make('Type', 'type_2')
->options([
/* different options */
->options([
/* different options */
])
->displayUsingLabels()
])
->dependsOn('role_user', 2)
,

// .. and so on
]
}),
Expand All @@ -230,7 +232,7 @@ NovaDependencyContainer::make([
Text::make('Additional Text', 'additional'),
Boolean::make('Visible', 'visible')
])
->dependsOn('commentable', 'Post')
->dependsOn('commentable', 'Post')
```

<br />
Expand Down
2 changes: 1 addition & 1 deletion dist/js/field.js

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions resources/js/components/FormField.vue
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@
return;
}

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

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

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

/**
* Adds a dependency for in
*
* @param $field
* @param $array
* @return $this
*/
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
*
Expand All @@ -67,6 +83,23 @@ public function dependsOnNot($field, $value)
]);
}

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


/**
* Adds a dependency for not empty
*
Expand Down Expand Up @@ -173,6 +206,17 @@ 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('notin', $dependency) && !in_array($resource->{$dependency['property']}, $dependency['notin'])) {
$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