conditional fields

How to conditionally hide or show form fields based on database query data.

Overview

The issue arises when needing to dynamically control field visibility depending on external data, such as from a PHP database query.
Use a PHP action with Events to conditionally disable fields from being added to the form. Alternatively, in CF v8.0.45+, use the "String not empty" trigger to fire an event that hides or shows fields based on a data parameter.

Answered
ChronoForms v8
kr kraadde 09 Apr, 2025

is it possible to hide or show form fields based on data from a php database query?

Can you give an example?

Max_admin Max_admin 09 Apr, 2025
Answer

You can hide it completely using a PHP action with Events, this will disable the fields from being added to the form page

Another way is using the "String not empty" trigger (coming in the next update):

conditional fields image 1

in this example, when there is a data parameter named "xyz" present and it's value is not empty then the trigger "hide_me" will be fired which can hide the field

Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
kr kraadde 11 Apr, 2025

max, thanks for the above answer.

I have tried several approces without success. Also downloaded version 8.0.44. Guess the string not empty is not yet implemented in that version?

You mentionned "You can hide it completely using a PHP action with Events, this will disable the fields from being added to the form page".

Have you a code line for that?

I tried following without success inside a php action before the fields text8,..9..and 10

$fieldsToHide = ['text8', 'text9', 'text10']; // Add all fields you want to hide

foreach ($fieldsToHide as $field) {
    if ($cf_id != 8) {
        unset($this->fields[$field]);
    }
}
Max_admin Max_admin 11 Apr, 2025

Yes, the "String not empty" is available only in v8.0.45

The PHP code can be like this:

if(!empty($this->data("xyz"))){
return "show";
}

then you need an event named "show" in your PHP behaviors > Events, and drag your fields inside that event box

in this case, the "show" event will only run when the "xyz" parameter is not empty

Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
Post a Reply