If field is disabled, do not require

Is it true, if field is disabled it would be not required (pass validation)? I have toggle, when toggle is on, field becomes disabled, but still required validation don't let me to pass the field.
21 Replies
Tetracyclic
Tetracyclic6mo ago
Can you share the code for your disabled field?
LeandroFerreira
LeandroFerreira6mo ago
->requiredIf('toggle',false) ?
Arshavir
ArshavirOP6mo ago
Toggle::make('toggle')
->dehydrated(false)
->live(),
Toggle::make('toggle')
->dehydrated(false)
->live(),
ToggleButtons::make('toggleButton')
->grouped()
->requiredIf('toggle', false)
->options([
'1' => '1',
'2' => '2',
'3' => '3',
'4' => '4',
'5' => '5',
]);
ToggleButtons::make('toggleButton')
->grouped()
->requiredIf('toggle', false)
->options([
'1' => '1',
'2' => '2',
'3' => '3',
'4' => '4',
'5' => '5',
]);
But not working
LeandroFerreira
LeandroFerreira6mo ago
custom page or resource?
Arshavir
ArshavirOP6mo ago
Custom Page in addition default() not working too in Custom Page
LeandroFerreira
LeandroFerreira6mo ago
did you do $this->form->fill() in the mount method? are you using statePath()?
Arshavir
ArshavirOP6mo ago
yes ofcourse
LeandroFerreira
LeandroFerreira6mo ago
console errors? Could you share the whole code?
Arshavir
ArshavirOP6mo ago
no error
public function mount(): void
{
$this->form->fill($this->data);
}
public function mount(): void
{
$this->form->fill($this->data);
}
LeandroFerreira
LeandroFerreira6mo ago
the whole code?
toeknee
toeknee6mo ago
I have noticed this, @Leandro Ferreira it's usually if you edit a form and have a field required added later and it is empty. It can't continue as it's required even though it's disabled so can't be filled.
LeandroFerreira
LeandroFerreira6mo ago
do you mean, if you use disabled()->required(), required validation should be ignored?
toeknee
toeknee6mo ago
Yep!
Tetracyclic
Tetracyclic6mo ago
It doesn't look like you're actually disabling this field. I don't see in that code how you're disabling the field when the toggle is changed?
LeandroFerreira
LeandroFerreira6mo ago
public function disabled(bool | Closure $condition = true): static
{
$this->isDisabled = $condition;
$this->dehydrated(fn (Component $component): bool => ! $component->evaluate($condition));

return $this;
}
public function disabled(bool | Closure $condition = true): static
{
$this->isDisabled = $condition;
$this->dehydrated(fn (Component $component): bool => ! $component->evaluate($condition));

return $this;
}
Tetracyclic
Tetracyclic6mo ago
Toggle::make('toggle')
->dehydrated(false)
->live(),

ToggleButtons::make('toggleButton')
->disabled(fn (Get $get): bool => ! $get('toggle'))
->grouped()
->options([
'1' => '1',
'2' => '2',
'3' => '3',
'4' => '4',
'5' => '5',
]);
Toggle::make('toggle')
->dehydrated(false)
->live(),

ToggleButtons::make('toggleButton')
->disabled(fn (Get $get): bool => ! $get('toggle'))
->grouped()
->options([
'1' => '1',
'2' => '2',
'3' => '3',
'4' => '4',
'5' => '5',
]);
Does that not work? If the toggle is false, toggleButton should be disabled and its state shouldn't appear in the data array when the form is submitted
LeandroFerreira
LeandroFerreira6mo ago
toggleButton is requred if toggle is false disabled when toggle is true ->requiredIf('toggle', false) should work
Tetracyclic
Tetracyclic6mo ago
Ah, I assumed from their question that they wanted to actually disable the toggleButtons, not just make it not required.
Arshavir
ArshavirOP6mo ago
@Ross Bearman disabled working Also I noticed
ToggleButtons::make('toggleButton')
->disabled(fn (Get $get): bool => ! $get('toggle'))
->default('1')
->grouped()
->options([
'1' => '1',
'2' => '2',
'3' => '3',
'4' => '4',
'5' => '5',
]);
ToggleButtons::make('toggleButton')
->disabled(fn (Get $get): bool => ! $get('toggle'))
->default('1')
->grouped()
->options([
'1' => '1',
'2' => '2',
'3' => '3',
'4' => '4',
'5' => '5',
]);
default() not working too Scenario is if I toggling to On, than I want to disable ToggleButtons, Set value 0 (0 option will add later, this is second challenge, need to hide 0 option for user ) for example, and Validation not required
Tetracyclic
Tetracyclic6mo ago
You should be able to use afterStateUpdated() on the Ttoggle to update the selected item on the ToggleButton. However if you're using disabled(), you'll also need to set ->dehydrated(true) on the ToggleButtons and use requiredIf('toggle', false) , otherwise the 0 state won't be returned. Another option could be to use a Hidden field to actually return the value and update its value to either 0 or the number selected in the ToggleButton whenever the state of either is updated. That way you won't need to deal with hiding the 0 option.
Arshavir
ArshavirOP6mo ago
Solved
$this->form->fill($this->data);
$this->form->fill($this->data);
changed to
$this->form->fill();
$this->form->fill();
as @Leandro Ferreira suggested
Want results from more Discord servers?
Add your server