Skip to content

Slug ​

The Slug field renders a text input that may generate a URL-safe slug from another field in the same generated form.

Basic Usage ​

To generate the slug from another field, pass that field's name to the from method:

Example
php
use InertiaUI\Forms\Fields\Slug;
use InertiaUI\Forms\Fields\TextInput;

TextInput::make('title');

Slug::make('slug')
    ->from('title');

The frontend component slugifies the source value in the browser as the source field changes, so no additional JavaScript dependency is required. The field adds a Laravel string rule by default.

Manual Overrides ​

By default, a manual edit locks the slug, so later changes to the source field don't overwrite the custom value:

Example
php
Slug::make('slug')
    ->from('title')
    ->lockOnManualEdit();

To make the slug always follow the source field, you may disable the lock:

php
Slug::make('slug')
    ->from('title')
    ->lockOnManualEdit(false);

Update Strategy ​

By default, the slug updates whenever the source changes, until a manual edit locks it. To preserve an existing value, use the onlyWhenEmpty method:

Example
php
Slug::make('slug')
    ->from('title')
    ->onlyWhenEmpty();

You may switch back to updating on source edits explicitly using the updateOnEdit method:

php
Slug::make('slug')
    ->from('title')
    ->updateOnEdit();

The onlyWhenEmpty(false) call is the inverse and restores updateOnEdit(true).

Separator and Case ​

The slug uses a - separator and lowercase letters by default. To change them, you may use the separator and lowercase methods:

Example
php
Slug::make('slug')
    ->from('title')
    ->separator('_')
    ->lowercase(false);

The separator may not be empty. The separator method throws an InvalidArgumentException when it receives an empty string.

Length and Placeholder ​

You may set length limits and a placeholder on the slug input:

php
Slug::make('slug')
    ->minLength(3)
    ->maxLength(80)
    ->placeholder('launch-readiness-checklist');

The minLength and maxLength methods set the native attributes and add the matching Laravel rules. Passing null clears a generated limit and its rule. Calling placeholder(null) clears the placeholder text.

The slug methods work together with the shared field APIs:

Using the Component Directly ​

You may render the Slug component with local state, or pass it serialized field props during manual form rendering. Standalone controls pass the source text using the sourceValue prop:

vue
<script setup lang="ts">
import { ref } from 'vue'
import { Slug, TextInput } from '@inertiaui/form-vue/components'

const title = ref('Launch Readiness Checklist')
const slug = ref('')

const trackBlur = () => {}
</script>

<template>
    <div class="grid gap-4">
        <TextInput v-model="title" name="title" label="Title" />

        <Slug
            v-model="slug"
            id="article-slug"
            name="slug"
            label="Slug"
            help="Used in the public URL."
            placeholder="launch-readiness-checklist"
            from="title"
            :source-value="title"
            separator="-"
            :lowercase="true"
            :lock-on-manual-edit="true"
            :update-on-edit="true"
            :min-length="3"
            :max-length="80"
            required
            data-testid="article-slug"
            @blur="trackBlur"
        />
    </div>
</template>
tsx
import { useState } from 'react'
import { Slug, TextInput } from '@inertiaui/form-react/components'

export default function SlugControls() {
    const [title, setTitle] = useState('Launch Readiness Checklist')
    const [slug, setSlug] = useState('')

    const trackBlur = () => {}

    return (
        <div className="grid gap-4">
            <TextInput
                value={title}
                onValueChange={(value) => setTitle(String(value ?? ''))}
                name="title"
                label="Title"
            />

            <Slug
                value={slug}
                onValueChange={setSlug}
                id="article-slug"
                name="slug"
                label="Slug"
                help="Used in the public URL."
                placeholder="launch-readiness-checklist"
                from="title"
                sourceValue={title}
                separator="-"
                lowercase
                lockOnManualEdit
                updateOnEdit
                minLength={3}
                maxLength={80}
                required
                data-testid="article-slug"
                onBlur={trackBlur}
            />
        </div>
    )
}

Vue uses v-model, while React uses a controlled value with onValueChange. Native attributes and listeners target the underlying <input> element. See Native Attributes And Events.