Form Helpers
Trongate's form helpers let you easily build HTML forms without the tedium. Call a function. Get perfect HTML.
Zero config. Just functions:
- Generates a generic button that requires JavaScript to perform actions.
- Outputs a checkbox input for binary choices.
- Closes the form and automatically injects a CSRF token.
- Generates a date input with browser-native date picker.
- Generates a local datetime input with browser-native controls.
- Generates a select menu allowing single or multiple selections.
- Creates an email input with built-in browser validation.
- Generates a file picker input for uploading files.
- Adds hidden fields for data that should not be visible to users.
- Generates a standard text input field.
- Generates an HTML label element for form fields.
- Generates a month input with browser-native month picker.
- Outputs a numeric input with browser-level validation and controls.
-
Outputs the opening
<form>tag and initializes a new form. - Opens a form configured for file uploads using multipart encoding.
- Produces a password field that masks user input.
- Creates a radio button for mutually exclusive options.
- Generates a search input with optional clear button.
- Creates a URL input with built-in browser validation.
- Creates a submit button for sending form data to the server.
- Creates a multi-line text field for longer user input.
- Generates a time input with browser-native time picker.
- Generates a week input with browser-native week picker.
- Retrieves submitted POST, JSON, or multipart form data.
- Displays validation errors, supporting inline, grouped, or JSON output.
Building a Form
In a view file:
<?php
echo form_open('submit');
echo form_input('username');
echo form_password('password');
echo form_checkbox('remember', '1', true);
echo 'Remember me';
echo form_submit('submit', 'Login');
echo form_close();
?>Output:
<form action="submit" method="post">
<input type="text" name="username">
<input type="password" name="password">
<input type="checkbox" name="remember" value="1" checked> Remember me
<input type="submit" name="submit" value="Login">
<input type="hidden" name="csrf_token" value="abc123">
</form>The function automatically handles the insertion of a hidden CSRF token field, so developers do not need to manually include it in their forms.
File Uploads
For forms that accept files, just use form_open_upload() instead of form_open(). It adds the required enctype="multipart/form-data" automatically. No extra arguments needed.
In a view file:
<?php
echo form_open_upload('products/upload_picture');
echo form_label('Product Image');
echo form_file_select('picture');
echo form_submit('upload', 'Upload Image');
echo form_close();
?>Output:
<form action="products/upload_picture" method="post" enctype="multipart/form-data">
<label>Product Image</label>
<input type="file" name="picture">
<input type="submit" name="upload" value="Upload Image">
<input type="hidden" name="csrf_token" value="abc123">
</form>Adding Attributes
All form helpers accept an optional third parameter: an associative array of HTML attributes.
In a view file:
<?php
$email_attr = [
'placeholder' => 'Enter email address here...',
'required' => true
];
echo form_email('email', '', $email_attr);
?>Output:
<input type="email" name="email" placeholder="Enter email address here..." required>Form handling (in any web development framework!) has lots of moving parts.
Check out the Form Handling chapter for a complete walkthrough of how to perform form handling with Trongate.
Looking for a super fancy way to do form handling but without page refreshes?
Trongate MX has you covered!
Check out the Trongate MX documentation for more information.
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.