2017 © Pedro Peláez
 

library zf-form

Simple form for Zend Framework 3.

image

bupy7/zf-form

Simple form for Zend Framework 3.

  • Thursday, March 22, 2018
  • by bupy7
  • Repository
  • 2 Watchers
  • 2 Stars
  • 168 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 9 Versions
  • 2 % Grown

The README.md

zf-form

Latest Stable Version Total Downloads Latest Unstable Version License Build Status Coverage Status, (*1)

Simple and powerful form with laminas/laminas-inputfilter and bupy7/php-html-form., (*2)

If you want to use only form input filter I recomend to you php-input-filter instead. In the future this package (zf-form) will work together with php-input-filter., (*3)

Installation

The preferred way to install this extension is through composer., (*4)

Either run, (*5)

$ php composer.phar require bupy7/zf-form "*"

or add, (*6)

"bupy7/zf-form": "*"

to the require section of your composer.json file., (*7)

Usage

Form:, (*8)

// module/Application/src/Form/SignInForm.php

use Bupy7\Form\FormAbstract;

class SignInForm extends FormAbstract
{
    /**
     * @var string 
     */
    public $email;
    /**
     * @var string 
     */
    public $password;

    protected function inputs()
    {
        return [
            [
                'name' => 'email',
                'required' => true,
                'validators' => [
                    [
                        'name' => 'EmailAddress',
                    ],
                ],
            ],
            [
                'name' => 'password',
                'required' => true,
            ],
        ];
    }
}

View:, (*9)

// module/Application/view/auth/signin.phtml

formBuilder($this->signInForm); ?>
= $formBuilder->open()->action($this->url('signin'))->addClass('form-horizontal'); ?>
    <div class="form-group required<?= $formBuilder->hasError('email') ? ' has-error' : ''; ?>">
        <?= $formBuilder->label('Email')->forId('email')->addClass('control-label col-md-2'); ?>
        <div class="col-md-4">
            <?= $formBuilder->email('email')->addClass('form-control'); ?>
            <div class="help-block help-block-error">
                <?= $formBuilder->getError('email'); ?>
            </div>
        </div>
    </div>
    <div class="form-group required<?= $formBuilder->hasError('password') ? ' has-error' : ''; ?>">
        <?= $formBuilder->label('Password')->forId('password')->addClass('control-label col-md-2'); ?>
        <div class="col-md-4">
            <?= $formBuilder->password('password')->addClass('form-control'); ?>
            <div class="help-block help-block-error">
                <?= $formBuilder->getError('password'); ?>
            </div>
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-4 col-md-offset-2">
            <?= $formBuilder->button('Signin')->attribute('type', 'submit')->addClass('btn btn-primary'); ?>
        </div>
    </div>
= $formBuilder->close(); ?>

Controller:, (*10)

// module/Application/src/Controller/AuthController.php

use Application/Form/SignInForm;

public function signinAction()
{
    $signInForm = new SignInForm;
    if ($this->getRequest()->isPost()) {
        $signInForm->setValues($this->getRequest()->getPost());
        if ($signInForm->isValid()) {
            // authentication...
            // $auth->setLogin($signInForm->email)
            // $auth->setPassword($signInForm->password);
            // $result = $auth->authenticate();
            if ($result->isValid()) {
                // some actions
            }
        }
    }
    return new ViewModel([
        'signInForm' => $signInForm,
    ]);
}

Uses scenarios

By default using FormAbstract::DEFAULT_SCENARIO but you can use your customs one:, (*11)

// module/Application/src/Form/SignInForm.php

use Bupy7\Form\FormAbstract;

class SignInForm extends FormAbstract
{
    const SCENARIO_PASSWORD = 2;

    /**
     * @var string
     */
    public $email;
    /**
     * @var string
     */
    public $password;

    protected function scenarios()
    {
        $scenarios = parent::scenarios();
        $scenarios[self::SCENARIO_PASSWORD] = [
            'password',
        ];
        return $scenarios;
    }

    protected function inputs()
    {
        return [
            [
                'name' => 'email',
                'required' => true,
                'validators' => [
                    [
                        'name' => 'EmailAddress',
                    ],
                ],
            ],
            [
                'name' => 'password',
                'required' => true,
                'filters' => [
                    [
                        'name' => 'StringTrim',
                    ]
                ],
            ],
        ];
    }
}

Controller:, (*12)

// DEFAULT scenario
$signInForm = new SignInForm;
$signInForm->email = 'test@gmail.com';
$signInForm->password = '12q34e56t78';
if ($signInForm->isValid()) {
    // do something
}

// or PASSWORD scenario
$signInForm = new SignInForm;
$signInForm->setScenario(SignInForm::SCENARIO_PASSWORD);
$signInForm->password = '12q34e56t78';
if ($signInForm->isValid()) {
    // do something
}

Configuration for ZF-Commons/ZfcTwig

// config/autoload/twig.global.php

use Bupy7\Form\View\Helper\FormBuilderHelper;
use Bupy7\Form\View\Helper\FormBuilderHelperFactory;

return [
    'zfctwig' => [
        'helper_manager' => [
            'factories' => [
                FormBuilderHelper::class => FormBuilderHelperFactory::class,
            ],
            'shared' => [
                'formBuilder' => false,
                FormBuilderHelper::class => false,
            ],
            'aliases' => [
                'formBuilder' => FormBuilderHelper::class,
            ],
        ],
    ],
];

License

zf-form is released under the BSD 3-Clause License., (*13)

The Versions