diff --git a/docs/src/extending/validations.md b/docs/src/extending/validations.md new file mode 100644 index 0000000000..4640eef29c --- /dev/null +++ b/docs/src/extending/validations.md @@ -0,0 +1,31 @@ +# Validations + +[[toc]] + +## Overview + +If you want to add additional validation rules, you can do so by registering in service provider. + +## Extending Validation Rules + +```php +use Lunar\Hub\Http\Livewire\Components\Products\ProductCreate; +use Lunar\Models\Product; + +public function boot() { + ProductCreate::extendValidation([ + 'variant.sku' => ['required', 'min:8'], + 'collections' => ['required', 'array', function (Product $product) { + return function ($attribute, $value, $fail) use (Product $product) { + // closure validation + $fail($product->translateAttribute('name') . " validation failed"); + }; + }], + ]); +} +``` + +| Type | Page | Closure parameters | +| ------- | ---------------------------------------------------------------------------------------------------------------------------- | -------------------------------- | +| Product | `\Lunar\Hub\Http\Livewire\Components\Products\ProductCreate`
`\Lunar\Hub\Http\Livewire\Components\Products\ProductShow` | `\Lunar\Models\Product $product` | + \ No newline at end of file diff --git a/packages/admin/src/Http/Livewire/Components/Products/AbstractProduct.php b/packages/admin/src/Http/Livewire/Components/Products/AbstractProduct.php index 774ff49c8c..2f6c98fc6b 100644 --- a/packages/admin/src/Http/Livewire/Components/Products/AbstractProduct.php +++ b/packages/admin/src/Http/Livewire/Components/Products/AbstractProduct.php @@ -8,6 +8,7 @@ use Illuminate\Validation\Validator; use Livewire\Component; use Livewire\WithFileUploads; +use Lunar\Hub\Http\Livewire\Traits\CanExtendValidation; use Lunar\Hub\Http\Livewire\Traits\HasAvailability; use Lunar\Hub\Http\Livewire\Traits\HasDimensions; use Lunar\Hub\Http\Livewire\Traits\HasImages; @@ -43,6 +44,7 @@ abstract class AbstractProduct extends Component use HasUrls; use HasTags; use HasSlots; + use CanExtendValidation; /** * The current product we are editing. @@ -204,7 +206,8 @@ protected function getValidationMessages() return array_merge( [], $this->hasPriceValidationMessages(), - $this->withAttributesValidationMessages() + $this->withAttributesValidationMessages(), + $this->getExtendedValidationMessages(), ); } @@ -276,7 +279,10 @@ protected function rules() $baseRules, $this->hasImagesValidationRules(), $this->hasUrlsValidationRules(! $this->product->id), - $this->withAttributesValidationRules() + $this->withAttributesValidationRules(), + $this->getExtendedValidationRules([ + 'product' => $this->product, + ]), ); } @@ -368,7 +374,6 @@ public function save() })->validate(null, $this->getValidationMessages()); $this->validateUrls(); - $isNew = ! $this->product->id; DB::transaction(function () use ($isNew) { diff --git a/packages/admin/src/Http/Livewire/Traits/CanExtendValidation.php b/packages/admin/src/Http/Livewire/Traits/CanExtendValidation.php new file mode 100644 index 0000000000..bbb8331ec1 --- /dev/null +++ b/packages/admin/src/Http/Livewire/Traits/CanExtendValidation.php @@ -0,0 +1,60 @@ +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); + } + + return $rule; + })->toArray(); + } + + return $rules; + })->toArray(); + } + + /** + * Get extended validation messages + * + * @return array + */ + protected function getExtendedValidationMessages(): array + { + return self::$extendedValidationMessages; + } +}