jessesoeteman/validate-get-post-input

这个PHP类允许您验证POST或GET输入。

v1.3.1 2023-01-23 05:23 UTC

This package is auto-updated.

Last update: 2024-09-26 12:13:29 UTC


README

这个PHP库可以帮助您验证GET和POST请求的输入。
它还可以帮助您清理输入。

需求

使用此库需要以下要求

  • PHP 8.0或更高版本
  • Composer

安装

要安装此库,请在您的终端中运行以下命令

composer require jessesoeteman/validate-get-post-input

确保您的系统已安装Composer。并且将自动加载器包含在您的代码中。

require __DIR__ . '/vendor/autoload.php';

如何使用ValidateGetPostInput?

基本用法

以下是一些使用预构建类的示例,这些类使库的使用更加容易。
预构建类包括以下内容:

一般信息

除了ValidateVarchar255Regex类之外,预构建类将以下参数作为构造函数的第一个参数

  • 输入名称,如果您想验证$_GET["username"],则使用"username"作为第一个参数。
  • 请求类型,默认为RequestType::GET,您可以使用RequestType::POST来验证$_POST["username"]。
  • 如果输入是必需的,默认为true。

ValidateBoolean

use ValidateGetPostInput\Prebuilt\ValidateBoolean;

$_boolean = new ValidateBoolean("boolean"); // The name of the input, so $_GET["boolean"] or $_POST["boolean"]
$errors += $_boolean->validate(); // This returns an array with errors if there are any, returns an empty array if there are no errors.
$boolean = $_boolean->getValue(); // This returns the value of the input.

如果您不想将参数设置为必需,可以将第二个参数设置为false。

use ValidateGetPostInput\Prebuilt\ValidateBoolean;

$_boolean = new ValidateBoolean("boolean", false);
$errors += $_boolean->validate(); 
$boolean = $_boolean->getValue();

ValidateNumber

use ValidateGetPostInput\Prebuilt\ValidateNumber;

$_number = new ValidateNumber("number"); // The name of the input, so $_GET["number"] or $_POST["number"]
$errors += $_number->validate(); // This returns an array with errors if there are any, returns an empty array if there are no errors.
$number = $_number->getValue(); // This returns the value of the input.

ValidateFloat

use ValidateGetPostInput\Prebuilt\ValidateFloat;

$_float = new ValidateFloat("float"); // The name of the input, so $_GET["float"] or $_POST["float"]
$errors += $_float->validate(); // This returns an array with errors if there are any, returns an empty array if there are no errors.
$float = $_float->getValue(); // This returns the value of the input.

ValidateText

use ValidateGetPostInput\Prebuilt\ValidateText;

$_text = new ValidateText("text"); // The name of the input, so $_GET["text"] or $_POST["text"]
$errors += $_text->validate(); // This returns an array with errors if there are any, returns an empty array if there are no errors.
$text = $_text->getValue(); // This returns the value of the input.

ValidateJSON

use ValidateGetPostInput\Prebuilt\ValidateJSON;

$_json = new ValidateJSON("json"); // The name of the input, so $_GET["json"] or $_POST["json"]
$errors += $_json->validate(); // This returns an array with errors if there are any, returns an empty array if there are no errors.
$json = $_json->getValue(); // This returns the value of the input.

ValidateID

id的最小值是1,最大值是2147483647。

use ValidateGetPostInput\Prebuilt\ValidateID;

$_id = new ValidateID("id"); // The name of the input, so $_GET["id"] or $_POST["id"]
$errors += $_id->validate(); // This returns an array with errors if there are any, returns an empty array if there are no errors.
$id = $_id->getValue(); // This returns the value of the input.

ValidateEmail

use ValidateGetPostInput\Prebuilt\ValidateEmail;

$_email = new ValidateEmail("email"); // The name of the input, so $_GET["email"] or $_POST["email"]
$errors[] = $_email->validate(); // This returns an array with errors if there are any, returns an empty array if there are no errors.
$email = $_email->getValue(); // This returns the value of the input.

ValidateVarchar255

use ValidateGetPostInput\Prebuilt\ValidateVarchar255;

$_varchar255 = new ValidateVarchar255("varchar255"); // The name of the input, so $_GET["varchar255"] or $_POST["varchar255"]
$errors += $_varchar255->validate(); // This returns an array with errors if there are any, returns an empty array if there are no errors.
$varchar255 = $_varchar255->getValue(); // This returns the value of the input.

ValidateVarchar255Regex

use ValidateGetPostInput\Prebuilt\ValidateVarchar255Regex;

$_varchar255Regex = new ValidateVarchar255Regex("varchar255Regex", "/^[a-zA-Z0-9]+$/"); // The name of the input, so $_GET["varchar255Regex"] or $_POST["varchar255Regex"], the second parameter is the regex, the third parameter is if the input is required, default is true.
$errors += $_varchar255Regex->validate(); // This returns an array with errors if there are any, returns an empty array if there are no errors.
$varchar255Regex = $_varchar255Regex->getValue(); // This returns the value of the input.

ValidateDate

默认日期格式是'Y-m-d H:i:s',但您可以使用第四个参数进行更改。

use ValidateGetPostInput\Prebuilt\ValidateDate;
use ValidateGetPostInput\Statics\DateFormat;

$_date = new ValidateDate("date", true, DateFormat::YYYY_MM_DD); // The name of the input, so $_GET["date"] or $_POST["date"], the second parameter is if the input is required, default is true. The third parameter is the date format, default is 'Y-m-d H:i:s'.
$errors += $_date->validate(); // This returns an array with errors if there are any, returns an empty array if there are no errors.
$date = $_date->getValue(); // This returns the value of the input.

如何使用ValidateMultiple?

ValidateMultiple允许您一次验证多个输入。

use ValidateGetPostInput\ValidateMultiple;

use ValidateGetPostInput\Prebuilt\ValidateID;
use ValidateGetPostInput\Prebuilt\ValidateVarchar255;
use ValidateGetPostInput\Prebuilt\ValidateText;

请确保使用try-catch块,因为如果有错误,它将抛出异常。

try {
    $validations = new ValidateMultiple([
        new ValidateID("id"),
        new ValidateVarchar255("name"),
        new ValidateText("description"),
        new ValidateVarchar255("link")
    ]);
} catch (Exception $e) {
    $erros += $e->getMessage();
    // Do something with the errors
    exit();
}
$error += $validations->validate();
// Check if the validation faid
if (count($errors) > 0) {
    // Do something with the errors
    exit();
}
// Get the validated data as an key value array
$inputData = $validations->getValues();

许可证

本项目采用MIT许可证 - 有关详细信息,请参阅LICENSE文件