HomeDocumentationTutorialsIncluding a checkbox using an array

Including a checkbox using an array

Overview

In this tutorial, we'll cover how to add a checkbox to your forms. Additionally, we will demonstrate how to configure the checkbox to read its option data from an array using the childArray method. This method adds a checkbox input field to your form, taking three parameters:

 

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

 

To configure the checkbox to read its option data from an array, you can chain the childArray method after the checkbox method. This example shows how to add a checkbox that retrieves its options from an array.

 

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

 

In this example:

 

  • $data: An associative array where the keys represent the option values and the values represent the display names of the options.

 

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 checkbox  fields are correctly initialized and integrated into your form. This example shows how to configure the add and edit fields.

 

PHP
// Adding your checkbox 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 checkbox fields into your forms using $objConfig->checkbox('field_name', 'field_label', $required = 0);. We also covered how to configure the checkbox to read its option data from an array using $objConfig->checkbox('field_name', 'field_label', $required = 0)->childArray($data);.