HomeDocumentationTutorialsAdding a Datetime Field to Forms

Adding a Datetime Field to Forms

Overview

In this tutorial, we'll walk through the process of adding a datetime field to your forms. This method allows you to easily integrate datetime inputs into your forms. We will cover how to:

 

  • Prevent the date field from exceeding or not exceeding certain dates.
  • Set the default datetime as the current datetime.
  • Set constraints on the minimum and maximum allowable years for the date field.

 

This method adds a datetime input field to your form, taking three parameters:

 

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

 

This example shows how to add a datetime field to your form.

 

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

 

Adding a Datetime Field with Validation Against Another Field  

To ensure the inputted datetime exceeds the datetime inputted in another field on the same form, you can chain the exceedField() method after the datetime() method. This example shows how to add such validation.

 

PHP
 // Adding a datetime field that must exceed the 'other_datetime_field' field
$objConfig->datetime('field_name', 'field_label', $required = 0)->exceedField('other_datetime_field');

 

To ensure the inputted datetime does not exceed a certain datetime inputted in another field on the same form, you can use the notExceedField() method. This example shows how to add a datetime field with such validations.

 

PHP
// Adding a datetime field that must not exceed the 'other_datetime_field' field
$objConfig->datetime('field_name', 'field_label', $required = 0)->notExceedField('other_datetime_field');

 

Adding a Date Field with the Current Date as Default

To set the default datetime as the current date, you can chain the xdefault() method after the datetime() method. This example shows how to add a date field with the current datetime set as the default value.

 

PHP
// Adding a required datetime field with the current date as default
$objConfig->datetime('field_name', 'field_label', $required = 0)->xdefault(date('Y-m-d H:i'));

 

Adding a Datetime Field with a Minimum Year

To set the minimum year for the date field, you can chain the min() method after the datetime() method. This example shows how to add a date field with a minimum year that is 14 years before the current year.

 

PHP
// Adding a required datetime field with a minimum year that is 14 years before the current year
$objConfig->datetime('field_name', 'field_label', $required = 0)->min(14);

 

Adding a Date Field with a Maximum Year

To set the maximum year for the date field, you can chain the max() method after the datetime() method. This example shows how to add a datetime field with a maximum year that is 5 years from the current year.

 

PHP
// Adding a required datetime field with a maximum year that is 5 years from the current year
$objConfig->datetime('field_name', 'field_label', $required = 0)->max(5);

 

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

 

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

 

Conclusion

This tutorial provided a comprehensive overview of integrating datetime fields into your forms using the configuration method $objConfig->datetime(). Key features covered include:

 

  • Setting the default datetime dynamically.
  • Validating if the input date exceeds or does not exceed a certain datetime on the same form.

 

By following these guidelines, you can effectively manage datetime inputs in your forms and ensure data integrity in your applications. If you have specific requirements or need further customization, feel free to adapt these methods to suit your component's needs.