-
Notifications
You must be signed in to change notification settings - Fork 372
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[0.2] Allow extending of validation rules (#443)
* 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
1 parent
685013d
commit a27ce4b
Showing
3 changed files
with
99 additions
and
3 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
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` | | ||
|
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
60 changes: 60 additions & 0 deletions
60
packages/admin/src/Http/Livewire/Traits/CanExtendValidation.php
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,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; | ||
} | ||
} |