germania-kg/formvalidator

Callable for validating and filtering user inputs with convenient evaluation API.

1.5.1 2022-03-30 09:42 UTC

This package is auto-updated.

Last update: 2024-10-29 04:20:08 UTC


README

Callable for validating and filtering user inputs with convenient evaluation API.

Packagist PHP version Build Status Scrutinizer Code Quality Code Coverage Build Status

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