Download the PHP package coroq/form without Composer

On this page you can find all versions of the php package coroq/form. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package form

Coroq Form

PHP form validation library. Type-safe, zero dependencies.

Scope

What This Library Does

What This Library Does NOT Do

This is a validation and data processing layer that sits between your HTTP layer and business logic.

Requirements

Installation

Quick Start

Core Concepts

1. Forms and Form Items

A Form holds items with names. Each item represents a single field - an email address, a username, a number, etc.

2. Setting Values

When you assign values to a form, the form distributes those values to its items by matching names. Each item receives and stores its corresponding value.

3. Filtering

The moment a value is set, it is automatically filtered - normalized and transformed according to the item's type. Email addresses get trimmed and lowercased, numbers get stripped of formatting.

4. Validation and Errors

When you request validation, the form checks all its items. Each item validates its value against its rules. The form returns whether all items are valid. Invalid items store an error object representing what went wrong.

Defining Forms

Recommended: Form Subclasses

Define form classes with typed readonly properties for IDE support:

Dynamic Forms (for temporal use)

For dynamic or one-off forms, you can use Form directly:

Form State

Form items have three state flags that control their behavior:

Required/Optional

Input level:

Form level:

Form-level example:

Read-Only

Input level:

Form level:

Form-level example:

Disabled

Disabled items are excluded from output and validation, but still accept values.

Item behavior when disabled:

Parent form behavior:

Form-level example:

State Summary

State setValue() getValue() validate()
Normal (required=true) ✓ Sets value ✓ Returns value ✓ Validated, must not be empty
Optional (required=false) ✓ Sets value ✓ Returns value ✓ Validated, empty allowed
Read-only ✗ Ignored ✓ Returns value ✓ Validated
Disabled ✓ Sets value Returns empty value ✗ Skipped

Form-level states apply to the form as a whole:

Validation

Custom Validators

All Input subclasses support custom validators via setValidator(). This allows you to add validation logic without creating custom subclasses.

Basic Example

How It Works

The validator:

Advanced Examples

Accessing form item properties:

External Validation

When you validate a value in external logic (authentication, API calls, business rules) but want to hold the error in the form, use setError() on a form item. The form item can be used only for holding the error.

Conditional Validation

Make validation rules conditional based on other field values by overriding setValue() in your Form subclass.

This pattern works for any conditional logic: disabling fields, changing constraints, or toggling entire sections.

For conditional sections (multi-step forms, draft/publish workflows), use nested forms with setDisabled():

Error Handling

Error Customizer

Transform error objects before they are stored. Useful for converting generic errors to field-specific error types.

The customizer receives $error and $formItem, runs after validation, and returns the transformed error. You can replace the error object or mutate it by adding properties.

Error Messages

Use ErrorMessageFormatter to convert error objects to human-readable messages. You define your own message set by mapping error class names to messages (strings or closures).

Basic Usage

Error Hierarchy and Inheritance

The formatter uses instanceof matching, supporting error class inheritance. Many specific errors extend base error types. For example, InvalidEmailError, InvalidUrlError, InvalidDateError, InvalidMimeTypeError, and InvalidExtensionError all extend InvalidError.

Define base messages as defaults, then override specific types as needed:

Later definitions override earlier ones. This makes it easy to merge preset messages with custom overrides:

Adding Individual Messages

Use setMessage() to add or override individual messages without replacing the entire set:

Dynamic Messages with Closures

Use closures to access error object properties for dynamic messages:

Custom Error Types

You can create custom error classes for application-specific validation:

Built-in Error Types

The library provides these error types:

Base Errors:

*Invalid Hierarchy (all extend InvalidError):**

Range/Length Errors:

Type/Format Errors:

Selection Errors:

File Errors:

Derived Errors:

Tip: Define messages for base error types (like InvalidError) as catch-alls, then optionally override specific subtypes for custom messages.

Form Values

Forms provide four methods to retrieve values:

getParsedValue() Contract

getParsedValue() returns null if the value is empty or invalid, otherwise returns the value converted to its appropriate type.

This applies to all form items:

Special cases:

Input Types

Text Input

Unicode Normalization:

Text input values are normalized using NFC (Canonical Composition) by default if the intl extension is available. This ensures consistent character representation (e.g., Japanese combining marks: か゛ → が).

For normalization form details, see Normalizer class documentation.

Email Input

URL Input

UrlInput validates URLs using PHP's FILTER_VALIDATE_URL. It converts full-width characters to half-width and trims whitespace. You can restrict allowed schemes (default: http, https).

Telephone Input

TelInput strips all formatting characters (spaces, hyphens, parentheses) but preserves a leading + for international E.164 format. It does NOT validate phone numbers - use libphonenumber for validation and formatting:

Select Input

Multi-Select Input

Number Inputs

Note on IntegerInput limits:

Date Input

Boolean Input

BooleanInput considers only '', null, and false as "empty" (false). Everything else including '0', 0, 'off', 'no' is considered "not empty" (true).

File Input

FileInput validates files by their path. It checks file size, MIME type, and extension. This library does not handle HTTP file uploads ($_FILES) - that should be done by your HTTP layer.

FileInput works with file paths (strings), not $_FILES arrays. For tracking uploaded files across form submissions, use a separate TextInput for file ID.

Example upload flow:

Nested Forms

Repeating Forms

RepeatingForm manages dynamic lists of form items using a factory pattern:

Factory Function

The factory function receives an index parameter:

Nested Repeating Forms

RepeatingForm can contain other forms, including nested RepeatingForms:

Items can be added programmatically:

Derived Inputs

Derived inputs are special form items that depend on other form items. They can:

Key Properties:

Basic Example: Calculated Values

More Calculation Examples

Cross-Field Validation

Use setValidator() to validate relationships between fields. The validator receives:

  1. All source values as individual parameters
  2. The calculated value as the last parameter (or null if no calculator)

The validator returns an Error object if invalid, or null if valid.

Note: Derived validation only runs if all source inputs pass their own validation first. If any source fails, the Derived item automatically gets a SourceItemInvalidError.

Combined: Calculation with Validation

You can use both setValueCalculator() and setValidator() together:

Complete Example

Configuration

UTF-8 Invalid Character Handling

This library assumes all input is UTF-8 encoded. Invalid UTF-8 byte sequences are automatically replaced with a substitute character during filtering.

By default, PHP uses ? (U+003F QUESTION MARK) as the substitute character. For better visibility of data corruption, it's recommended to use � (U+FFFD REPLACEMENT CHARACTER) instead by configuring it in your application bootstrap:

Alternative configurations:

See mb_substitute_character documentation for more options.

API Reference

Form

Input

All input types extend Input and support:

Text Input

Select/MultiSelect

Number Inputs

Boolean Input

File Input

RepeatingForm

License

MIT


All versions of form with dependencies

PHP Build Version
Package Version
Requires php Version ^8.0
ext-mbstring Version *
ext-bcmath Version *
ext-fileinfo Version *
ext-filter Version *
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package coroq/form contains the following files

Loading the files please wait ...