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 all 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
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);
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();
}

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