PHPackages                             tatter/forms - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. tatter/forms

ActiveLibrary[HTTP &amp; Networking](/categories/http)

tatter/forms
============

RESTful AJAX forms for CodeIgniter 4

v0.10.0(3y ago)71.9k7[3 issues](https://github.com/tattersoftware/codeigniter4-forms/issues)[1 PRs](https://github.com/tattersoftware/codeigniter4-forms/pulls)MITPHPPHP ^7.4 || ^8.0

Since Mar 29Pushed 2y ago1 watchersCompare

[ Source](https://github.com/tattersoftware/codeigniter4-forms)[ Packagist](https://packagist.org/packages/tatter/forms)[ Docs](https://github.com/tattersoftware/codeigniter4-forms)[ Fund](https://paypal.me/tatter)[ GitHub Sponsors](https://github.com/tattersoftware)[ RSS](/packages/tatter-forms/feed)WikiDiscussions develop Synced 2w ago

READMEChangelog (4)Dependencies (2)Versions (6)Used By (0)

Tatter\\Forms
=============

[](#tatterforms)

RESTful AJAX forms for CodeIgniter 4

[![](https://github.com/tattersoftware/codeigniter4-forms/workflows/PHPUnit/badge.svg)](https://github.com/tattersoftware/codeigniter4-forms/actions/workflows/phpunit.yml)[![](https://github.com/tattersoftware/codeigniter4-forms/workflows/PHPStan/badge.svg)](https://github.com/tattersoftware/codeigniter4-forms/actions/workflows/phpstan.yml)[![](https://github.com/tattersoftware/codeigniter4-forms/workflows/Deptrac/badge.svg)](https://github.com/tattersoftware/codeigniter4-forms/actions/workflows/deptrac.yml)[![Coverage Status](https://camo.githubusercontent.com/f5eda2c41035a7e17b64a5ee44e8f89638d78b265a6902a2d23fe79a21edc8b2/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f746174746572736f6674776172652f636f646569676e69746572342d666f726d732f62616467652e7376673f6272616e63683d646576656c6f70)](https://coveralls.io/github/tattersoftware/codeigniter4-forms?branch=develop)

Quick Start
-----------

[](#quick-start)

1. Install with Composer: `> composer require tatter/forms`
2. Publish the assets: `> php spark assets:publish`
3. Add the JavaScript to your views

Features
--------

[](#features)

Provides Resource and Presenter controller templates and corresponding JavaScript for using AJAX forms with CodeIgniter 4's native RESTful implementation.

Installation
------------

[](#installation)

Install easily via Composer to take advantage of CodeIgniter 4's autoloading capabilities and always be up-to-date:

```
> composer require tatter/forms
```

Or, install manually by downloading the source files and adding the directory to **app/Config/Autoload.php**.

After installation you will need to copy or publish the required assets to your **public/** folder. If you want to automate this process check out the [Assets Library](https://github.com/tattersoftware/codeigniter4-assets).

Finally, notify your view/layout of your intention to use the JavaScript for your forms (your paths may vary):

```

	var baseUrl = "";
	var siteUrl = "";
	var apiUrl  = "";

```

Configuration (optional)
------------------------

[](#configuration-optional)

The library's default behavior can be overridden or augment by its config file. Copy **examples/Forms.php** to **app/Config/Forms.php** and follow the instructions in the comments. If no config file is found the library will use its defaults.

Usage
-----

[](#usage)

***Note:*** Please consider this module to be modular itself - you need not use every piece!\* *Treat portions of the code that you do not use as examples for how to implement this in your own app.*

After the initial installation there are a few pieces to implement. **Forms** will run CRUD-style operations for you by interfacing views with the `ResourcePresenter` or `ResourceController` depending on the method of interaction (i.e. page load versus AJAX). Not surprisingly, you will need some **Views** and two **Controllers** per resource.

### Naming

[](#naming)

**Forms** interacts with each resource route and controller through that resource's name. If you are creating a game, your resource names might be *hero(es)*, *level(s)*, *reward(s)*, etc. The naming convention is important for autoloading resources and their endpoints. By default, **Forms** will use the name of the model associated with your resource. So a URL of `heroes/new` would route to the `HeroController` which uses `HeroModel` and the whole resource would be dubbed "hero" off that model.

If you need to set your own names, do so with your model's `$name` property:

```
class HeroModel extends \CodeIgniter\Model
{
	public $name = 'superhero';
	...
```

### Views

[](#views)

If you choose to use the built-in Controllers they expect the following views to be available for each resource (where {names} is the plural of your resource name):

- **Views/{names}/new** - Prompt to create a new object, wrapping **{names}/form**
- **Views/{names}/index** - List of all (or filtered) objects, wrapping **{names}/list**
- **Views/{names}/show** - Details of a single object, wrapping **{names}/display**
- **Views/{names}/edit** - Prompt to change an object, wrapping **{names}/form**
- **Views/{names}/remove** - Prompt to remove an object, wrapping **{names}/display** and **{names}/confirm**
- **Views/{names}/list** - Patial view; a list of objects
- **Views/{names}/form** - Patial view; the form used for new and edit
- **Views/{names}/display** - Partial view; a displayable verison of one object
- **Views/{names}/confirm** - Partial view; prompt to delete an object

As you can see **Forms** expects some views that are part of a full page load layout and some that can be injected into an existing page via AJAX (e.g. in a modal). See [examples](examples/Views/) for a full set of example view files (note: these are presented "as is" and may not always be the best solution for all use cases).

### Controllers

[](#controllers)

In addition to the views, you will need two controllers for each resource:

- **Controllers/{names}.php** - Your presenter for page loads, extends `Tatter\Forms\Controllers\ResourcePresenter`
- **Controllers/API/{names}.php** - Your controller for AJAX calls, extends `Tatter\Forms\Controllers\ResourceController`

As with other framework RESTful controllers, your controllers set their model via the `$modelName` property:

```
