Hi,
is there a way to have a unique (default field) slug in Tailor structure?
I have tried to force validation (as in docs for custom fields), but it is not working for default fields (works for existing records but not new).
Jan
Hi,
is there a way to have a unique (default field) slug in Tailor structure?
I have tried to force validation (as in docs for custom fields), but it is not working for default fields (works for existing records but not new).
Jan
If anyone fights the same problem with unique (built-in tailor) field:
Create plugin like php artisan plugin:create JanVince.TailorTools
Add an event listener to plugin’s Plugin.php
file in method boot()
:
Use your Tailor blueprint handles, I have Product\Category here.
\Tailor\Models\EntryRecord::extendInSection('Product\Category', function($model) {
$model->bindEvent('model.beforeCreate', function () use ($model) {
$sameSlugItems = EntryRecord::inSection('Product\Category')->where('slug', $model->slug)->get();
if($sameSlugItems and $sameSlugItems->count()) {
throw new \ValidationException(['slug' => 'URL is used!']);
}
});
});
I don’t like creating plugin just for a simple validation but haven’t found a better way yet.
But it works
Or you can do it with simplier solution in blueprints
fields:
slug:
type: text
validation:
- unique
This is a good workaround ^^
We need a unique rule for this that is site-aware. Something like this
unique_site:email
As a modified version of the unique
rule, internally it would look like this:
unique:users,email,NULL,id,site_root_id,<current site id>
I’ve made a note of this in our internal tracker.