Get Relation Fieldset State / Set Outside State in Relationship Fieldset

Form resources that have fieldset relationship, how to get state that fieldset on outside? just for example :
[
Fieldset::make('User')
->relationship('users')
->schema([
TextInput::make('name')
->required(),
]),
TextInput::make('slug')
->dehydrateStateUsing(
fn(string $state, callable $get) => Str::slug($get('user.name')) // <-- when get this, the value is null
)
]
[
Fieldset::make('User')
->relationship('users')
->schema([
TextInput::make('name')
->required(),
]),
TextInput::make('slug')
->dehydrateStateUsing(
fn(string $state, callable $get) => Str::slug($get('user.name')) // <-- when get this, the value is null
)
]
or set outside state in relationship fieldset
[
Fieldset::make('User')
->relationship('users')
->schema([
TextInput::make('name')
->required()
->reactive()
->live()
->afterStateUpdated( callable $set) => $set('slug', Str::slug($state))),
// nothing happens
]),
TextInput::make('slug')
->prefix(config('app.url') . "/profile/")
]
[
Fieldset::make('User')
->relationship('users')
->schema([
TextInput::make('name')
->required()
->reactive()
->live()
->afterStateUpdated( callable $set) => $set('slug', Str::slug($state))),
// nothing happens
]),
TextInput::make('slug')
->prefix(config('app.url') . "/profile/")
]
Solution:
you should use
$set('../slug', Str::slug($state))
$set('../slug', Str::slug($state))
...
Jump to solution
18 Replies
Tally
Tally6mo ago
can you explain what you are trying to do with the FieldSet and the relationship? Are you trying to create a Select with the users?
nyannss
nyannssOP6mo ago
Nah. I have one to one relationship, I want edit in one form. Lets say table users have name column and the nurses table have slug column. Like in example (https://filamentphp.com/docs/3.x/forms/advanced#generating-a-slug-from-a-title), when edit name, will automatically update slug value.
Tally
Tally6mo ago
check out the example again... you are using callable which is an old syntax for Filament v2
nyannss
nyannssOP6mo ago
Isn't "callable" just for ide helper?
Tally
Tally6mo ago
nope
Tally
Tally6mo ago
from example
use Filament\Forms\Components\TextInput;
use Filament\Forms\Set;
use Illuminate\Support\Str;

TextInput::make('title')
->live()
->afterStateUpdated(fn (Set $set, ?string $state) => $set('slug', Str::slug($state)))

TextInput::make('slug')
use Filament\Forms\Components\TextInput;
use Filament\Forms\Set;
use Illuminate\Support\Str;

TextInput::make('title')
->live()
->afterStateUpdated(fn (Set $set, ?string $state) => $set('slug', Str::slug($state)))

TextInput::make('slug')
nyannss
nyannssOP6mo ago
Tried, nothing happens.
Tally
Tally6mo ago
and if you use the example provided in your code... comment the rest
nyannss
nyannssOP6mo ago
Fieldset::make('User Data')
->relationship('user')
->schema([
TextInput::make('name')
->reactive()
->live()
->required()
->afterStateUpdated(fn(?string $state, Set $set) => $set('slug', Str::slug($state))),
TextInput::make('email')
->email(),
TextInput::make('password')
->required(fn(string $operation) => $operation == "create")
->password()
->dehydrateStateUsing(fn(string $state): string => Hash::make($state))
->dehydrated(fn(?string $state): bool => filled($state))
->confirmed(),
TextInput::make('password_confirmation')
->required(fn(string $operation) => $operation == "create")
->password(),
FileUpload::make('profile_image_url')
->label("Foto Profil")
->directory(fn(callable $get) => '/profile-image/' . Str::slug($get('name')))
->getUploadedFileNameForStorageUsing(
fn(TemporaryUploadedFile $file): string => (string)str(time() . Str::random(14) . "." . $file->getClientOriginalExtension())
->prepend('image-'),
)
->image()
->storeFileNamesIn('/assets/images/services')
->imageCropAspectRatio("1:1")
->imageEditor()
->maxSize(1000)
->columnSpan('full')
]),
Fieldset::make('User Data')
->relationship('user')
->schema([
TextInput::make('name')
->reactive()
->live()
->required()
->afterStateUpdated(fn(?string $state, Set $set) => $set('slug', Str::slug($state))),
TextInput::make('email')
->email(),
TextInput::make('password')
->required(fn(string $operation) => $operation == "create")
->password()
->dehydrateStateUsing(fn(string $state): string => Hash::make($state))
->dehydrated(fn(?string $state): bool => filled($state))
->confirmed(),
TextInput::make('password_confirmation')
->required(fn(string $operation) => $operation == "create")
->password(),
FileUpload::make('profile_image_url')
->label("Foto Profil")
->directory(fn(callable $get) => '/profile-image/' . Str::slug($get('name')))
->getUploadedFileNameForStorageUsing(
fn(TemporaryUploadedFile $file): string => (string)str(time() . Str::random(14) . "." . $file->getClientOriginalExtension())
->prepend('image-'),
)
->image()
->storeFileNamesIn('/assets/images/services')
->imageCropAspectRatio("1:1")
->imageEditor()
->maxSize(1000)
->columnSpan('full')
]),
Fieldset::make('Konten Halaman')->schema([
TextInput::make('title')
->required(),

TextInput::make('meta_desc')
->required(),

MarkdownEditor::make('content')
->required()
->columnSpan('full'),

TextInput::make('desc_nurse')
->label("Deskripsi Singkat")
->required()
->columnSpan('full'),

TextInput::make('slug')
->prefix(config('app.url') . "/profile/")
->unique(Nurse::class, 'slug', fn($record) => $record)
->columnSpan('full'),
])
Fieldset::make('Konten Halaman')->schema([
TextInput::make('title')
->required(),

TextInput::make('meta_desc')
->required(),

MarkdownEditor::make('content')
->required()
->columnSpan('full'),

TextInput::make('desc_nurse')
->label("Deskripsi Singkat")
->required()
->columnSpan('full'),

TextInput::make('slug')
->prefix(config('app.url') . "/profile/")
->unique(Nurse::class, 'slug', fn($record) => $record)
->columnSpan('full'),
])
krekas
krekas6mo ago
live() and reactive() are the same. use one which should be live()
nyannss
nyannssOP6mo ago
Forgot to delete, thanks for reminding.
Solution
Tally
Tally6mo ago
you should use
$set('../slug', Str::slug($state))
$set('../slug', Str::slug($state))
Tally
Tally6mo ago
this is because of the relationship user you have to go one level up to target the slug input
nyannss
nyannssOP6mo ago
Thats what i think 👌
Tally
Tally6mo ago
It will work for sure 😉
nyannss
nyannssOP6mo ago
Yes, it is. Thanks for your help! Turns out it's like a folder path, i just realised that too.
Want results from more Discord servers?
Add your server