rougin / fortem
Simple form templates in PHP.
Installs: 698
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/rougin/fortem
Requires
- php: >=5.3.0
- staticka/staticka: ~0.4
Requires (Dev)
This package is auto-updated.
Last update: 2025-10-30 16:00:16 UTC
README
A collection of form template helpers for PHP.
Installation
Install the package using Composer:
$ composer require rougin/fortem
Basic usage
The FormHelper class is used for generating form-related HTML elements. It provides a fluent interface for creating labels, inputs, buttons, select dropdowns, and error messages:
use Rougin\Fortem\Helpers\FormHelper; $form = new FormHelper; // ...
Labels
To create a <label> element, the label method is used:
<?php // ... echo $form->label('Name');
<label>Name</label>
The class attribute can be specified in its second argument:
<?php // ... echo $form->label('Name', 'form-label');
<label class="form-label">Name</label>
A label can also be marked as required, which adds a red asterisk:
<?php // ... echo $form->label('Name')->asRequired();
<label>Name <span class="text-danger">*</span></label>
Inputs
To create an <input> element, the input method is used. By default, it creates a text input:
<?php // ... echo $form->input('name');
<input type="text" name="name">
Same from label, its second argument is for the class attribute:
<?php // ... echo $form->input('name', 'form-control');
<input type="text" name="name" class="form-control">
The input type can be changed using the withType method or the convenient asEmail and asNumber methods:
<?php // ... echo $form->input('email')->asEmail();
<input type="email" name="email">
<?php // ... echo $form->input('age')->asNumber();
<input type="number" name="age">
Buttons
To create a <button> element, the button method is used:
<?php // ... echo $form->button('Submit');
<button type="button">Submit</button>
Use the second argument for specifying its class attribute:
<?php // ... echo $form->button('Submit', 'btn btn-primary');
<button type="button" class="btn btn-primary">Submit</button>
The button type can be changed using the withType method:
<?php // ... echo $form->button('Submit')->withType('submit');
<button type="submit">Submit</button>
Select dropdowns
To create a <select> element, the select method is used. An array of items can be passed to populate the options:
<?php // ... $items = array('Male', 'Female'); echo $form->select('gender', $items);
<select name="gender"> <option value="">Please select</option> <option value="0">Male</option> <option value="1">Female</option> </select>
An associative array with id and name keys can also be provided:
$items = [ array('id' => 'm', 'name' => 'Male') ]; $items[] = array('id' => 'f', 'name' => 'Female'); echo $form->select('gender', $items);
<select name="gender"> <option value="">Please select</option> <option value="m">Male</option> <option value="f">Female</option> </select>
Error messages
The error method is used to create a placeholder for validation error messages:
<?php // ... echo $form->error('error.name');
<template x-if="error.name"> <p class="text-danger small mb-0" x-text="error.name[0]"></p> </template>
Note
This is only works when integrated in alpinejs.
Integration to alpinejs
Fortem provides several methods for seamless integration with alpinejs.
The asModel method adds the x-model attribute to an input or select element, binding its value to its variable:
<?php // ... echo $form->input('name')->asModel();
<input type="text" name="name" x-model="name">
<?php // ... echo $form->select('gender', $items)->asModel();
<select name="gender" x-model="gender">...</select>
The disablesOn method adds the :disabled attribute, allowing an element to be disabled based on its variable:
<?php // ... echo $form->input('name')->disablesOn('loading');
<input type="text" name="name" :disabled="loading">
The onClick method adds the @click attribute to a button, executing its function on click:
<?php // ... echo $form->button('Submit')->onClick('submitForm');
<button type="button" @click="submitForm">Submit</button>
Scripts
The script method helps create a JavaScript object from PHP. This is useful for initializing data for alpinejs:
<?php // ... echo $form->script('data') ->with('name', 'John Doe') ->with('age', 30) ->withLoading() ->withError();
<script> let data = {"name":"John Doe","age":30,"loading":false,"error":{}}; </script>
Link Helper
The LinkHelper class helps in generating and checking URLs:
use Rougin\Fortem\Helpers\LinkHelper; $server = array(); $server['HTTP_HOST'] = 'localhost'; $server['REQUEST_URI'] = '/'; $link = new LinkHelper($server); echo $link; // http://localhost/
Use the isActive method to check if a given link is the current URL:
$current = $link->isActive('/'); // true
If HTTP_HOST is not available, the setBase method can be used:
use Rougin\Fortem\Helpers\LinkHelper; $link = new LinkHelper(array()); $link->setBase('roug.in') echo $link; // http://roug.in/
Change log
Please see CHANGELOG for more recent changes.
Contributing
See CONTRIBUTING on how to contribute.
License
The MIT License (MIT). Please see LICENSE for more information.