-
Notifications
You must be signed in to change notification settings - Fork 372
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
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
58ca168
extend validation rules
wychoong ca77ae2
fix
wychoong e142f01
Merge branch 'main' into extend-validation
wychoong ef7d6a2
Merge branch 'main' into extend-validation
wychoong 6b6b6d9
revert config related changes
wychoong 354c1f5
extendValidation method
wychoong 82bc731
fix
wychoong 0c82198
Merge branch 'develop' into extend-validation
alecritson 1c94253
display error msg for channel & customer group
wychoong 348a914
add extend validation message
wychoong b7df3d6
doc
wychoong a33d781
Merge branch 'develop' into pr/443
wychoong 1f38e77
Update CanExtendValidation.php
wychoong 2ae57f8
Update config.js
wychoong fa4be2f
Update validations.md
wychoong 8953db4
remove commented code
wychoong 737c144
Merge branch 'main' into pr/443
alecritson dfc5c71
Pint fixes
alecritson 214c708
Try fix style mismatch
alecritson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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