eredi93 / forms
Form generation and validation
0.3.0
2016-06-07 13:56 UTC
Requires
- php: >=5.5
Requires (Dev)
- phpunit/phpunit: ~4.5
- squizlabs/php_codesniffer: 2.1.*
This package is not auto-updated.
Last update: 2025-01-18 20:35:36 UTC
README
Easy to use & highly customisable PHP package for Form generation and data validation. Supports PSR-7 HTTP-Message.
- Generate HTML forms
- Validate data
- PSR-7 HTTP-Message
Note: This package has been developed under Slim3 and is the only Framework supported at the moment.
Installing
Install using Composer.
{ "require": { "eredi93/forms": "0.2.*" } }
Basic usage (Slim 3)
Extend basic Form and within the setUp method set the form fields
use Forms\From; use Forms\Validators\Required; class LoginForm extends Form { protected function setUp() { $this->fields = [ (new TagOpen(['class' => "wrapper"])), (new TextInput("identifier")) ->setLabel("User / Email") ->setAutocomplete("off") ->setDecoratorClass("input-field col s12") ->setValidators([ new Required(), ]), (new PasswordInput("password")) ->setLabel("Password") ->setDecoratorClass("input-field col s12") ->setValidators([ new Required(), ]), (new SubmitButton()) ->setDecoratorClass("input-field col s12") ->setClass("btn yellow right") ->setText("Login"), (new TagClose(), ]; } }
now that you have your form created instantiate it in the GET controller
public function login(Request $request, Response $response, $args) { // Instantiate Form $form = new LoginForm(); /** * You can pass to the form the method action and the CSRF name * by default the form uses * - $action="" * - $method="POST" * - $csrf=['csrf_name', 'csrf_value'] */ // Render Form echo $form->render($request); return $response; }
to validate the form in POST controller call the validate method
public function loginPost(Request $request, Response $response, $args) { // Instantiate Form $form = new LoginForm(); // Check validation if ($form->validate($request)) { /* * Form validation passed log in the user */ } // Render Form with validation echo $form->render($request); return $response; }
Contributing
Please file issues under GitHub, or submit a pull request if you'd like to directly contribute.
Running tests
Tests are run with phpunit. Run ./vendor/bin/phpunit to run tests.