Adding a Calendar Date Picker to Your Custom Magento Admin Form
Creating custom modules in Magento is a powerful way to extend the functionality of your e-commerce platform. Once you’ve created a module using tools like ModuleCreator, the next step often involves adding complex form elements like calendar date pickers.
In this guide, I’ll walk you through the process of integrating a calendar date picker into your custom admin form. We’ll use jQuery UI for the datepicker functionality and ensure that selected dates are properly saved to your database upon submission.
Step 1: Include Required JavaScript Libraries
First, include the required libraries in your module’s view/adminhtml/layout directory:
- In your layout XML file (e.g.,
your_module_form.xml), add the following inside<reference name="head">:
<script src="//code.jquery.com/ui/1.12.0/jquery-ui.js"></script>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.0/themes/smoothness/jquery-ui.css">
Step 2: Add the Date Picker to Your Form
Next, modify your form’s phtml file (e.g., Form.phtml) to incorporate the date picker fields:
<div class="field">
<label for="from_date"><?= $block->escapeHtml(__('From Date')) ?></label>
<input type="text" name="your_module[from_date]" id="from_date"
value="<?= $block->escapeHtml($model->getData('from_date')) ?>"
style="width:100%;">
</div>
<div class="field">
<label for="to_date"><?= $block->escapeHtml(__('To Date')) ?></label>
<input type="text" name="your_module[to_date]" id="to_date"
value="<?= $block->escapeHtml($model->getData('to_date')) ?>"
style="width:100%;">
</div>
Step 3: Initialize the Date Picker in JavaScript
Create a new file requirejs-config.js inside your module’s view/adminhtml/requirejs-config.js directory:
var config = {
map: {
'*': {
your_module_form_js: 'your_module/js/form'
}
},
shim: {
your_module_form_js: {
deps: ['jquery', 'jquery/ui']
}
}
};
Then, create the JavaScript file form.js inside view/adminhtml/js/:
require([
'jquery',
'jquery/ui'
], function($) {
$(function() {
$("#from_date").datepicker({
dateFormat: "yy-mm-dd"
});
$("#to_date").datepicker({
dateFormat: "yy-mm-dd"
});
});
});
Step 4: Save the Selected Dates to Database
In your module’s Controller/Adminhtml/Index/Save.php file, ensure you’re saving the selected dates correctly:
public function execute()
{
$post = $this->getRequest()->getPostValue();
$model = $this->_initModel();
if ($post) {
$model->setData('from_date', $post['your_module']['from_date']);
$model->setData('to_date', $post['your_module']['to_date']);
try {
// Attempt to save the data
$model->save();
$this->messageManager->addSuccess(__('The form has been saved.'));
} catch (Exception $e) {
$this->messageManager->addError($e->getMessage());
}
}
return $this->_redirect('your_module/index/edit', ['id' => $model->getId()]);
}
Conclusion
Adding a calendar date picker to your custom Magento admin form involves several steps, including including required libraries, modifying the form’s phtml file, initializing the datepicker in JavaScript, and properly saving selected dates to the database upon submission.
By following this guide, you should now be able to create dynamic forms with calendar date pickers that function seamlessly within your custom Magento module. Remember to test your implementation thoroughly to ensure compatibility across different browsers and devices.
For more advanced form handling techniques in Magento, consider exploring our comprehensive guides on form validation and custom form fields.