HomeDocumentationTutorialsIncluding a select dropdown using an array

Including a select dropdown using an array

Overview

In this tutorial, we'll cover how to add a select dropdown field to your forms.  Additionally, we'll explain how to populate the dropdown options from an array. This method takes three parameters:

 

  • field_name: The name attribute for the dropdown.
  • field_label: The label that will be displayed alongside the dropdown.
  • $required: An optional parameter (default is 0) that specifies whether the input is required (1) or not (0).

 

 Example Code;

 

PHP
 // Adding a required select dropdown field
$objConfig->select('field_name', 'field_label', $required = 0); 

 

Populating Dropdown Options from an Array

To populate the select dropdown options from an array, you can chain the childArray method after the select method. This example shows how to add a select dropdown field with options from an array.

 

Example Code;

 

PHP
 // Adding a select dropdown with options from an array
$objConfig->select('field_name', 'field_label', $required = 0)->childArray($data);     

 

In this example:

  • $data: An associative array where keys are the option values and values are the option labels.

 

Configuration in Your Application

For the configuration to work properly, it's important to include the 'field_name'  amongst the `addFields` and `editFields`. This ensures that the fields are correctly initialized and integrated into your form. This example shows how to configure the add and edit fields.

 

PHP
// Adding your field to the add and edit forms
$objConfig->addFields(array('field_name'));
$objConfig->editFields(array('field_name'));

 

Conclusion

By following these steps, you can easily integrate select dropdown fields into your forms using $objConfig->select('field_name', 'field_label', $required = 0);. We also covered how to populate the dropdown options from an array using $objConfig->select('field_name', 'field_label', $required = 0)->childArray($data);.