How to conditionally set a field as non-required based on the value of another field?

I have a checkbox ('location_type'), and I want to remove the 'required' constraint from a subsequent field ('name') if the checkbox is checked, utilizing the reactive() method. The relevant code snippet is as follows: Checkbox::make('location_type')->inline()->reactive(), TextInput::make('name') ->required() ->maxLength(191) ->columnSpan(1),
3 Replies
ModestasV
ModestasV13mo ago
->required(fn ($get) => $get('location_type') !== true) Or something similar might do the trick for you
Ali Abbas
Ali AbbasOP13mo ago
@ModestasV thank you this works 😊
awcodes
awcodes13mo ago
Have a look at the other validation methods in the docs. There’s even a ->requiredIf()
Want results from more Discord servers?
Add your server