germania-kg / formvalidator
Callable for validating and filtering user inputs with convenient evaluation API.
Requires
- php: ^5.6|^7.0|^8.0
- psr/container: ^1.0
- psr/http-message: ^1.0
- psr/log: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.0
- php-coveralls/php-coveralls: ^2.0
- phpspec/prophecy-phpunit: ^2.0
- phpstan/phpstan: ^0.12
- phpunit/phpunit: ^9.0
- spatie/phpunit-watcher: ^1.0
README
Callable for validating and filtering user inputs with convenient evaluation API.
Installation with Composer
$ composer require germania-kg/formvalidator
Form validation
The validation is done by invoking the object. The __invoke
method accepts both Arrays and Psr\Http\Message\ServerRequestInterface
instances.
<?php use Germania\FormValidator\FormValidator; use Germania\FormValidator\InputContainer; // Setup $required = [ "send_button" => FILTER_VALIDATE_BOOLEAN, "user_email" => FILTER_VALIDATE_EMAIL, "user_login_name" => FILTER_SANITIZE_STRING ]; $optional = [ "family_name" => FILTER_SANITIZE_STRING, "first_name" => FILTER_SANITIZE_STRING ]; $formtest = new FormValidator( $required, $optional ); // Invoking uses PHP's filter_var_array internally. // Arrays accepted but Psr\Http\Message\ServerRequestInterface // will do as well. // Return value is InputContainer instance: $filtered_input = $formtest( $_POST ); $filtered_input = $formtest( $request ); // At least one required field valid? echo $formtest->isSubmitted(); // All required fields valid? echo $formtest->isValid();
Adding fields
After instantiation, you can add required or optional fields. An existing optional field is no longer optional if added using addRequired
, the same goes with required fields, if added using addOptional
.
$formtest = new FormValidator( $required, $optional ); $formtest->addRequired('additional_info', FILTER_SANITIZE_STRING); $formtest->addOptional('additional_info', FILTER_SANITIZE_STRING);
Removing fields
After instantiation, you can remove required or optional fields.
$formtest = new FormValidator( $required, $optional ); $formtest->removeRequired('user_email'); $formtest->removeOptional('family_name');
Filtered Result: InputContainer
The InputContainer is a PSR-11 Container and also implements ArrayAccess.
ArrayAccess
<?php // Invocation returns InputContainer instance $filtered_input = $formtest( $_POST ); // ArrayAccess: // If field not set, return values are null. echo $filtered_input['foo']; echo $filtered_input->offsetGet('foo');
Simple Array access
<?php // Invocation returns InputContainer instance $filtered_input = $formtest( $_POST ); $input_array = $filtered_input->getArrayCopy();
ContainerInterface
<?php use Germania\FormValidator\NotFoundException; use Psr\Container\NotFoundExceptionInterface; // Invocation returns InputContainer instance $filtered_input = $formtest( $_POST ); try { echo $filtered_input->has('foo'); echo $filtered_input->get('foo'); } catch (NotFoundException $e) { // not found } catch (NotFoundExceptionInterface $e) { // not found }
Filtered Result: Custom InputContainer
The FormValidator class optionally accepts a Callable that takes the filtered input. It should return something useful (such as the default InputContainer).
Variant A: Using the constructor
// Setup the factory $factory = function( $filtered_input ) { return new \ArrayObject( $filtered_input ); }; // Pass to the ctor $formtest = new FormValidator( $required, $optional, $factory ); // Returns an ArrayObject $filtered_input = $formtest( $_POST );
Variant B: Use per call
// Setup as usual: $formtest = new FormValidator( $required, $optional ); $filtered_input = $formtest( $_POST ); // While the above returns the usual InputContainer, // this will return an ArrayObject: $filtered_input = $formtest( $_POST, function( $filtered_input ) { return new \ArrayObject( $filtered_input ); });
Issues
See issues list.
Development
$ git clone https://github.com/GermaniaKG/FormValidator.git
$ cd FormValidator
$ composer install
Unit tests
Either copy phpunit.xml.dist
to phpunit.xml
and adapt to your needs, or leave as is. Run PhpUnit test or composer scripts like this:
$ composer test # or $ vendor/bin/phpunit