Skip to content

Commit

Permalink
update action field validation for latest compatible nova code
Browse files Browse the repository at this point in the history
  • Loading branch information
ragingdave committed Mar 4, 2021
1 parent f7b18a2 commit 089764f
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions src/ActionHasDependencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,13 @@
namespace Epartment\NovaDependencyContainer;

use Laravel\Nova\Http\Requests\ActionRequest;
use Illuminate\Support\Facades\Validator;

trait ActionHasDependencies
{
use HasChildFields;

/**
* Validate action fields
*
* @param \Laravel\Nova\Http\Requests\ActionRequest $request
* @return void
*/
public function validateFields(ActionRequest $request)
protected function fieldsForValidation(ActionRequest $request)
{
$availableFields = [];

Expand All @@ -33,9 +28,33 @@ public function validateFields(ActionRequest $request)
if ($this->childFieldsArr) {
$availableFields = array_merge($availableFields, $this->childFieldsArr);
}
}

/**
* Validate action fields. Mostly a copy paste from Nova
*
* Uses the above to validate only on fields that have satisfied dependencies.
*
* @param \Laravel\Nova\Http\Requests\ActionRequest $request
* @return void
*/
public function validateFields(ActionRequest $request)
{
$fields = collect($this->fieldsForValidation($request));

$this->validate(collect($availableFields)->mapWithKeys(function ($field) {
return $field->getCreationRules($this);
})->all());
return Validator::make(
$request->all(),
$fields->mapWithKeys(function ($field) use ($request) {
return $field->getCreationRules($request);
})->all(),
[],
$fields->reject(function ($field) {
return empty($field->name);
})->mapWithKeys(function ($field) {
return [$field->attribute => $field->name];
})->all()
)->after(function ($validator) use ($request) {
$this->afterValidation($request, $validator);
})->validate();
}
}

0 comments on commit 089764f

Please sign in to comment.