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