diff --git a/README.md b/README.md index f6cd7cb..368e156 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ But then you want to make sure each translation is unique for its language. That's where this package comes in to play. -This package also supports [`spatie/nova-translatable`](https://github.com/spatie/nova-translatable/) in case you are using [Laravel Nova](https://nova.laravel.com/). +This package also supports [`spatie/nova-translatable`](https://github.com/spatie/nova-translatable/) in case you are using [Laravel Nova](https://nova.laravel.com/) and [`filamentphp/spatie-laravel-translatable-plugin`](https://github.com/filamentphp/spatie-laravel-translatable-plugin) in case you are using [Filament](https://filamentphp.com/). ## ✅ Requirements @@ -44,6 +44,7 @@ This package also supports [`spatie/nova-translatable`](https://github.com/spati - [Laravel](https://laravel.com/) >= 6 - [spatie/laravel-translatable](https://github.com/spatie/laravel-translatable) ^4.4|^5.0 - [spatie/nova-translatable](https://github.com/spatie/nova-translatable/) ^3.0 +- [filamentphp/spatie-laravel-translatable-plugin](https://github.com/filamentphp/spatie-laravel-translatable-plugin) ^3.0 ## 📦 Installation @@ -205,6 +206,26 @@ Text::make(__('Slug'), 'slug') ->updateRules('unique_translation:posts,slug,{{resourceId}}'); ``` +### ☑️ Filament + +If you are using [Filament](https://filamentphp.com/) in combination with [`filamentphp/spatie-laravel-translatable-plugin`](https://github.com/filamentphp/spatie-laravel-translatable-plugin), then you can add the validation rule like this: + +```php +TextInput::make('slug') + ->title(__('Slug')) + ->rules([ + UniqueTranslationRule::for('posts', 'slug') + ]) +``` + +```php +TextInput::make('slug') + ->title(__('Slug')) + ->rules([ + fn (Get $get) => UniqueTranslationRule::for('posts', 'slug')->ignore($get('id')) + ]) +``` + ## 🖥 Example Your existing `slug` column (JSON) in a `posts` table: diff --git a/src/UniqueTranslationValidator.php b/src/UniqueTranslationValidator.php index 9d7f4f5..39f1a8f 100644 --- a/src/UniqueTranslationValidator.php +++ b/src/UniqueTranslationValidator.php @@ -20,10 +20,14 @@ class UniqueTranslationValidator * @return bool */ public function validate($attribute, $value, $parameters, $validator) - { - list ($name, $locale) = $this->isNovaTranslation($attribute) + { + list ($name, $locale) = $this->isNovaTranslation($attribute) ? $this->getNovaAttributeNameAndLocale($attribute) - : $this->getArrayAttributeNameAndLocale($attribute); + : ( + $this->isFilamentTranslation($attribute) + ? $this->getFilamentAttributeNameAndLocale($attribute) + : $this->getArrayAttributeNameAndLocale($attribute) + ); if ($this->isUnique($value, $name, $locale, $parameters)) { return true; @@ -74,7 +78,7 @@ protected function isNovaTranslation($attribute) } /** - * Get the attribute name and locale of a Nova translation field. + * Get the attribute name and locale of a Filament translation field. * * @param string $attribute * @@ -87,6 +91,32 @@ protected function getNovaAttributeNameAndLocale($attribute) return $this->getAttributeNameAndLocale($attribute, '_'); } + /** + * Check if the attribute is a Filament translation field name. + * + * @param string $attribute + * + * @return bool + */ + protected function isFilamentTranslation($attribute) + { + return strpos($attribute, 'data.') === 0; + } + + /** + * Get the attribute name and locale of a Filament translation field. + * + * @param string $attribute + * + * @return array + */ + protected function getFilamentAttributeNameAndLocale($attribute) + { + $attribute = str_replace('data.', '', $attribute); + @list($locale, $name) = @explode('.', $attribute); + return [$name, $locale]; + } + /** * Get the attribute name and locale of an array field. * @@ -210,7 +240,7 @@ protected function isUnique($value, $name, $locale, $parameters) $query = $this->findTranslation($connection, $table, $column, $locale, $value); $query = $this->ignore($query, $ignoreColumn, $ignoreValue); $query = $this->addConditions($query, $this->getUniqueExtra($parameters)); - + $isUnique = $query->count() === 0; return $isUnique;