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

[0.2] Allow extending of validation rules #443

Merged
merged 19 commits into from
Dec 15, 2022
Merged
Show file tree
Hide file tree
Changes from 7 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
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,11 @@ class="flex items-center justify-between px-4 py-2 text-sm text-red-600 hover:bg
</div>
@endforeach
</div>
@error('collections')
<p class="mt-2 text-sm text-red-600">{{ $message }}</p>
@enderror
@error('collections.*')
<p class="mt-2 text-sm text-red-600">{{ $message }}</p>
@enderror
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace GetCandy\Hub\Http\Livewire\Components\Products;

use GetCandy\Hub\Http\Livewire\Traits\CanExtendValidation;
use GetCandy\Hub\Http\Livewire\Traits\HasAvailability;
use GetCandy\Hub\Http\Livewire\Traits\HasDimensions;
use GetCandy\Hub\Http\Livewire\Traits\HasImages;
Expand Down Expand Up @@ -42,6 +43,7 @@ abstract class AbstractProduct extends Component
use HasUrls;
use HasTags;
use HasSlots;
use CanExtendValidation;

/**
* The current product we are editing.
Expand Down Expand Up @@ -246,8 +248,11 @@ protected function rules()
$baseRules,
$this->hasImagesValidationRules(),
$this->withAttributesValidationRules(),
$this->hasUrlsValidationRules(! $this->product->id),
$this->withAttributesValidationRules()
$this->hasUrlsValidationRules(!$this->product->id),
$this->withAttributesValidationRules(),
$this->getExtendValidation([
'product' => $this->product,
]),
);
}

Expand Down Expand Up @@ -846,6 +851,8 @@ public function getSideMenuProperty()
'id' => 'collections',
'hidden' => false,
'has_errors' => $this->errorBag->hasAny([
'collections',
'collections.*'
]),
],
])->reject(fn ($item) => ($item['hidden'] ?? false));
Expand Down
48 changes: 48 additions & 0 deletions packages/admin/src/Http/Livewire/Traits/CanExtendValidation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace GetCandy\Hub\Http\Livewire\Traits;

use Closure;
use Illuminate\Support\Facades\Log;

trait CanExtendValidation
{

public static array $extendedValidationRules = [];

/**
* Extend validation rules
*
* @return void
*/
public static function extendValidation(array $validations): void
{
self::$extendedValidationRules = $validations;
}

/**
* Get extended validation rules
*
* @return array
*/
protected function getExtendValidation($parameters): array
{
return collect(self::$extendedValidationRules)
->map(function ($rules) use ($parameters) {

if (is_array($rules) || $rules instanceof Closure) {
if ($rules instanceof Closure) $rules = [$rules];

return collect($rules)->map(function ($rule) use ($parameters) {
if ($rule instanceof Closure) {
return app()->call($rule, $parameters);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this idea is taken from Filament on allowing injecting value for closure

}

return $rule;
})->toArray();
}

return $rules;
})->toArray();
}
}
8 changes: 4 additions & 4 deletions packages/admin/src/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ function max_upload_filesize()
}
}

if (! function_exists('get_validation')) {
if (!function_exists('get_validation')) {
glennjacobs marked this conversation as resolved.
Show resolved Hide resolved
function get_validation($reference, $field, $defaults = [], Model $model = null)
{
$config = config("getcandy-hub.{$reference}.{$field}", []);

$rules = $defaults;

$rules[] = ! empty($config['required']) ? 'required' : 'nullable';
$rules[] = !empty($config['required']) ? 'required' : 'nullable';

if (($config['unique'] ?? false) && $model) {
$rule = 'unique:'.get_class($model).','.$field;
$rule = 'unique:' . get_class($model) . ',' . $field;

if ($model->id) {
$rule .= ','.$model->id;
$rule .= ',' . $model->id;
}

$rules[] = $rule;
Expand Down