-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #157 from epartment/fix-actions
break out traits to fix Action vs ActionRequest usage
- Loading branch information
Showing
6 changed files
with
146 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace Epartment\NovaDependencyContainer; | ||
|
||
use Laravel\Nova\Http\Requests\ActionRequest; | ||
|
||
trait ActionHasDependencies | ||
{ | ||
use HasChildFields; | ||
|
||
/** | ||
* Validate action fields | ||
* | ||
* @param \Laravel\Nova\Http\Requests\ActionRequest $request | ||
* @return void | ||
*/ | ||
public function validateFields(ActionRequest $request) | ||
{ | ||
$availableFields = []; | ||
|
||
foreach ($this->fields() as $field) { | ||
if ($field instanceof NovaDependencyContainer) { | ||
// do not add any fields for validation if container is not satisfied | ||
if($field->areDependenciesSatisfied($this)) { | ||
$availableFields[] = $field; | ||
$this->extractChildFields($field->meta['fields']); | ||
} | ||
} else { | ||
$availableFields[] = $field; | ||
} | ||
} | ||
|
||
if ($this->childFieldsArr) { | ||
$availableFields = array_merge($availableFields, $this->childFieldsArr); | ||
} | ||
|
||
$this->validate(collect($availableFields)->mapWithKeys(function ($field) { | ||
return $field->getCreationRules($this); | ||
})->all()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace Epartment\NovaDependencyContainer; | ||
|
||
trait HasChildFields | ||
{ | ||
protected $childFieldsArr = []; | ||
|
||
/** | ||
* @param [array] $childFields [meta fields] | ||
* @return void | ||
*/ | ||
protected function extractChildFields($childFields) | ||
{ | ||
foreach ($childFields as $childField) { | ||
if ($childField instanceof NovaDependencyContainer) { | ||
$this->extractChildFields($childField->meta['fields']); | ||
} else { | ||
if (array_search($childField->attribute, array_column($this->childFieldsArr, 'attribute')) === false) { | ||
// @todo: we should not randomly apply rules to child-fields. | ||
$childField = $this->applyRulesForChildFields($childField); | ||
$this->childFieldsArr[] = $childField; | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @param [array] $childField | ||
* @return [array] $childField | ||
*/ | ||
protected function applyRulesForChildFields($childField) | ||
{ | ||
if (isset($childField->rules)) { | ||
$childField->rules[] = "sometimes:required:".$childField->attribute; | ||
} | ||
if (isset($childField->creationRules)) { | ||
$childField->creationRules[] = "sometimes:required:".$childField->attribute; | ||
} | ||
if (isset($childField->updateRules)) { | ||
$childField->updateRules[] = "sometimes:required:".$childField->attribute; | ||
} | ||
return $childField; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters