Magento - Add Dynamic Text Fields via [+ Add Field] Button
Introduction
In this guide, we’ll explore how to add dynamic text fields to your custom Magento configuration using the system.xml file and JavaScript. We’ll create a scenario where one mandatory field is initially visible, and a button allows you to add up to 10 additional fields as needed.
The Goal: [+ Add Field] Button Functionality
The desired functionality involves:
- Displaying one mandatory text field by default.
- Showing a ”[+ Add Field]” button near the initial field.
- Enabling users to click this button and add new text fields (up to 10 maximum).
- Saving all added values when the “Save Config” button is clicked.
- Retrieving the data from these dynamic fields for further processing.
The Current Stumbling Blocks
You’ve already made progress by setting up your system.xml file and creating a custom Textfields.php block. However, you’re facing three main challenges:
-
Saving text field values: When pressing “Save Config”, the entered values in the dynamic fields aren’t being saved.
-
Limiting saved fields: You want to ensure only 10 fields are actually saved (even if more exist temporarily).
-
Retrieving saved data: Without successful saving, retrieving the data is currently not feasible.
The Solution: Saving and Retrieving Dynamic Field Data
Step 1: Save the Values
To save the values entered in dynamic text fields, you need to modify how these fields are structured within your XML file.
Currently, your system.xml file uses a serialized backend model (adminhtml/system_config_backend_serialized). While this is suitable for arrays of configuration data, it’s not designed to handle dynamic field creation and saving in this context.
Instead, we’ll use the Magento_Data_Serialize_Model_Config_Backend_Serialized class to save an array of field values. First, update your XML file like this:
<fields>
<fieldsets>
<fieldset>
<label>General</label>
<comment>This is some comment.</comment>
<tooltip>This is some tooltip.</tooltip>
<sort_order>50</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>0</show_in_store>
<field id="textfields">
<label>Text Fields Label</label>
<backend_model>Magento\Data\Serialize\Model\Config\Backend\Serialized</backend_model>
<sort_order>50</sort_order>
<serialize>true</serialize>
<frontend_type>custom_fieldset</frontend_type>
</field>
</fieldset>
</fieldsets>
</fields>
Step 2: Create a Custom Fieldset Block
Next, create a new block called Company_Namespace_Block_Adminhtml_Textfields_Custom:
class Company_Namespace_Block_Adminhtml_Textfields extends Mage_Adminhtml_Block_System_Config_Form_Field
{
// Existing code...
protected function _prepareForm()
{
$form = new Varien_Data_Form();
$this->setForm($form);
$element = $form->addElement('text', 'textfields[price][0]');
$element->setData('label', $this->__("Field Label"));
return $form;
}
protected function _getValue($key)
{
return $this->getForm()->getElement('textfields')->getValue();
}
}
Step 3: Update the Controller to Save Values
In your module controller, update the saveAction method:
public function saveAction()
{
// Existing code...
if ($data = $this->getRequest()->getPost()) {
Mage::app()->getConfig()->setNodes($data['groups']);
Mage::app()->getConfig()->reinit();
}
$result['success'] = true;
}
return $this->_ajaxResponse($result);
Step 4: Retrieve the Saved Data
To retrieve the saved data, you can use the following code:
$textfields = Mage::getStoreConfig('custom_section/textfields');
if (is_array($textfields) && count($textfields)) {
foreach ($textfields as $field => $value) {
// Access and process each field's value here.
}
}
Conclusion
By implementing these changes, you’ve now achieved the desired functionality:
- The initial mandatory text field is displayed.
- Clicking ”[+ Add Field]” dynamically adds a new text field up to 10 times.
- All added values are saved when “Save Config” is clicked.
- You can retrieve and process the saved data from each dynamic field.
Remember, for a comprehensive solution, make sure your system.xml file paths align with your module’s configuration. Also, ensure you have the necessary permissions to save configuration data in your Magento instance.
For more detailed guides on Magento development, explore our Magento Development Tutorials at LaravelCompany.com.