omerucel / form
There is no license information available for the latest version (1.0.0) of this package.
1.0.0
2017-07-13 13:47 UTC
Requires
- respect/validation: 1.1.12
Requires (Dev)
- phpunit/phpunit: 6.2.3
This package is not auto-updated.
Last update: 2024-12-28 16:59:06 UTC
README
About
This library provides clean and an easy solution for form validation process. It's using respect/validation package for validation part.
Composer
{ "require": { "omerucel/form": "1.0.0" } }
Usage
There's two options to use. First way is to extend OU\Form\Form class and it is clean way to use this library.
<?php namespace { use OU\Form\Field; use OU\Form\Message\DangerMessage; use OU\Form\Message\SuccessMessage; use Respect\Validation\Rules; use Symfony\Component\HttpFoundation\Request; $request = Request::createFromGlobals(); $form = new RegistrationForm($request); if ($form->validate()) { // Complete form action $form->setCompleted(true); $form->addMessage(new SuccessMessage('User created.')); } else { $form->addMessage(new DangerMessage('Please try again.')); } class RegistrationForm extends OU\Form\Form { public $email; public $password; public function __construct(Request $request) { $this->email = new Field($request->get('email')); $this->email->addRule(new Rules\Email(), new DangerMessage('Invalid email address.')); $this->password = new Field($request->get('password')); $this->password->addRule(new Rules\NotEmpty(), new DangerMessage('Password is empty.')); $this->password->addRule(new Rules\Length(8), new DangerMessage('Password is too short.')); } } }
Another way is using OU\Form\Form class directly.
<?php namespace { use OU\Form\Form; use OU\Form\Field; use OU\Form\Message\DangerMessage; use OU\Form\Message\SuccessMessage; use Respect\Validation\Rules; use Symfony\Component\HttpFoundation\Request; $request = Request::createFromGlobals(); $form = new Form(); $form->email = new Field($request->get('email')); $form->email->addRule(new Rules\Email(), new DangerMessage('Invalid email address.')); $form->password = new Field($request->get('password')); $form->password->addRule(new Rules\NotEmpty(), new DangerMessage('Password is empty.')); $form->password->addRule(new Rules\Length(8), new DangerMessage('Password is too short.')); if ($form->validate()) { $form->setCompleted(true); $form->addMessage(new SuccessMessage('User created.')); } else { $form->addMessage(new DangerMessage('Please try again.')); } }
You can extend Respect\Validation\Rules\AbstractRule class or implement Respect\Validation\Validatable interface to create your own validation rule. Please check it's documentation for this.