Populating Dynamic field with Easy Registration Forms

Let’s say you have a list in your database which you want to display as a drop-down option,that’s where dynamic field population comes in handy.

Please follow the steps to create a dynamically populated field :

  1. Create a New Form
  2. Add a dropdown field without adding any options as we would be filling those options from the database.
  3. Open your theme’s functions.php.
  4. Add a filter in this file. “add_filter(‘erforms_{form_id}_{field_id}_load’,10,2);”
  5. In the above filter {form_id} and {field_id} will have to be replaced with actual Form ids and Field Ids respectively.

Steps to get form ID:

  1. Go to ‘All Forms’ menu under ERF. Here each form is presented as a card. On each card, you see a shortcode similar to No form fields.. Here 1198 is a Form ID. After placing the Form ID filter will look like:
    add_filter('erforms_1198_{field_id}_load','dynamic_options',10,2);

Steps to get dropdown field id:

  1. Go to the ‘Build’ area of the form. Click on ‘preview’ link on top.
  2. The system will open form preview in a new window.
  3. Locate your dropdown field in the preview. Now right click on the field and click on ‘inspect element’ option.
  4. Inspect element will open a tiny window and here you can locate the field ID. It would be similar to : ‘select-1545124005248’
  5. Copy the element ID and place into the filter. So it should look like:
    add_filter('erforms_1198_select-1545124005248_load','dynamic_options',10,2);

Now create your call back function (in the same file): dynamic_options. It will have two parameters.

Here is the sample:

function dynamic_options($command,$form){
$command['on'] = array('select-1545124005248'); // field ID on which this command will be executed.
$command['default_value']= 'option2'; // Default value of the dropdown. It will be selected by default. (This is optional)
$command['options']= true; // This must be true
$command['data']= array('option1'=>'Option 1','option2'=>'Option 2','option3'=>'Option 3','option4'=>'Option 4'); //Dropdown options in key-value format.
return $command;
}

Note: As of now this feature is available only for single selection dropdown. It won’t work if dropdown is allowed to select multiple values.

add_filter('erforms_1198_select-1545124005248_load','dynamic_options',10,2);
function dynamic_options($command,$form){
$command['on'] = array('select-1545124005248');
$command['default_value']= 'option2';
$command['options']= true;
$command['data']= array('option1'=>'Option 1','option2'=>'Option 2','option3'=>'Option 3','option4'=>'Option 4');
return $command;
}

You can also bind a child dropdown with the above field. Here is the filter:

add_filter('erforms_1198_select-1545124005248_change','dynamic_options_onchange',10,2);This is similar filter to the one above but for change event. Here field ID belongs to the parent dropdown.

Sample function here:

function dynamic_options_onchange($commands,$form){
$commans= erforms_default_field_command(); // Provides you template command object.
$command['on'] = array('select-1545125693043'); // Child dropdown field ID
$command['default_value']= 'option5'; // You can do your custom logic
$command['options']= true;
$command['data']= array('option5'=>'Option 5','option6'=>'Option 6','option7'=>'Option 7','option8'=>'Option 8');
array_push($commands,$command);
return $commands;
}
In the above function you can perform your own custom logic.
add_filter('erforms_1198_select-1545124005248_change','dynamic_options_onchange',10,2);
function dynamic_options_onchange($commands,$form){
$command= erforms_default_field_command();
$command['on'] = array('select-1545125693043');
$command['default_value']= 'option5'; // You can do your custom logic
$command['options']= true;
$command['data']= array('option5'=>'Option 5','option6'=>'Option 6','option7'=>'Option 7','option8'=>'Option 8');
array_push($commands,$command);
return $commands;
}

One Reply to “Populating Dynamic field with Easy Registration Forms”

Leave a Reply