Skip to content

Commit

Permalink
integrating various pull requests with fixes (#46)
Browse files Browse the repository at this point in the history
* added nvmrc file

* integrating various pull requests  (#45)

* Fix bug in nested forms, as discussed in issue #39 (#40)

* Fix dependencies and remove debugging

* Fix formUniqueId in nested forms

* Revert "Fix bug in nested forms, as discussed in issue #39 (#40)" (#44)

This reverts commit a2bcc77.

* Handle BackedEnum (#34)

* Handle BackedEnum
* Use ->value instead of tryFrom

* Get Correct Rules (#32)

Instead of trying to access the specific rule property and check if it is callable call the correct rule method instead so the individual field has a chance to handle the logic.

fixes #17

---------

Co-authored-by: Wajdi Jurry <[email protected]>
Co-authored-by: Lonny Kapelushnik <[email protected]>

---------

Co-authored-by: Wajdi Jurry <[email protected]>
Co-authored-by: Lonny Kapelushnik <[email protected]>
  • Loading branch information
3 people authored Jun 2, 2024
1 parent 7fba392 commit 72d2169
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lts/fermium
32 changes: 17 additions & 15 deletions src/DependencyContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Aqjw\MedialibraryField\Fields\Medialibrary;
use Aqjw\MedialibraryField\Fields\Support\MediaCollectionRules;
use BackedEnum;
use Illuminate\Support\Arr;
use Laravel\Nova\Fields\Field;
use Laravel\Nova\Http\Requests\NovaRequest;
Expand Down Expand Up @@ -334,9 +335,14 @@ public function areDependenciesSatisfied(NovaRequest $request)
if (array_key_exists('value', $dependency)
&& !array_key_exists('in', $dependency)
&& !array_key_exists('notin', $dependency)
&& !array_key_exists('nullOrZero', $dependency)
&& $dependency['value'] == $request->get($dependency['property'])) {
$satisfiedCounts++;
&& !array_key_exists('nullOrZero', $dependency)) {
if ($dependency['value'] instanceof BackedEnum) {
if ($dependency['value']->value == $request->get($dependency['property'])) {
$satisfiedCounts++;
}
} elseif ($dependency['value'] == $request->get($dependency['property'])) {
$satisfiedCounts++;
}
}
}

Expand All @@ -347,10 +353,10 @@ public function areDependenciesSatisfied(NovaRequest $request)
* Get a rule set based on field property name
*
* @param NovaRequest $request
* @param string $propertyName
* @param string $methodName
* @return array
*/
protected function getSituationalRulesSet(NovaRequest $request, string $propertyName = 'rules')
protected function getSituationalRulesSet(NovaRequest $request, string $methodName = 'getRules')
{
$fieldsRules = [$this->attribute => []];

Expand All @@ -366,22 +372,18 @@ protected function getSituationalRulesSet(NovaRequest $request, string $property
/** @var Field $field */
foreach ($this->meta['fields'] as $field) {
// if field is DependencyContainer, then add rules from dependant fields
if ($field instanceof DependencyContainer && $propertyName === "rules") {
$fieldsRules[Str::random()] = $field->getSituationalRulesSet($request, $propertyName);
if ($field instanceof DependencyContainer && $methodName === "getRules") {
$fieldsRules[Str::random()] = $field->getSituationalRulesSet($request, $methodName);
} elseif ($field instanceof Medialibrary) {
$rules = is_callable($field->{$propertyName})
? call_user_func($field->{$propertyName}, $request)
: $field->{$propertyName};
$rules = $field->{$methodName}($request);

$fieldsRules[$field->attribute] = MediaCollectionRules::make(
$rules,
$request,
$field,
);
} else {
$fieldsRules[$field->attribute] = is_callable($field->{$propertyName})
? call_user_func($field->{$propertyName}, $request)
: $field->{$propertyName};
$fieldsRules[$field->attribute] = $field->{$methodName}($request);
}
}

Expand Down Expand Up @@ -429,7 +431,7 @@ public function getRules(NovaRequest $request)
*/
public function getCreationRules(NovaRequest $request)
{
$fieldsRules = $this->getSituationalRulesSet($request, 'creationRules');
$fieldsRules = $this->getSituationalRulesSet($request, 'getCreationRules');

return array_merge_recursive(
$this->getRules($request),
Expand All @@ -445,7 +447,7 @@ public function getCreationRules(NovaRequest $request)
*/
public function getUpdateRules(NovaRequest $request)
{
$fieldsRules = $this->getSituationalRulesSet($request, 'updateRules');
$fieldsRules = $this->getSituationalRulesSet($request, 'getUpdateRules');

return array_merge_recursive(
$this->getRules($request),
Expand Down

0 comments on commit 72d2169

Please sign in to comment.