folded/request

Request utilities, including a request validator, for your PHP web app.

v0.3.3 2020-10-03 11:25 UTC

This package is auto-updated.

Last update: 2024-04-17 15:43:28 UTC


README

Request utilities, including a request validator, for your PHP web app.

Packagist License Packagist PHP Version Support Packagist Version Build Status Maintainability TODOs

Summary

About

I need a way to pull a simple library to validate my request data when a form is submited. This library hopefuly solves this by faciliting the set up to be ready to validate request values.

Folded is a constellation of packages to help you setting up a web app easily, using ready to plug in packages.

Features

  • Can get GET or POST request data
  • Can check if a key is present in GET or POST data
  • Can validate request (using Laravel's validation request utilities)
  • Can check if the request validation succeeded
  • Can get the (translated) errors when the request validation failed

Requirements

  • PHP version >= 7.4.0
  • Composer installed

Installation

1. Install the package

In your root folder directory, run this command:

composer require folded/request

2. Set up the library

If you want to use the request validation functions, use this code before validating your form data:

use function Folded\setRequestValidationTranslationFolderPath;

setRequestValidationTranslationFolderPath(__DIR__ . "/lang");

The lang folder should contain an arborescence made of folders named with the lang (like "en", "fr", "es", ...), which themselves contain a file named validation.php containing the translation of the validation error message.

As this library relies on Laravel's validation system, the validation errors messages are pulled from these files.

Here is the content of en/validation.php for example. You can start from this file to create translation for other languages.

Examples

1. Get a request value by its key

In this example, we will get the value of a request key after a post request has been submitted.

use function Folded\getRequestValue;

echo getRequestValue("name");

2. Check if a request key is present

In this example, we will check first if a key is present before echoing its content.

use function Folded\getRequestValue;
use function Folded\hasRequestValue;

if (hasRequestValue("name")) {
  echo getRequestValue("name");
} else {
  echo "Request does not have key name.";
}

3. Get all the request values

In this example, we will get all the request value key-pairs in an array.

use function Folded\getAllRequestValues;

$values = getAllRequestValues();

foreach ($values as $name => $value) {
  echo "Post value for $name is $value";
}

4. Validate a request

In this example, we will validate form request data.

use function Folded\validateRequest;

validateRequest([
  "name" => "required|filled",
]);

For more information about the available validation, check Laravel's validation documentation, as this library relies on this system.

5. Check if a request validation succeeded

In this example, we will check if a request succeeded after validating it.

use function Folded\validateRequest;
use function Folded\requestValidationSucceeded;

validateRequest([
  "name" => "required|filled",
]);

if (requestValidationSucceeded()) {
  echo "it worked!";
} else {
  echo "some errors occured...";
}

6. Get error messages after a request validation

In this example, we will get the errors messages of a failed request validation.

use function Folded\validateRequest;
use function Folded\requestValidationSucceeded;
use function Folded\getRequestValidationErrors;

validateRequest([
  "email" => "required|filled",
]);

if (!requestValidationSucceeded()) {
  $errors = getRequestValidationErrors();

  foreach ($errors as $error) {
    echo "error: {$error->message()}";
  }
}

For more information about how to manipulate the errors, see Laravel's validation error documentation, as this library relies on this system.

7. Choosing the validation error language before validating request

In this example, before validating the request, we will change the language for the validation erorrs. Note that you should do it ideally right after the code from the Installation section: 2. Set up the library section.

use function Folded\setRequestValidationLang;
use function Folded\setRequestValidationTranslationFolderPath;

setRequestValidationTranslationFolderPath(__DIR__ . "/lang");
setRequestValidationLang("fr");

8. Get an old submited form value

In this example, we will get a previously submitted form value. This is convenient if you want to display values the user used before an error occured (in order for the user to not loose everything he wrote).

use function Folded\storeOldRequestValues;
use function Folded\getOldRequestValue;

// Session must be enabled to make it work
session_start();

// As soon as possible
storeOldRequestValues();

// Get the old form values, assuming you previously listened a POST request with an email key
echo getOldRequestValue("email");

If the key is not found, the getOldRequestValue() function returns null.

Version support

7.3 7.4 8.0
v0.1.0 ✔️
v0.2.0 ✔️
v0.3.0 ✔️
v0.3.1 ✔️
v0.3.2 ✔️
v0.3.3 ✔️