In this tutorial, we'll walk through the process of adding a date field to your forms. This method allows you to easily integrate date inputs into your forms with various constraints such as required fields, date validation, default values, and year limits.
We will cover how to:
This method adds a date input field to your form, taking three parameters:
This example shows how to add a required date field to your form.
// Adding a required date field
$objConfig->date('field_name', 'field_label', $required = 0);
To ensure the date field does not accept future dates, you can chain the noFutureDate()
method after the date()
method. This example shows how to add a date field that does not accept future dates to your form.
// Adding a required date field that does not accept future dates
$objConfig->date('field_name', 'field_label', $required = 0)->noFutureDate(true);
To ensure the date field does not accept past dates, you can chain the noPastDate()
method after the date()
method. This example shows how to add a date field that does not accept past dates to your form.
// Adding a required date field that does not accept past dates
$objConfig->date('field_name', 'field_label', $required = 0)->noPastDate(true);
To set the default date as the current date, you can chain the xdefault()
method after the date()
method. This example shows how to add a date field with the current date set as the default value.
// Adding a required date field with the current date as default
$objConfig->date('field_name', 'field_label', $required = 0)->xdefault(date('Y-m-d'));
To set the minimum year for the date field, you can chain the min()
method after the date()
method. This example shows how to add a date field with a minimum year that is 14 years before the current year.
// Adding a required date field with a minimum year that is 14 years before the current year
$objConfig->date('field_name', 'field_label', $required = 0)->min(-14);
To set the maximum year for the date field, you can chain the max()
method after the date()
method. This example shows how to add a date field with a maximum year that is 5 years from the current year.
// Adding a required date field with a maximum year that is 5 years from the current year
$objConfig->date('field_name', 'field_label', $required = 0)->max(5);
To ensure the inputted date exceeds a certain age, you can use the ageMustExceed()
methods after the date()
method. This example shows how to add a date field where the inputted date must exceed 18 years ago and must be within 75 years ago from the current date.
// Adding a required date field where the inputted date must exceed 18 years ago and must be within 75 years ago from the current date
$objConfig->date('field_name', 'field_label', $required = 0)->max(-18)->min(-75)->ageMustExceed(18);
To ensure the inputted date does not exceed a certain age, you can use the ageMustNotExceed()
method after the date()
method. This example shows how to add a date field where the inputted date must not exceed 50 years ago from the current date.
// Adding a required date field where the inputted date must not exceed 18 years ago
$objConfig->date('field_name', 'field_label', $required = 0)->max(0)->min(-50)->ageMustNotExceed(50);
For the configuration to work properly, it's important to include the 'field_name'
amongst the addFields and editFields
. This ensures that the date fields are correctly initialized and integrated into your form. This example shows how to configure the add and edit fields.
// Adding your date field to the add and edit forms
$objConfig->addFields(array('field_name'));
$objConfig->editFields(array('field_name'));
This tutorial provided a comprehensive overview of integrating date fields into your forms using the configuration method $objConfig->date()
. Key features covered include:
By following these guidelines, you can effectively manage date 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.
Your download is here.