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
Tetracyclic3w ago
Can you share the code for your disabled field?
LeandroFerreira
->requiredIf('toggle',false) ?
Arshavir
Arshavir3w 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
custom page or resource?
Arshavir
Arshavir3w ago
Custom Page in addition default() not working too in Custom Page
LeandroFerreira
did you do $this->form->fill() in the mount method? are you using statePath()?
Arshavir
Arshavir3w ago
yes ofcourse
LeandroFerreira
console errors? Could you share the whole code?
Arshavir
Arshavir3w ago
no error
public function mount(): void
{
$this->form->fill($this->data);
}
public function mount(): void
{
$this->form->fill($this->data);
}
LeandroFerreira
the whole code?
toeknee
toeknee3w 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
do you mean, if you use disabled()->required(), required validation should be ignored?
toeknee
toeknee3w ago
Yep!
Tetracyclic
Tetracyclic3w 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
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
Tetracyclic3w 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
toggleButton is requred if toggle is false disabled when toggle is true ->requiredIf('toggle', false) should work
Tetracyclic
Tetracyclic3w ago
Ah, I assumed from their question that they wanted to actually disable the toggleButtons, not just make it not required.
Arshavir
Arshavir3w 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
Tetracyclic3w 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
Arshavir3w ago
Solved
$this->form->fill($this->data);
$this->form->fill($this->data);
changed to
$this->form->fill();
$this->form->fill();
as @Leandro Ferreira suggested