codezero-be / laravel-unique-translation Goto Github PK
View Code? Open in Web Editor NEW⭐️ Check if a translated value in a JSON column is unique in the database.
License: MIT License
⭐️ Check if a translated value in a JSON column is unique in the database.
License: MIT License
Hi!
Fantastic package. Works great during browser usage, but I get a really weird error when testing the form I want to validate.
Basics:
I am using Laravel-translatable from Spatie and want to check that the title is unique in a JSON column for a specific locale. My form is really simple:
<label>TítuloES</label><br>
<input class="border" type="text" name="title[es]"><br>
<label>TítuloEN</label><br>
<input class="border" type="text" name="title[en]"><br>
My validation looks like this:
$attributes = request()->validate([
'title.*' => 'required|max:255|unique_translation:posts,title',
'summary.*' => 'required',
'content.*' => 'required',
'imagetitle' => [
'sometimes',
'mimes:jpeg,jpg,png'
]
]);
When I use the web, it works, but when I test it, I get a super long error, but the most important part seems this one:
The following exception occurred during the last request:
ErrorException: Undefined array key "collation" in E:\Programas\laragon\www\webtronica\vendor\codezero\laravel-unique-translation\src\UniqueTranslationValidator.php:238
Stack trace:
#0 E:\Programas\laragon\www\myapp\vendor\laravel\framework\src\Illuminate\Foundation\Bootstrap\HandleExceptions.php(259): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(2, 'Undefined array...', 'E:\\Programas\\la...', 238)
#1 E:\Programas\laragon\www\myapp\vendor\codezero\laravel-unique-translation\src\UniqueTranslationValidator.php(238): Illuminate\Foundation\Bootstrap\HandleExceptions->Illuminate\Foundation\Bootstrap\{closure}(2, 'Undefined array...', 'E:\\Programas\\la...', 238)
#2 E:\Programas\laragon\www\myapp\vendor\laravel\framework\src\Illuminate\Database\Query\Builder.php(1551): CodeZero\UniqueTranslation\UniqueTranslationValidator->CodeZero\UniqueTranslation\{closure}(Object(Illuminate\Database\Query\Builder))
I guess the package is trying to get some info from the DB, but it is not getting it. Then, everything breaks. So, I have 2 questions:
Should I somehow modify configuration of PHP unit? Now it looks like this:
<php>
<server name="APP_ENV" value="testing"/>
<server name="BCRYPT_ROUNDS" value="4"/>
<server name="CACHE_DRIVER" value="array"/>
<server name="DB_CONNECTION" value="sqlite"/>
<server name="DB_DATABASE" value=":memory:"/>
<server name="MAIL_DRIVER" value="array"/>
<server name="QUEUE_CONNECTION" value="sync"/>
<server name="SESSION_DRIVER" value="array"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="RECAPTCHA_KEY" value="6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI"/>
<env name="RECAPTCHA_SECRET" value="6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe"/>
</php>
</phpunit>
Question number 2: when using browser and submitting the form, no error is seen and the post is created with success.
The collation fields of my database are empty, so I don't understand how its working:
Many thanks!!!
Hello,
I tried to use this great package with Filament which handles translations using spatie/nova-translatable in a simple way like Laravel Nova, unfortunately this is not working (allowing duplication).
Laravel Nova
Text::make(__('Slug'), 'slug')
->creationRules('unique_translation:posts,slug')
->updateRules('unique_translation:posts,slug,{{resourceId}}');
Filament (assumed and tested)
Forms\Components\TextInput::make('slug')->required()
->rules(['unique_translation:posts,slug']),
Thanks.
How can I ignore soft deleted records?
'FIELD_NAME.*' => [
UniqueTranslationRule::for('TABLE_NAME', 'FIELD_NAME')->ignore(!0, 'deleted_at') -> NO WORKS
UniqueTranslationRule::for('TABLE_NAME', 'FIELD_NAME')->ignore(!NULL, 'deleted_at') -> NO WORKS
UniqueTranslationRule::for('TABLE_NAME', 'FIELD_NAME')->whereNull('deleted_at') -> NO WORKS
]
My hosting provider does not support MySQL version 5.7. Maximum supported version is 5.6.46. So I'm facing JSON_EXTRACT and JSON_UNQUOTE function does not exist error.
Is there any fix on that?
I form for saving the category. The name field is unique. I'm using FormRequest
for validating the form. Create works fine. but while updating getting error showing The name.en has already been taken.
form fields
<input class="form-control" name="name[en]" type="text" id="name-en" value="{{ old('name[en]', optional($serviceCategory)->getTranslation('name','en')) }}" minlength="1" maxlength="255" placeholder="{{ __('preferencesCategory.name_en__placeholder') }}">
<input class="form-control" name="name[ar]" type="text" id="name-ar" value="{{ old('name[ar]', optional($serviceCategory)->getTranslation('name','ar')) }}" minlength="1" maxlength="255" placeholder="{{ __('preferencesCategory.name_ar__placeholder') }}">
FormRequest.php
public function rules()
{
return [
'code' => 'string|min:1|nullable',
'name.*' => 'required|string|min:1|max:255|unique_translation:service_categories,name,{$this->serviceCategory->id}',
'status' => 'string|min:1|nullable',
];
web.php (route file)
Route::put('service_category/{serviceCategory}', 'ServiceCategoriesController@update')
->name('service_categories.service_category.update')->where('id', '[0-9]+');
controller.php
public function update(ServiceCategory $serviceCategory, ServiceCategoriesFormRequest $request)
{
try {
$data = $request->getData();
$data['updated_by'] = \Auth::guard('admin')->user()->id;
$serviceCategory->update($data);
return redirect()->route('service_categories.service_category.index')
->with('success_message', 'Service Category was successfully updated.');
} catch (Exception $exception) {
return back()->withInput()
->withErrors(['unexpected_error' => 'Unexpected error occurred while trying to process your request.']);
}
}
atm to get the error you will use name.code
but is there a way to pass an option that make the error msg unified whether am using a code or not ?
ex.
// form
<input name="slug[en]" value="abc">
<input name="slug[nl]" value="abc">
// validate
$attributes = request()->validate([
'slug.*' => ["unique_translation:posts,slug,{$post->id}"],
]);
// error
@if($errors->has('slug'))
{{ $errors->first('slug') }}
@endif
this way , either i use a single input or array, the error condition doesnt change.
atm, here is how i do the validation
foreach ($locales as $code) {
$v = Validator::make($request->all(), ["slug.$code" => "unique:pages,slug->$code," . $id ?: '']);
if ($v->fails()) {
$validator->errors()->add('slug', 'this slug is already taken');
}
}
also is it possible to use the validation rule with |
pipe rules or do i have to use the array instead ?
....
In this case:
{
"en":"unique_title", //scoped for en
"lt":"unique_title", //scoped for lt
}
<input name="title_en" value="unique_title" />
<input name="title_lt" value="some other title"/>
I want to have different validation for each input like:
$attributes = request()->validate([
'title_lt' => 'required|unique_translation:posts:title',
'title_en' => 'unique_translation:posts:title',
]);
how would i check against a specific value? Or this will just work?
Hi
Thanks for your extension.
I have some trouble.
Example
<input type="text" name="post_name[de]">
<input type="text" name="post_name[en]">
class MyRequest extends FormRequest{
public function rules(): array
{
return [
'post_name.*' => [
UniqueTranslationRule::for('posts', 'name')
]
];
}}
When i put exists values and submit form, i got a lot of messages:
Is there any way to generate only one message for each language?
Best regards
It seems that the where
clause in the unique_translation
rule is case sensitive.
laravel-unique-translation/src/UniqueTranslationValidator.php
Lines 161 to 164 in 1eb3d56
According to my Google searches, MySQL is supposed to be case insensitive when using a collation that ends in _ci
(I'm using utf8mb4_unicode_ci
). But that's not what my tests are saying...
Is anyone else having this issue?
Hello,
I'm using this package with MariaDB 10.6.9, Laravel 9, PHP 8.1 and Spatie Laravel Translatable v6, but it is not working.
Is it compatible with this environment or only compatible only with MySQL?
Thanks.
am not sure if its an issue with the https://github.com/spatie/laravel-tags package or something is missing from the validation package,
but here is what i have
$request->validate([
'name.*' => 'unique_translation:tags,name,' . $id
]);
the db name column i will update is
{"en": "black"}
and will be replaced with
{
"en": "test",
"fr": null
}
but i keep getting the error that the item already exists in the db, so maybe u can help me with this one
How to use this with Laravel Nova?
Hi, I have an issue using the validation rule provided by the library.
I'm using
I'm developing a multilingual API application and when I use the validation rule in a Request
validator file I get an ErrorException
with this data:
{
"message": "Array to string conversion",
"exception": "ErrorException",
"file": "/var/www/html/vendor/codezero/laravel-unique-translation/src/UniqueTranslationValidator.php",
"line": 273,
"trace": [
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php",
"line": 255,
"function": "handleError",
"class": "Illuminate\\Foundation\\Bootstrap\\HandleExceptions",
"type": "->"
},
{
"file": "/var/www/html/vendor/codezero/laravel-unique-translation/src/UniqueTranslationValidator.php",
"line": 273,
"function": "Illuminate\\Foundation\\Bootstrap\\{closure}",
"class": "Illuminate\\Foundation\\Bootstrap\\HandleExceptions",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php",
"line": 1621,
"function": "CodeZero\\UniqueTranslation\\{closure}",
"class": "CodeZero\\UniqueTranslation\\UniqueTranslationValidator",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php",
"line": 783,
"function": "whereNested",
"class": "Illuminate\\Database\\Query\\Builder",
"type": "->"
},
{
"file": "/var/www/html/vendor/codezero/laravel-unique-translation/src/UniqueTranslationValidator.php",
"line": 271,
"function": "where",
"class": "Illuminate\\Database\\Query\\Builder",
"type": "->"
},
{
"file": "/var/www/html/vendor/codezero/laravel-unique-translation/src/UniqueTranslationValidator.php",
"line": 240,
"function": "findTranslation",
"class": "CodeZero\\UniqueTranslation\\UniqueTranslationValidator",
"type": "->"
},
{
"file": "/var/www/html/vendor/codezero/laravel-unique-translation/src/UniqueTranslationValidator.php",
"line": 32,
"function": "isUnique",
"class": "CodeZero\\UniqueTranslation\\UniqueTranslationValidator",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Validation/Validator.php",
"line": 1611,
"function": "validate",
"class": "CodeZero\\UniqueTranslation\\UniqueTranslationValidator",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Validation/Validator.php",
"line": 1596,
"function": "callClassBasedExtension",
"class": "Illuminate\\Validation\\Validator",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Validation/Validator.php",
"line": 1628,
"function": "callExtension",
"class": "Illuminate\\Validation\\Validator",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Validation/Validator.php",
"line": 657,
"function": "__call",
"class": "Illuminate\\Validation\\Validator",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Validation/Validator.php",
"line": 457,
"function": "validateAttribute",
"class": "Illuminate\\Validation\\Validator",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Validation/Validator.php",
"line": 492,
"function": "passes",
"class": "Illuminate\\Validation\\Validator",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Validation/ValidatesWhenResolvedTrait.php",
"line": 31,
"function": "fails",
"class": "Illuminate\\Validation\\Validator",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Providers/FormRequestServiceProvider.php",
"line": 30,
"function": "validateResolved",
"class": "Illuminate\\Foundation\\Http\\FormRequest",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php",
"line": 1302,
"function": "Illuminate\\Foundation\\Providers\\{closure}",
"class": "Illuminate\\Foundation\\Providers\\FormRequestServiceProvider",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php",
"line": 1266,
"function": "fireCallbackArray",
"class": "Illuminate\\Container\\Container",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php",
"line": 1252,
"function": "fireAfterResolvingCallbacks",
"class": "Illuminate\\Container\\Container",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php",
"line": 813,
"function": "fireResolvingCallbacks",
"class": "Illuminate\\Container\\Container",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Application.php",
"line": 957,
"function": "resolve",
"class": "Illuminate\\Container\\Container",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php",
"line": 731,
"function": "resolve",
"class": "Illuminate\\Foundation\\Application",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Application.php",
"line": 942,
"function": "make",
"class": "Illuminate\\Container\\Container",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Routing/ResolvesRouteDependencies.php",
"line": 85,
"function": "make",
"class": "Illuminate\\Foundation\\Application",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Routing/ResolvesRouteDependencies.php",
"line": 50,
"function": "transformDependency",
"class": "Illuminate\\Routing\\ControllerDispatcher",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Routing/ResolvesRouteDependencies.php",
"line": 29,
"function": "resolveMethodDependencies",
"class": "Illuminate\\Routing\\ControllerDispatcher",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php",
"line": 59,
"function": "resolveClassMethodDependencies",
"class": "Illuminate\\Routing\\ControllerDispatcher",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php",
"line": 40,
"function": "resolveParameters",
"class": "Illuminate\\Routing\\ControllerDispatcher",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Route.php",
"line": 259,
"function": "dispatch",
"class": "Illuminate\\Routing\\ControllerDispatcher",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Route.php",
"line": 205,
"function": "runController",
"class": "Illuminate\\Routing\\Route",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
"line": 806,
"function": "run",
"class": "Illuminate\\Routing\\Route",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 144,
"function": "Illuminate\\Routing\\{closure}",
"class": "Illuminate\\Routing\\Router",
"type": "->"
},
{
"file": "/var/www/html/vendor/rakutentech/laravel-request-docs/src/LaravelRequestDocsMiddleware.php",
"line": 49,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 183,
"function": "handle",
"class": "Rakutentech\\LaravelRequestDocs\\LaravelRequestDocsMiddleware",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php",
"line": 50,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 183,
"function": "handle",
"class": "Illuminate\\Routing\\Middleware\\SubstituteBindings",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php",
"line": 159,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php",
"line": 125,
"function": "handleRequest",
"class": "Illuminate\\Routing\\Middleware\\ThrottleRequests",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php",
"line": 87,
"function": "handleRequestUsingNamedLimiter",
"class": "Illuminate\\Routing\\Middleware\\ThrottleRequests",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 183,
"function": "handle",
"class": "Illuminate\\Routing\\Middleware\\ThrottleRequests",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php",
"line": 57,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 183,
"function": "handle",
"class": "Illuminate\\Auth\\Middleware\\Authenticate",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/sanctum/src/Http/Middleware/EnsureFrontendRequestsAreStateful.php",
"line": 25,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 144,
"function": "Laravel\\Sanctum\\Http\\Middleware\\{closure}",
"class": "Laravel\\Sanctum\\Http\\Middleware\\EnsureFrontendRequestsAreStateful",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 119,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/sanctum/src/Http/Middleware/EnsureFrontendRequestsAreStateful.php",
"line": 24,
"function": "then",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 183,
"function": "handle",
"class": "Laravel\\Sanctum\\Http\\Middleware\\EnsureFrontendRequestsAreStateful",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 119,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
"line": 805,
"function": "then",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
"line": 784,
"function": "runRouteWithinStack",
"class": "Illuminate\\Routing\\Router",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
"line": 748,
"function": "runRoute",
"class": "Illuminate\\Routing\\Router",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Router.php",
"line": 737,
"function": "dispatchToRoute",
"class": "Illuminate\\Routing\\Router",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
"line": 200,
"function": "dispatch",
"class": "Illuminate\\Routing\\Router",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 144,
"function": "Illuminate\\Foundation\\Http\\{closure}",
"class": "Illuminate\\Foundation\\Http\\Kernel",
"type": "->"
},
{
"file": "/var/www/html/vendor/barryvdh/laravel-debugbar/src/Middleware/InjectDebugbar.php",
"line": 66,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 183,
"function": "handle",
"class": "Barryvdh\\Debugbar\\Middleware\\InjectDebugbar",
"type": "->"
},
{
"file": "/var/www/html/app/Http/Middleware/LocalizationMiddleware.php",
"line": 44,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 183,
"function": "handle",
"class": "App\\Http\\Middleware\\LocalizationMiddleware",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php",
"line": 121,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php",
"line": 64,
"function": "handleStatefulRequest",
"class": "Illuminate\\Session\\Middleware\\StartSession",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 183,
"function": "handle",
"class": "Illuminate\\Session\\Middleware\\StartSession",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php",
"line": 21,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ConvertEmptyStringsToNull.php",
"line": 31,
"function": "handle",
"class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 183,
"function": "handle",
"class": "Illuminate\\Foundation\\Http\\Middleware\\ConvertEmptyStringsToNull",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php",
"line": 21,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TrimStrings.php",
"line": 40,
"function": "handle",
"class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 183,
"function": "handle",
"class": "Illuminate\\Foundation\\Http\\Middleware\\TrimStrings",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php",
"line": 27,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 183,
"function": "handle",
"class": "Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php",
"line": 99,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 183,
"function": "handle",
"class": "Illuminate\\Foundation\\Http\\Middleware\\PreventRequestsDuringMaintenance",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Http/Middleware/HandleCors.php",
"line": 62,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 183,
"function": "handle",
"class": "Illuminate\\Http\\Middleware\\HandleCors",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php",
"line": 119,
"function": "Illuminate\\Pipeline\\{closure}",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
"line": 175,
"function": "then",
"class": "Illuminate\\Pipeline\\Pipeline",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php",
"line": 144,
"function": "sendRequestThroughRouter",
"class": "Illuminate\\Foundation\\Http\\Kernel",
"type": "->"
},
{
"file": "/var/www/html/public/index.php",
"line": 51,
"function": "handle",
"class": "Illuminate\\Foundation\\Http\\Kernel",
"type": "->"
},
{
"file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/resources/server.php",
"line": 16,
"function": "require_once"
}
]
}
The Request validator file:
public function rules(): array
{
return [
'name' => [
'required',
UniqueTranslationRule::for('ticket_statuses', 'name'),
],
'description' => 'nullable|array',
'color' => [
'string',
new Enum(Colors::class),
],
'sort_order' => 'required|integer|between:0,255',
];
}
The provided data as JSON:
{
"name": {
"bg": "asd",
"en": "asd"
},
"description": {
"bg": "asd",
"en": "asd"
},
"color": "slate",
"sort_order": 0
}
I went into the library code and used a dd()
statement on line 272 of the UniqueTranslationValidator.php
file: dd($column, $operator, $locale, $value);
The dumped data is:
"name" // vendor/codezero/laravel-unique-translation/src/UniqueTranslationValidator.php:272
"LIKE" // vendor/codezero/laravel-unique-translation/src/UniqueTranslationValidator.php:272
"bg" // vendor/codezero/laravel-unique-translation/src/UniqueTranslationValidator.php:272
array:2 [▼ // vendor/codezero/laravel-unique-translation/src/UniqueTranslationValidator.php:272
"bg" => "asd"
"en" => "asd"
]
I don't understand if I'm doing something wrong or if there is a bug in the lib.
Hello @ivanvermeyen!
First, thanks for developing this great package. I'm trying to use it on a Laravel 8 project I started recently, but I can't get it to work. I'm pretty sure I forgot something stupid, but I've been looking for a week and even showed it to a colleague, but we can't find an error. Maybe you can help me?
When I try to update a post with the same slug as another (or create once with the same slug as another), the UniqueTranslationRule rule doesn't detect it. My form request App\Http\Requests\Admin\PostRequest has this rules
function :
public function rules()
{
return [
'post.title' => 'required|string',
'post.slug' => ['required', 'max:2000', UniqueTranslationRule::for('posts', 'slug'), 'regex:/^([a-z0-9\-\/]*)$/'],
'post.category_id' => ['nullable', 'numeric', Rule::exists('categories', 'id')
->where('deleted_at', NULL)->where('type', Category::TYPE_POST)],
];
}
Things I checked :
use CodeZero\UniqueTranslation\UniqueTranslationRule;
at the beginning of the filecomposer require codezero/laravel-unique-translation
Thanks in advance ! 🙏
protected function getFilamentAttributeNameAndLocale($attribute)
{
$attribute = str_replace('data.', '', $attribute);
@list($locale, $name) = @explode('.', $attribute);
return [$name, $locale];
}
Need to switch places
@list($name, $locale) = @explode('.', $attribute);
Add please support of laravel 6.
I am using the Astrotomic/Laravel-Translatable package for the translation of my content through the models, but for me to validate with this package it is as follows
"es_name" => [
"required_without:en_name,en_slug,en_extract,en_body",
"required_with:es_slug",
Rule::unique("blog_post_translations", "name")->where(function ($query) {
return $query->where("locale", "es");
}),
],
"es_slug" => [
"required_without:en_name,en_slug,en_extract,en_body",
"required_with:es_name",
Rule::unique("blog_post_translations", "slug")->where(function ($query) {
return $query->where("locale", "es");
}),
],
I see that the use of this package makes this validation much easier
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.