bupy7 / zf-form
This package is abandoned and no longer maintained.
No replacement package was suggested.
The simplest form for Laminas.
2.0.0
2020-11-28 13:21 UTC
Requires
- php: ^5.6 || ^7.0
- bupy7/php-html-form: ~1.0
- laminas/laminas-inputfilter: ^2.7.2
- laminas/laminas-modulemanager: ^2.7.2
- laminas/laminas-servicemanager: ^3.1
- laminas/laminas-validator: ^2.8
- laminas/laminas-view: ^2.8
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16.1
- laminas/laminas-test: ^3.0.1
- phpunit/phpunit: >=5.5 <6.0.0
- satooshi/php-coveralls: ^1.0.1
README
Simple and powerful form with laminas/laminas-inputfilter
and bupy7/php-html-form
.
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 withphp-input-filter
.
Installation
The preferred way to install this extension is through composer.
Either run
$ php composer.phar require bupy7/zf-form "*"
or add
"bupy7/zf-form": "*"
to the require section of your composer.json file.
Usage
Form:
// 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:
// module/Application/view/auth/signin.phtml <?php $formBuilder = $this->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:
// 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:
// 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:
// 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, ], ], ], ];
Links
License
zf-form is released under the BSD 3-Clause License.