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:

3 Likes

Or you can do it with simplier solution in blueprints

fields:
    slug:
        type: text
        validation:
            - unique
1 Like

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.

1 Like