Tailor unique slug

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

@daft Hi Sam, have you any tips, ho to make slug unique with Tailor?

If anyone fights the same problem with unique (built-in tailor) field:

  1. Create plugin like php artisan plugin:create JanVince.TailorTools

  2. 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 :slight_smile:

2 Likes

Or you can do it with simplier solution in blueprints

fields:
    slug:
        type: text
        validation:
            - unique