Skip to content

Commit

Permalink
[0.2] Allow extending of validation rules (#443)
Browse files Browse the repository at this point in the history
* extend validation rules

* fix

* revert config related changes

* extendValidation method

* fix

* display error msg for channel & customer group

* add extend validation message

* doc

* Update CanExtendValidation.php

* Update config.js

* Update validations.md

* remove commented code

* Pint fixes

* Try fix style mismatch

Co-authored-by: Alec Ritson <hello@itsalec.co.uk>
  • Loading branch information
wychoong and alecritson authored Dec 15, 2022
1 parent 685013d commit a27ce4b
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 3 deletions.
31 changes: 31 additions & 0 deletions docs/src/extending/validations.md
Original file line number Diff line number Diff line change
@@ -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`<br />`\Lunar\Hub\Http\Livewire\Components\Products\ProductShow` | `\Lunar\Models\Product $product` |

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -43,6 +44,7 @@ abstract class AbstractProduct extends Component
use HasUrls;
use HasTags;
use HasSlots;
use CanExtendValidation;

/**
* The current product we are editing.
Expand Down Expand Up @@ -204,7 +206,8 @@ protected function getValidationMessages()
return array_merge(
[],
$this->hasPriceValidationMessages(),
$this->withAttributesValidationMessages()
$this->withAttributesValidationMessages(),
$this->getExtendedValidationMessages(),
);
}

Expand Down Expand Up @@ -276,7 +279,10 @@ protected function rules()
$baseRules,
$this->hasImagesValidationRules(),
$this->hasUrlsValidationRules(! $this->product->id),
$this->withAttributesValidationRules()
$this->withAttributesValidationRules(),
$this->getExtendedValidationRules([
'product' => $this->product,
]),
);
}

Expand Down Expand Up @@ -368,7 +374,6 @@ public function save()
})->validate(null, $this->getValidationMessages());

$this->validateUrls();

$isNew = ! $this->product->id;

DB::transaction(function () use ($isNew) {
Expand Down
60 changes: 60 additions & 0 deletions packages/admin/src/Http/Livewire/Traits/CanExtendValidation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Lunar\Hub\Http\Livewire\Traits;

use Closure;

trait CanExtendValidation
{
public static array $extendedValidationRules = [];

public static array $extendedValidationMessages = [];

/**
* Extend validation rules
*
* @return void
*/
public static function extendValidation(array $rules, array $messages = []): void
{
self::$extendedValidationRules = $rules;
self::$extendedValidationMessages = $messages;
}

/**
* Get extended validation rules
*
* @return array
*/
protected function getExtendedValidationRules($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);
}

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

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

/**
* Get extended validation messages
*
* @return array
*/
protected function getExtendedValidationMessages(): array
{
return self::$extendedValidationMessages;
}
}

0 comments on commit a27ce4b

Please sign in to comment.