Trongate PHP Framework Docs
Introduction
Basic Concepts
Understanding Routing
Intercepting Requests
Module Fundamentals
Database Operations
Templates
Helpers
Form Handling
Form Validation
Working With Files
Image Manipulation
Working With Dates & Times
Language Control
Authorization & Authentication
Tips And Best Practices

Date Input Fields

The date input field is the most commonly used date/time input type. It provides users with a calendar picker and always submits data in the standard YYYY-MM-DD format.

Basic Usage

Use to create a date input field:

Demonstration

Below is the native HTML5 date input rendered by form_date():

Function Signature

Parameters

Parameter Type Description Default
$name string The name attribute for the input (required) N/A
$value string|null The value in YYYY-MM-DD format null
$attributes array Additional HTML attributes []

With a Default Value

With Attributes

Output:

Boolean Attributes: Pass true for boolean attributes like required, readonly, or disabled. This generates clean HTML5 syntax: <input required>

Downloadable Demo: Want to see the function in action? We've got you covered! Check out the complete Friends module example on GitHub:

https://github.com/trongate/Trongate-v2-Friends-Module

This GitHub repository demonstrates a full-featured CRUD (Create, Read, Update, Delete) application for tracking friends' birthdays. Check it out and learn by exploring a full working example.

Common Attributes

Attribute Purpose Example
min Earliest selectable date (YYYY-MM-DD) ['min' => '2025-01-01']
max Latest selectable date (YYYY-MM-DD) ['max' => '2025-12-31']
required Field must have a value ['required' => true]
readonly Prevent user editing ['readonly' => true]
disabled Disable the input ['disabled' => true]

Setting Today as Default

How to Repopulate Forms After Validation Errors

When a form fails validation, you need to redisplay it with the user's entered value. In Trongate, this follows the MVC pattern:

Controller Example

View Example

The controller fetches the posted data using , then passes it to the view. The view receives these values as variables.

Form Repopulation Pattern: Always use post('field_name', true) in the controller to fetch submitted values. The true parameter trims whitespace, ensuring clean data.

Real-World Example: Task Due Date

Controller:

View:

Setting Min and Max Dates Dynamically

Often you'll want to restrict the date range based on business logic:

Working with the Create/Update Pattern

Here's the standard pattern for date fields that work for both creating and updating records:

Type-Casting Segments: Always use segment(3, 'int') when expecting numeric IDs. This prevents type-related bugs and improves code clarity.

Database Storage

Date inputs submit in YYYY-MM-DD format, which maps directly to the MySQL DATE type:

Table Schema

Inserting Data

No Conversion Needed: The YYYY-MM-DD format from HTML5 date inputs matches MySQL's DATE type exactly. You can insert the value directly without any conversion.

Formatting Dates for Display

When displaying dates to users, you'll often want to format them in a more readable way:

Validation

Use the valid_date validation rule to ensure the submitted value is in the correct format:

This validates that the value:

  • Matches the YYYY-MM-DD format
  • Represents a valid calendar date
  • Properly handles leap years

Client vs Server Validation: While browsers validate date formats client-side, always use server-side validation with valid_date. Client validation can be bypassed and doesn't protect your database.

Setting a Date Range Example

Here's a complete example of a form that requires a start date and end date, where the end date must be after the start date: