Preset Input Field with combination from id and title-slug

Hi,

I like to use the preset attribute in the fields.yaml

            preset:
                field: name
                type: slug
                prefixInput: id

I would like to preset the slug field with a slug which is a combination from ID and title.
So for id 1 and title “This is my title”, I would like to preset “1-this-is-my-title”

I know I can achieve such a solution onSave with solutions like this here:
https://octobercms.com/forum/post/generate-slug-from-two-form-fields

But the solution from the thread generate the slug on save. I would like it to be automatically generated as soon as someone sets the title.

Is there a way to do this properly?

I know prefixInput dont work in the upper example since it should be a CSS Selector

But also with this here: prefixInput: '#Form-field-Model-fieldname' I couldnt get it to work (the id of the input which is generated by october).

Thanks for the help ^^

I’d suggest using the Sluggable Trait in the model and set the slugs property like this:

protected $slugs = ['slug' => ['id','title']];
1 Like

Thanks for the idea.

The problem with sluggable is that the value is vsible on save but not before.

I hoped for a solution which works with preset.

Hi @LordRazen

This approach has a slight problem; the preset JS library is often used when creating a record, so the ID will be unavailable.

Secondly, if we include composite field support, there would need to be an ID field which might look unsightly.

A good pattern is to let the slug be the same and include the ID in the URL, like this

/:slug/:id

Lookup the record using the ID and the slug is just a facade for SEO purposes. You can also redirect any slug and this is even better for SEO, so when the slug changes links are never broken.

1 Like

im not sure, if you are aware of security problem of exposing numeric ID’s … but, it is not an good approach, u know.

For content it is fine. Exposing the primary key of the user is bad, indeed.

each time i got this discussion, every time i got an flashing light in my mind, because i work for high secure company and this is bit or less, always a exposing any type of incrementing value that can bring potential risk… of course, if ID is uuid, then this is out of topic, but, regarding example, simple integer value is always something, what is flagged as potential risk.