PHPackages                             ray/web-form-module - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. ray/web-form-module

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

ray/web-form-module
===================

Web Form module for Ray.Di

0.6.0(7y ago)322.2k—0%4[1 PRs](https://github.com/ray-di/Ray.WebFormModule/pulls)MITPHPPHP &gt;=7.0.0

Since Mar 24Pushed 7y ago1 watchersCompare

[ Source](https://github.com/ray-di/Ray.WebFormModule)[ Packagist](https://packagist.org/packages/ray/web-form-module)[ RSS](/packages/ray-web-form-module/feed)WikiDiscussions 1.x Synced 1mo ago

READMEChangelog (10)Dependencies (6)Versions (19)Used By (0)

Ray.WebFormModule
=================

[](#raywebformmodule)

[![Scrutinizer Code Quality](https://camo.githubusercontent.com/3c6cc0024c0e09135a02661d0287ce7d87f2eb015bf2ba7ebbb7236ab611284c/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f7261792d64692f5261792e576562466f726d4d6f64756c652f6261646765732f7175616c6974792d73636f72652e706e673f623d312e78)](https://scrutinizer-ci.com/g/ray-di/Ray.WebFormModule/?branch=1.x)[![Code Coverage](https://camo.githubusercontent.com/8838744349c89ddb103fc24f4d6312977b27cdac54447a767928d9568700391d/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f7261792d64692f5261792e576562466f726d4d6f64756c652f6261646765732f636f7665726167652e706e673f623d312e78)](https://scrutinizer-ci.com/g/ray-di/Ray.WebFormModule/?branch=1.x)[![Build Status](https://camo.githubusercontent.com/747e15cce15c239741696ebc4a1a1e6a30c1da7568a398a3a1aa9cc7ddf59537/68747470733a2f2f7472617669732d63692e6f72672f7261792d64692f5261792e576562466f726d4d6f64756c652e7376673f6272616e63683d312e78)](https://travis-ci.org/ray-di/Ray.WebFormModule)

An aspect oriented web form module powered by [Aura.Input](https://github.com/auraphp/Aura.Input) and [Ray.Di](https://github.com/ray-di/Ray.Di).

Getting Started
===============

[](#getting-started)

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

[](#installation)

### Composer install

[](#composer-install)

```
$ composer require web-form-module

```

### Module install

[](#module-install)

```
use Ray\Di\AbstractModule;
use Ray\WebFormModule\AuraInputModule;

class AppModule extends AbstractModule
{
    protected function configure()
    {
        $this->install(new AuraInputModule);
    }
}
```

Usage
-----

[](#usage)

### Form class

[](#form-class)

We provide two methods on self-initializing form class, one is `init()` method where we add an input field on form and apply fileters and rules. The other method method is `submit()` where it submit data. See more detail at [Aura.Input self-initializing forms](https://github.com/auraphp/Aura.Input/blob/1.x/README.md#self-initializing-forms).

```
use Ray\WebFormModule\AbstractForm;
use Ray\WebFormModule\SetAntiCsrfTrait;

class MyForm extends AbstractForm
{
    // for anti CSRF
    use SetAntiCsrfTrait;

    /**
     * {@inheritdoc}
     */
    public function init()
    {
        $this->setField('name', 'text')
             ->setAttribs([
                 'id' => 'name'
             ]);
        $this->filter->validate('name')->is('alnum');
        $this->filter->useFieldMessage('name', 'Name must be alphabetic only.');
    }

    /**
     * {@inheritdoc}
     */
    public function submit()
    {
        return $_POST;
    }

    /**
     * {@inheritdoc}
     */
    public function __toString()
    {
        $form = $this->form();
        // name
        $form .= $this->helper->tag('div', ['class' => 'form-group']);
        $form .= $this->helper->tag('label', ['for' => 'name']);
        $form .= 'Name:';
        $form .= $this->helper->tag('/label') . PHP_EOL;
        $form .= $this->input('name');
        $form .= $this->error('name');
        $form .= $this->helper->tag('/div') . PHP_EOL;
        // submit
        $form .= $this->input('submit');
        $form .= $this->helper->tag('/form');

        return $form;
    }
}
```

### Controller

[](#controller)

We annotate the methods which web form validation is required with `@FormValidation`. We can specify form object property name with `name` and failiure method name with `@onFailure`.

```
use Ray\Di\Di\Inject;
use Ray\Di\Di\Named;
use Ray\WebFormModule\Annotation\FormValidation;
use Ray\WebFormModule\FormInterface;

class MyController
{
    /**
     * @var FormInterface
     */
    protected $contactForm;

    /**
     * @Inject
     * @Named("contact_form")
     */
    public function setForm(FormInterface $form)
    {
        $this->contactForm = $form;
    }

    /**
     * @FormValidation(form="contactForm", onFailure="badRequestAction")
     */
    public function createAction()
    {
        // validation success
    }

    public function badRequestAction()
    {
        // validation faild
    }
}
```

### View

[](#view)

You can render entire form html when `__toString` is given.

```
  echo $form; // render entire form html
```

or render input element basis.

```
  echo $form->input('name'); //
  echo $form->error('name'); // "Name must be alphabetic only." or blank.
```

CSRF Protections
----------------

[](#csrf-protections)

```
use Ray\WebFormModule\SetAntiCsrfTrait;

class MyController
{
    use SetAntiCsrfTrait;
```

You can provide your custom `AntiCsrf` class. See more detail at [Aura.Input](https://github.com/auraphp/Aura.Input#applying-csrf-protections)

Validation Exception
--------------------

[](#validation-exception)

When we install `Ray\WebFormModule\FormVndErrorModule` as following,

```
use Ray\Di\AbstractModule;

class FakeVndErrorModule extends AbstractModule
{
    protected function configure()
    {
        $this->install(new AuraInputModule);
        $this->override(new FormVndErrorModule);
    }
```

A `Ray\WebFormModule\Exception\ValidationException` will be thrown. We can echo catched exception to get [application/vnd.error+json](https://tools.ietf.org/html/rfc6906) media type.

```
echo $e->error;

//{
//    "message": "Validation failed",
//    "path": "/path/to/error",
//    "validation_messages": {
//        "name": [
//            "Name must be alphabetic only."
//        ]
//    }
//}
```

More detail for `vnd.error+json`can be add with `@VndError` annotation.

```
    /**
     * @FormValidation(form="contactForm")
     * @VndError(
     *   message="foo validation failed",
     *   logref="a1000", path="/path/to/error",
     *   href={"_self"="/path/to/error", "help"="/path/to/help"}
     * )
     */
```

This optional module is handy for API application.

### Demo

[](#demo)

```
$ php -S docs/demo/1.csrf/web.php

```

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity30

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 99.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~68 days

Recently: every ~244 days

Total

18

Last Release

2905d ago

Major Versions

0.5.4 → 2.x-dev2017-05-22

0.5.5 → 1.x-dev2018-05-27

### Community

Maintainers

![](https://www.gravatar.com/avatar/db4fc75ffc631168d0d7143b6f2c24b1534dfb921212bd851c026c5cbbb1344d?d=identicon)[koriym](/maintainers/koriym)

---

Top Contributors

[![koriym](https://avatars.githubusercontent.com/u/529021?v=4)](https://github.com/koriym "koriym (115 commits)")[![kumamidori](https://avatars.githubusercontent.com/u/384567?v=4)](https://github.com/kumamidori "kumamidori (1 commits)")

---

Tags

ray-moduleweb-formRay.Di moduleweb form

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/ray-web-form-module/health.svg)

```
[![Health](https://phpackages.com/badges/ray-web-form-module/health.svg)](https://phpackages.com/packages/ray-web-form-module)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
