PHP code example of bupy7 / zf-form
1. Go to this page and download the library: Download bupy7/zf-form library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
bupy7 / zf-form example snippets
// 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',
'
// module/Application/view/auth/signin.phtml
$formBuilder = $this->formBuilder($this->signInForm);
// 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,
]);
}
// 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',
'
// DEFAULT scenario
$signInForm = new SignInForm;
$signInForm->email = '[email protected] ';
$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
}
// 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,
],
],
],
];
$ php composer.phar