I built a form with the plugin Uniform and need to show/hide a part of the form with a checkbox if more infos are needed. Some fields in this part are required. I found a solution with JS to cut or paste this part onclick() on the chechbox with insertAdjacentHTML() and remove() (what is probably not very elegenat but it works). But now the required fileds in this hidden part are still checked in the controller. Is there a way to also activate/deactive this ?
If I understand correctly, you basically need to mirror the front-end logic in your controller: your validation rules array must be compiled dynamically, too.
Ex.
Say you have a checkbox toggle
and a field text
and If toggle
is checked, text
is required.
In your controller, check whether toggle
is checked and then adjust your validation rules for text
accordingly, meaning to set required or not.
// put this in your controller (untested)
$fields= []; // basic field configuration
// Extend field configuration dynamically
if (kirby()->request()->get('toggle') === 'on') {
$fields['text'] = [ 'rules' => 'required' ]
}
$form = new \Uniform\Form($fields);