---
title: "Form Project"
description: "Derafu Form"
type: "docs"
category: "doc"
tags: [php]
authors: [Anonymous]
date: "2026-08-21"
last_update: "2026-08-21"
time_minutes: 1
draft: false
unlisted: false
url: "https://www.derafu.dev/docs/ui/form"
---

# Derafu Form



---

## Introduction

Declarative Forms, Seamless Rendering

# Declarative Forms, Seamless Rendering

![GitHub last commit](https://img.shields.io/github/last-commit/derafu/form/main)
![CI Workflow](https://github.com/derafu/form/actions/workflows/ci.yml/badge.svg?branch=main&amp;event=push)
![GitHub code size in bytes](https://img.shields.io/github/languages/code-size/derafu/form)
![GitHub Issues](https://img.shields.io/github/issues-raw/derafu/form)
![Total Downloads](https://poser.pugx.org/derafu/form/downloads)
![Monthly Downloads](https://poser.pugx.org/derafu/form/d/monthly)

A modern PHP form library that leverages a declarative, schema-based approach to form definition and rendering, compatible with JSON Forms while providing a powerful backend-centric workflow.

&gt; [!NOTE] WIP
&gt;
&gt; Work In Progress.

## Key Features

- **Schema-Based Form Definition**: Define forms using structured arrays that clearly separate data schemas from UI layouts.
- **Backend-First Approach**: Built for PHP developers who want control over form generation with minimum effort.
- **Symfony-Compatible Twig Extensions**: Familiar syntax for Symfony developers, minimizing learning curve.
- **Decoupled Rendering System**: Separate your form logic from its presentation with specialized renderers.
- **JSON Forms Compatibility**: Define once, use anywhere - same schema can be used with frontend JSON Forms library.
- **Automatic Form Generation**: Generate form UI schemas automatically from your data models.
- **Extensible Architecture**: Easily add custom types, layouts and renderers.

## Why Choose This Library?

### Compared to Traditional PHP Form Libraries

Traditional PHP form libraries often tightly couple form definition with HTML generation, making it difficult to separate concerns. This library takes a different approach:

1. **Declarative Rather Than Imperative**: Define _what_ your form should be rather than _how_ it should be built, step by step.
2. **Clear Separation of Concerns**: Schema (data structure) is separate from UI schema (presentation) and data (values).
3. **Powerful Layouts Without HTML Knowledge**: Create complex, multi-column layouts without writing HTML.

### Compared to Frontend-Only Solutions

Unlike frontend-only form builders, this library:

1. **Keeps Validation Logic Server-Side**: Where it belongs for security-critical applications.
2. **Provides PHP-Native Form Definition**: No need to write JavaScript to define your forms.
3. **Works With or Without JavaScript**: Generate the same forms for both traditional and SPA applications.

## Installation

Install the library using Composer:

```shell
composer require derafu/form
```

## Basic Usage

### Creating a Simple Form

```php
// Define the form structure.
$definition = [
    &#039;schema&#039; =&gt; [
        &#039;type&#039; =&gt; &#039;object&#039;,
        &#039;properties&#039; =&gt; [
            &#039;name&#039; =&gt; [
                &#039;type&#039; =&gt; &#039;string&#039;,
                &#039;title&#039; =&gt; &#039;Full Name&#039;,
            ],
            &#039;email&#039; =&gt; [
                &#039;type&#039; =&gt; &#039;string&#039;,
                &#039;format&#039; =&gt; &#039;email&#039;,
                &#039;title&#039; =&gt; &#039;Email Address&#039;,
            ],
        ],
        &#039;required&#039; =&gt; [&#039;name&#039;, &#039;email&#039;],
    ],
    &#039;uischema&#039; =&gt; [
        &#039;type&#039; =&gt; &#039;VerticalLayout&#039;,
        &#039;elements&#039; =&gt; [
            [
                &#039;type&#039; =&gt; &#039;Control&#039;,
                &#039;scope&#039; =&gt; &#039;#/properties/name&#039;,
            ],
            [
                &#039;type&#039; =&gt; &#039;Control&#039;,
                &#039;scope&#039; =&gt; &#039;#/properties/email&#039;,
            ],
        ],
    ],
    &#039;data&#039; =&gt; [
        &#039;name&#039; =&gt; &#039;&#039;,
        &#039;email&#039; =&gt; &#039;&#039;,
    ],
];

// Create the form.
$form = $formFactory-&gt;create($definition);
```

### Rendering with Twig

```twig
{# Simple rendering of the entire form. #}
{{ form(form) }}

{# Or with more control over individual components. #}
{{ form_start(form) }}
    {{ form_row(form.fields.name) }}
    {{ form_row(form.fields.email) }}
    &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary&quot;&gt;Submit&lt;/button&gt;
{{ form_end(form) }}
```

## Automatic Schema Generation

The library can automatically generate a schema from your data:

```php
$definition = [
    &#039;data&#039; =&gt; [
        &#039;name&#039; =&gt; &#039;John Doe&#039;,
        &#039;email&#039; =&gt; &#039;john.doe@example.com&#039;,
        &#039;age&#039; =&gt; 34,
        &#039;birthdate&#039; =&gt; &quot;1990-01-01&quot;,
    ],
];

// Schema and UI schema will be automatically generated.
$form = $formFactory-&gt;create($definition);
```

## Integration with JSON Forms

This library&#039;s form definitions are compatible with the [JSON Forms](https://jsonforms.io) JavaScript library, allowing you to use the same definitions for both server-side and client-side rendering:

```html
&lt;div id=&quot;json-form-container&quot;&gt;&lt;/div&gt;
```

```javascript
// https://raw.githubusercontent.com/derafu/form/refs/heads/main/assets/js/jsonforms-viewer.js
import &#039;./jsonforms-viewer.js&#039;;

// Render the form and save the reference.
const formDefinition = {{ jsonFormsDefinition | json_encode | raw }};
const formInstance = window.renderJsonForm(&#039;json-form-container&#039;, formDefinition);
// You can now use `formInstance` to get the form data or its errors.
```

### Development Requirements

To render forms using JSON Forms you must install:

```shell
npm install @jsonforms/core @jsonforms/react @jsonforms/material-renderers \
    react react-dom @mui/material @emotion/react @emotion/styled
```

## Form Layouts

The UI schema supports various layout types by default:

- **VerticalLayout**: Fields stacked vertically.
- **HorizontalLayout**: Fields arranged horizontally.
- **Group**: Logical grouping of fields, often with a label.
- **Categorization**: Tab-based layouts for complex forms.

Example:

```php
$uischema = [
    &#039;type&#039; =&gt; &#039;VerticalLayout&#039;,
    &#039;elements&#039; =&gt; [
        [
            &#039;type&#039; =&gt; &#039;Group&#039;,
            &#039;label&#039; =&gt; &#039;Personal Information&#039;,
            &#039;elements&#039; =&gt; [
                [&#039;type&#039; =&gt; &#039;Control&#039;, &#039;scope&#039; =&gt; &#039;#/properties/name&#039;],
                [&#039;type&#039; =&gt; &#039;Control&#039;, &#039;scope&#039; =&gt; &#039;#/properties/email&#039;],
            ],
        ],
        [
            &#039;type&#039; =&gt; &#039;Group&#039;,
            &#039;label&#039; =&gt; &#039;Address&#039;,
            &#039;elements&#039; =&gt; [
                [&#039;type&#039; =&gt; &#039;Control&#039;, &#039;scope&#039; =&gt; &#039;#/properties/street&#039;],
                [&#039;type&#039; =&gt; &#039;Control&#039;, &#039;scope&#039; =&gt; &#039;#/properties/city&#039;],
            ],
        ],
    ],
];
```




---

## Examples

Examples of how to use the form component.

# Form Examples




---

### From Data

The simplest form, created from data only.






---

### Control

The default controls provided by the form library.






---

### User Registration

Form to create a new user account.






---

### User Login

A user login form with username, password and 2FA options.






---

### User Profile with Cards

User profile form with grouped sections in cards.






---

### User Profile with Tabs

User profile form with tabs and cards.






---

### Setup Wizard

Multi-step wizard for account setup.






---

### Invoice Issuance

Form for creating invoices with basic fields and line items.






---

### Advanced Invoice Search

Complex search form for filtering invoices with conditional fields.






---

### E-Invoicing Configuration

Comprehensive configuration form for electronic invoicing.






---

### Contact Form

Simple contact form with default values and validation.






---

### User Registration

Complex user registration form with nested fields and validation.






---

### Collection

Collections and arrays in forms.






---

### Editors

Different editors using controls.






---

### Product Ecommerce

Product creation form with data processing rules.






---

### Rules

JSON Forms rules in UI Schema.






---

### Cascade

Select options in a cascade.







---
Last updated on 21/08/2026
#php
