How to get the right statePath?

I have a form with a statePath:
Section::make('My Section')
->schema([
TextInput::make('field1'),
TextInput::make('field2'),
])
->statePath('group')
Section::make('My Section')
->schema([
TextInput::make('field1'),
TextInput::make('field2'),
])
->statePath('group')
On output, the $data is shaped like this:
[
'group' => [
'field1' => 'value',
'field2' => 'value',
]
]
[
'group' => [
'field1' => 'value',
'field2' => 'value',
]
]
What I want is to require field2 when field1 is > 0 I tried that (based on other answers in this discord) but I think I'm not giving the right state path:
TextInput::make('field2')
->requiredWith(function (Get $get) {
return $get('field1') > 0 ? ['group.field1'] : [];
})
TextInput::make('field2')
->requiredWith(function (Get $get) {
return $get('field1') > 0 ? ['group.field1'] : [];
})
14 Replies
awcodes
awcodes4w ago
$get(‘group.field1’) Or $get(‘./field1’) It follows directory path syntax.
charlie
charlieOP4w ago
no no no, the $get is working fine with $get('field1'). That's the return value that is wrong: Let me choose a more clear example:
Section::make('Expenses')
->schema([
TextInput::make('amount'),
TextInput::make('details')
->requiredWith('amount'), // <-- ~~this doesnt work~~ EDIT: IT WORKS
])
->statePath('expenses')
Section::make('Expenses')
->schema([
TextInput::make('amount'),
TextInput::make('details')
->requiredWith('amount'), // <-- ~~this doesnt work~~ EDIT: IT WORKS
])
->statePath('expenses')
toeknee
toeknee4w ago
try expenses.amount. We array map the statePath so it is possible it's getting the current path which would be expenses.amount. I haven't tested it though.
charlie
charlieOP4w ago
whoops, I just tested my exemple and it actually works...
toeknee
toeknee4w ago
haha funny, we do use state paths but it should get the current path lol.
charlie
charlieOP4w ago
My real form is pretty huge, so I wanted to give a simple exemple. Give me one minute Ok, that's because of the FileUpload:
Section::make('Expenses')
->schema([
TextInput::make('amount'),
FileUpload::make('invoice')
->requiredWith('amount'), // This does NOT work
])
->statePath('expenses'),
Section::make('Expenses')
->schema([
TextInput::make('amount'),
FileUpload::make('invoice')
->requiredWith('amount'), // This does NOT work
])
->statePath('expenses'),
toeknee
toeknee4w ago
Ahh ok, so it's actually FileUpload that's not working with requiredWith
charlie
charlieOP4w ago
yes, sorry
toeknee
toeknee4w ago
Can you submit a bug report in that scenario plesae with a repoduction repo?
charlie
charlieOP4w ago
But it's not because of statePath
toeknee
toeknee4w ago
Correct, it's the field not working with it. Likely because file uploads are handled differently
charlie
charlieOP4w ago
There's already an existing bug here: https://github.com/filamentphp/filament/issues/15954
GitHub
requiredIf not working with FileUpload component · Issue #15954 ·...
Package filament/filament Package Version v3.3.3 Laravel Version v12.1.1 Livewire Version v3.6.1 PHP Version PHP 8.4.5 Problem description In a form with a select with two options and two dependent...
charlie
charlieOP4w ago
That's with requiredIf, not requiredWith, but I think these issues are linked Also a discord thread in this channel: https://discord.com/channels/883083792112300104/1141465962483286057
toeknee
toeknee4w ago
Yep please comment on it too

Did you find this page helpful?