Trongate PHP Framework Docs
Introduction
Quick Start
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
Security
Tips And Best Practices

Checkboxes and Radio Buttons

Checkboxes and radio buttons use a similar helper pattern as other form elements, with special handling for checked states.

Always do this:

  1. In your view: Use value="1" for checkboxes
  2. In your controller: Convert POST data to boolean for views, integer for database
  3. In your database: Store as 0 or 1

Checkboxes

Use :

Parameters

  • $name (string, required) - The name attribute for the checkbox
  • $value (string|bool|int, optional) - The value attribute. Defaults to '1'
  • $checked (mixed, optional) - Whether the checkbox should be checked. Defaults to false
  • $attributes (array, optional) - Additional HTML attributes. Defaults to []

Recommended Pattern (Best Practice)

For accessibility and improved usability, always wrap the checkbox input directly within the HTML <label> element. This allows users to click the text to toggle the checkbox, increasing the target area.

Note on Validity: Placing the input inside the label (implicit association) is perfectly valid HTML5 and the industry-standard best practice for these elements.

The Checkbox Reality

Unchecked checkboxes don't submit any data. This is HTML behavior, not Trongate.

When a form submits:

  • Checked checkbox → POST contains subscribe='1'
  • Unchecked checkbox → POST contains no subscribe field

Here's something crucial: unchecked checkboxes don't submit anything.

If a checkbox is checked, the form submits its value. If unchecked, the field doesn't appear in the POST data at all.

This is why you often see this pattern:

The Complete Pattern

Step 1: View File (Use Wrapping Pattern)

Important: The third parameter ($newsletter_checked) must be true or false.

Step 2: Controller (Two different conversions)

Your controller needs to handle data differently depending on whether it's going to the view or to the database.

Step 3: Database Schema

Two Different Conversions

When working with checkboxes the type of conversion that happens differs, depending on whether the goal is to render a view file or enter a checkbox value into a database.

Destination Needs Conversion Result
View (form_checkbox) Boolean (bool) post('field', true) true or false
Database Integer 0/1 (int) (bool) post('field', true) 1 or 0

Common Pitfalls

Never use string values for checkboxes.

Do this: Always use 1 as the value

Radio Buttons

Radio buttons are simpler - they always submit a value. You should also wrap the radio input within the <label> element for accessibility.

Parameters

  • $name (string, required) - The name attribute for the radio button
  • $value (string|bool|int, optional) - The value attribute. Defaults to ''
  • $checked (mixed, optional) - Whether the radio button should be checked. Defaults to false
  • $attributes (array, optional) - Additional HTML attributes. Defaults to []

Radio Group Example (Recommended Pattern)

Multiple Checkboxes

For multiple selections, store as JSON array:

Quick Reference

Element View Code To View (boolean) To Database
Checkbox <label>...form_checkbox('field', 1, $checked)...</label> (bool) post('field', true) (int) (bool) post('field', true)
Radio <label>...form_radio('field', 'value', $checked)...</label> post('field', true) post('field', true)
Text Input form_input('field', $value) post('field', true) post('field', true)

Remember: Checkboxes are the only form element that needs different handling for views vs. databases. Everything else uses the same value for both.

We're continually improving the Trongate documentation. If anything is incorrect, unclear, incomplete, or could be better, we'd genuinely appreciate your input.

Share your thoughts in the Documentation Feedback.

Leave Feedback About This Page