2017 © Pedro Peláez
 

library web-form-module

Web Form module for Ray.Di

image

ray/web-form-module

Web Form module for Ray.Di

  • Sunday, May 27, 2018
  • by koriym
  • Repository
  • 1 Watchers
  • 2 Stars
  • 13,277 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 3 Forks
  • 0 Open issues
  • 21 Versions
  • 12 % Grown

The README.md

Ray.WebFormModule

Scrutinizer Code Quality Code Coverage Build Status, (*1)

An aspect oriented web form module powered by Aura.Input and Ray.Di., (*2)

Getting Started

Installation

Composer install

$ composer require web-form-module

Module install

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

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

Usage

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., (*3)

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

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., (*4)

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

You can render entire form html when __toString is given., (*5)

  echo $form; // render entire form html

or render input element basis., (*6)

  echo $form->input('name'); // <input id="name" type="text" name="name" size="20" maxlength="20" />
  echo $form->error('name'); // "Name must be alphabetic only." or blank.

CSRF Protections

use Ray\WebFormModule\SetAntiCsrfTrait;

class MyController 
{
    use SetAntiCsrfTrait;

You can provide your custom AntiCsrf class. See more detail at Aura.Input, (*7)

Validation Exception

When we install Ray\WebFormModule\FormVndErrorModule as following,, (*8)

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 media type., (*9)

echo $e->error;

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

More detail for vnd.error+jsoncan be add with @VndError annotation., (*10)

    /**
     * @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., (*11)

Demo

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

The Versions

27/05 2018

1.x-dev

1.9999999.9999999.9999999-dev

Web Form module for Ray.Di

  Sources   Download

MIT

The Requires

 

The Development Requires

ray.di module web form

27/05 2018

0.6.0

0.6.0.0

Web Form module for Ray.Di

  Sources   Download

MIT

The Requires

 

The Development Requires

ray.di module web form

27/05 2018

dev-to_string

dev-to_string

Web Form module for Ray.Di

  Sources   Download

MIT

The Requires

 

The Development Requires

ray.di module web form

27/05 2018

dev-spike

dev-spike

Web Form module for Ray.Di

  Sources   Download

MIT

The Requires

 

The Development Requires

ray.di module web form

27/05 2018

dev-refactor

dev-refactor

Web Form module for Ray.Di

  Sources   Download

MIT

The Requires

 

The Development Requires

ray.di module web form

22/05 2017

0.5.5

0.5.5.0

Web Form module for Ray.Di

  Sources   Download

MIT

The Requires

 

The Development Requires

ray.di module web form

22/05 2017

2.x-dev

2.9999999.9999999.9999999-dev

Web Form module for Ray.Di

  Sources   Download

MIT

The Requires

 

The Development Requires

ray.di module web form

25/09 2015

0.5.4

0.5.4.0

Web Form module for Ray.Di

  Sources   Download

MIT

The Requires

 

ray.di module web form

25/09 2015

0.5.3

0.5.3.0

Web Form module for Ray.Di

  Sources   Download

MIT

The Requires

 

ray.di module web form

18/09 2015

0.5.2

0.5.2.0

Web Form module for Ray.Di

  Sources   Download

MIT

The Requires

 

ray.di module web form

18/09 2015

0.5.1

0.5.1.0

Web Form module for Ray.Di

  Sources   Download

MIT

The Requires

 

ray.di module web form

16/09 2015

0.5.0

0.5.0.0

Web Form module for Ray.Di

  Sources   Download

MIT

The Requires

 

ray.di module web form

16/09 2015

0.4.0

0.4.0.0

Web Form module for Ray.Di

  Sources   Download

MIT

The Requires

 

ray.di module web form

15/09 2015

0.3.3

0.3.3.0

Web Form module for Ray.Di

  Sources   Download

MIT

The Requires

 

ray.di module web form

10/09 2015

0.3.2

0.3.2.0

Web Form module for Ray.Di

  Sources   Download

MIT

The Requires

 

ray.di module web form

10/09 2015

0.3.1

0.3.1.0

Web Form module for Ray.Di

  Sources   Download

MIT

The Requires

 

ray.di module web form

09/09 2015

0.3.0

0.3.0.0

Web Form module for Ray.Di

  Sources   Download

MIT

The Requires

 

ray.di module web form

07/09 2015

0.2.1

0.2.1.0

Web Form module for Ray.Di

  Sources   Download

MIT

The Requires

 

ray.di module web form

04/09 2015

0.2.0

0.2.0.0

Web Form module for Ray.Di

  Sources   Download

MIT

The Requires

 

ray.di module web form

25/08 2015

0.1.0

0.1.0.0

Web Form module for Ray.Di

  Sources   Download

MIT

The Requires

 

ray.di module web form

24/03 2015

0.1.1

0.1.1.0

Validation module for Ray.Di

  Sources   Download

BSD-3-Clause

The Requires

 

validation ray.di module