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:
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:
Slug::make('slug')
->from('title')
->lockOnManualEdit();To make the slug always follow the source field, you may disable the lock:
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:
Slug::make('slug')
->from('title')
->onlyWhenEmpty();You may switch back to updating on source edits explicitly using the updateOnEdit method:
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:
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:
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:
- Form Class covers labels, help text, and placeholders.
- Model Binding covers the
defaultmethod. - Validation covers rules, required fields, and precognition.
- Styling covers classes, presentation helpers, and the invalid state.
- Conditional Visibility
- Authorization
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:
<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>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.