PHPackages                             mazed/php-validator-sanitizer - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. mazed/php-validator-sanitizer

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

mazed/php-validator-sanitizer
=============================

PHP validation &amp; sanitizer library

1.0.0(2y ago)0277↓93.8%[1 PRs](https://github.com/csemazharul/php-validator-sanitizer/pulls)MITPHPPHP &gt;=5.6

Since Nov 15Pushed 2y ago1 watchersCompare

[ Source](https://github.com/csemazharul/php-validator-sanitizer)[ Packagist](https://packagist.org/packages/mazed/php-validator-sanitizer)[ RSS](/packages/mazed-php-validator-sanitizer/feed)WikiDiscussions main Synced today

READMEChangelog (1)Dependencies (3)Versions (3)Used By (0)

PHP Validator Sanitizer
=======================

[](#php-validator-sanitizer)

Validate and sanitize form inputs and API requests with a PHP library inspired by Laravel, designed specifically for PHP.

[![Latest Stable Version](https://camo.githubusercontent.com/9b94f2b464a9981647e6c596bf6c2c42c0cd97e8c38f335ae6beda3e9907940e/68747470733a2f2f706f7365722e707567782e6f72672f6d617a65642f7068702d76616c696461746f722d73616e6974697a65722f762f737461626c65)](https://packagist.org/packages/mazed/php-validator-sanitizer) [![Total Downloads](https://camo.githubusercontent.com/2a73b1e7824b28433c40651e1d64787dfd7274eb38594706a558527db6c6918b/68747470733a2f2f706f7365722e707567782e6f72672f6d617a65642f7068702d76616c696461746f722d73616e6974697a65722f646f776e6c6f616473)](https://packagist.org/packages/mazed/php-validator-sanitizer) [![License: MIT](https://camo.githubusercontent.com/08cef40a9105b6526ca22088bc514fbfdbc9aac1ddbf8d4e6c750e3a88a44dca/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d626c75652e737667)](https://opensource.org/licenses/MIT)

Overview
--------

[](#overview)

PHP Validator Sanitizer is a comprehensive PHP package inspired by Laravel. It simplifies the process of data validation and sanitization for PHP, providing a versatile and user-friendly solution for developers to ensure that user input meets specific criteria and is secure against common vulnerabilities.

Features
--------

[](#features)

- **Data Validation:** Easily validate user inputs, form submissions, and API requests.
- **Custom Validation Rules:** Define your custom validation rules to meet your application's specific needs.
- **Error Messages:** Detailed error messages to assist users in understanding validation failures.
- **Data Sanitization:** Optional data sanitization functions for cleaning and formatting data.

Example Usage
-------------

[](#example-usage)

To use the php-validator-sanitizer package for data validation in your PHP application, follow these steps:

### 1. Install the Package

[](#1-install-the-package)

Begin by installing the PHP Validator Sanitizer package using Composer:

```
composer require mazed/php-validator-sanitizer
```

### 2. Initialize the Validator

[](#2-initialize-the-validator)

Create an instance of the Validator class from the package:

```
use Mazed\PHPValidator\Validator;

$validator = new Validator;
```

For public methods that can be used with `$validator`, refer to the [Validator Instance Methods](#validator-instance-methods) section.

### 3. Define Your Data and Validation Rules

[](#3-define-your-data-and-validation-rules)

Prepare the data you want to validate and define the validation rules. Here's an example:

```
$data = [
    'first_name' => 'John',
    'last_name' => '',
    'email' => 'email@example',
    'password' => '##112233',
    'confirm_password' => '##112233',
];

$rules = [
    'first_name' => ['required', 'string'],
    'last_name' => ['required', 'string'],
    'email' => ['required', 'email'],
    'password' => ['required', 'min:8'],
    'confirm_password' => ['required', 'min:6', 'same:password'],
];
```

Explore all available validation rules and their usage in the [Available Validation Rules](#available-validation-rules) section.

### 4. Customize Error Messages (Optional)

[](#4-customize-error-messages-optional)

If you need to customize error messages, you can use the `$customMessages` array. In this example, we leave it empty.

```
$customMessages = [];
```

Learn more about customizing error messages in the [Customizing Error Messages](#customizing-error-messages) section.

### 5. Map Attribute Names (Optional)

[](#5-map-attribute-names-optional)

Map your field names to user-friendly labels using the `$attributes` array, these labels will be used for error messages.

```
$attributes = [
    'first_name' => 'First Name',
    'last_name' => 'Last Name',
    'email' => 'Email',
];
```

### 6. Perform Validation

[](#6-perform-validation)

Execute the validation using the `make` method:

```
$validation = $validator->make($data, $rules, $customMessages, $attributes);
```

### 7. Handle Validation Results

[](#7-handle-validation-results)

Check if validation fails and, if so, print out the validation errors:

```
if ($validation->fails()) {
    echo "";
    echo print_r($validation->errors(), true);
    echo "";
} else {
    echo "Success!";
}
```

### Validator Instance Methods

[](#validator-instance-methods)

#### `make($data: array, $rules: array[, $customMessages?: array, $attributes?: array])`

[](#makedata-array-rules-array-custommessages-array-attributes-array)

This method runs the validations of `$data` based on given `$rules`. Optionally, if you pass `$customMessages` and `$attributes`, it will make the error messages (*if any*) based on that.

#### `fails(): boolean`

[](#fails-boolean)

This method will return true or false based on the validation status. If it returns true, that means the validator has found errors in data, and you can get those errors by the `errors()` method.

#### `errors(): array`

[](#errors-array)

This method will return the error messages (*if any*) based on the format of the passed `$data` array in the `make()` method.

### Available Validation Rules

[](#available-validation-rules)

PHP Validator provides a comprehensive set of validation rules to suit your needs. Here's a list of available rules:

1. **`accepted`**
    Checks if the field under validation is one of the following: `'yes'`, `'on'`, `'1'`, `1`, `'true'`, `true`. This is useful for validating agreement type fields.
2. **`array`**
    Checks if the field under validation is an array.
3. **`between:min,max`**
    Checks if the field under validation falls within the range of `:min` and `:max` (inclusive).
    - For string data, the value corresponds to the number of characters.
    - For numeric data, the value corresponds to a given integer value.
    - For an array, the value corresponds to the count of the array.
4. **`date`**
    Checks if the field under validation is a valid date according to the `strtotime` PHP function.
5. **`digit_between:min,max`**
    Checks if the length of digits for the integer number falls within the range of `:min` and `:max` (inclusive).
6. **`digits:value`**
    Checks if the length of digits for the integer number is exactly the same as `:digits`.
7. **`email`**
    Checks if the field under validation is a valid email address.
8. **`integer`**
    Checks if the field under validation is an integer number.
9. **`ip`**
    Checks if the field under validation is a valid IP (IPv4, IPv6) address.
10. **`ipv4`**
    Checks if the field under validation is a valid IPv4 address.
11. **`ipv6`**
    Checks if the field under validation is a valid IPv6 address.
12. **`json`**
    Checks if the field under validation is a valid JSON string.
13. **`lowercase`**
    Checks if the field under validation consists of all lowercase letters.
14. **`mac_address`**
    Checks if the field under validation is a valid MAC address.
15. **`max:value`**
    Checks if the field under validation is less than or equal to `:max`.
    - For string data, the value corresponds to the number of characters.
    - For numeric data, the value corresponds to a given integer value.
    - For an array, the value corresponds to the count of the array.
16. **`min:value`**
    Checks if the field under validation has a minimum value of `:min`.
    - For string data, the value corresponds to the number of characters.
    - For numeric data, the value corresponds to a given integer value.
    - For an array, the value corresponds to the count of the array.
17. **`nullable`**
    Makes the field under validation as optional (allows to be null), but respects other validation rules if specified and value is not null.
18. **`numeric`**
    Checks if the field under validation is a valid real number.
19. **`required`**
    Checks if the field under validation is present and not empty. A field is "empty" if it meets one of the following criteria:
    - The value is `NULL` or `FALSE`.
    - The value is an empty string.
    - The value is an empty array or empty countable object.
20. **`same:field`**
    Checks if the field under validation is equal to the specified `:other` attribute.
21. **`size:value`**
    Checks if the field under validation has exactly the same size as `:size`.
    - For string data, the value corresponds to the number of characters.
    - For numeric data, the value corresponds to a given integer value.
    - For an array, the value corresponds to the count of the array.
22. **`string`**
    Checks if the given value is a string.
23. **`uppercase`**
    Checks if the string value consists of all uppercase letters.
24. **`url`**
    Checks if the value is a valid URL.

Missing any validation rule that you need? Refer to the [Custom Validation Rule](#custom-validation-rule) section to know how you can create and use custom validation rules in your project alongside the library.

### Custom Validation Rule

[](#custom-validation-rule)

Create the class of the validation rule into your project:

```
