PHPackages                             priteshyadav444/validation - 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. priteshyadav444/validation

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

priteshyadav444/validation
==========================

The library is inspired by Laravel validation and provides limited but similar functionality for form validation in Core PHP.

v1.0.0(3y ago)28[6 issues](https://github.com/priteshyadav444/validation/issues)MITPHPPHP &gt;=8.0

Since Feb 20Pushed 3y ago1 watchersCompare

[ Source](https://github.com/priteshyadav444/validation)[ Packagist](https://packagist.org/packages/priteshyadav444/validation)[ RSS](/packages/priteshyadav444-validation/feed)WikiDiscussions main Synced today

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

Php Validation Library
======================

[](#php-validation-library)

The library is inspired by Laravel validation and provides limited but similar functionality for form validation. It allows developers to define validation rules in a simple, human-readable format (e.g. "required|numeric") and quickly validate user input. This helps to ensure that the data entered into forms is accurate, consistent, and meets certain standards, improving the overall user experience and reducing the risk of errors in the application.

This library provides classes for validating different types of data inputs in PHP. It includes the following classes:

- `Validate`: contains functions for validating individual inputs, such as strings, numbers, and passwords.
- `FormValidator`: uses the Validate class to validate form inputs and return errors.
- `FileValidator`: it validate and uploading files. it takes in a $\_FILES object and a key to identify the file, and a target directory to upload the file.
- `ErrorHandler`: returns error messages and it methods to handle errors for each particular form input.

Features
--------

[](#features)

- Easy to Use
- API like Laravel
- File handling
- Validation
- Error handling

Requirements
------------

[](#requirements)

- PHP 7.0 or above.

Usage - How to use in core Php Form Validation
----------------------------------------------

[](#usage---how-to-use-in-core-php-form-validation)

### FormValidator Class

[](#formvalidator-class)

The FormValidator class uses the Validate class to validate form inputs and return errors using Error Class. How to Use :

1. Include the FormValidator class in your code by using require or include statement.
2. Create an instance of the FormValidator class and store it in a variable.
3. Call the validate method on the instance of the FormValidator class and pass the form data and validation rules to it as arguments.
4. The validate method will validate the form data based on the rules and return true if the data is valid, and false otherwise.
5. If the validate method returns false, you can access the error messages using the errors method on the instance of the FormValidator class.

    ```
    $path = "../../validation/validators/FormValidator.php";
    indlude "$path";
    use Validators\{FormValidator, FileValidator};
    $obj = new FormValidator();

    if (isset($_POST['submit'])) {
    $validations = [
        'name' => 'string',
        'email' => 'email',
        'phone' => 'numeric|min:10|max:10',
        'docs' => 'required|filetype:txt'
    ];

    if (!$obj->validate($_POST, $validations)->isError()) {
        $file = new FileValidator($_FILES, "docs", "files");
        $file->upload();
        }
    }

    ```

FormValidator class uses `ErrorHandler` Class to Handle Error : Using isErro() method and all() we can list errors :

```
if ($obj->isError() != false) {
        echo '';
        foreach ($obj->all() as $error)
            echo "$error";
        echo '';
}

```

To populate old value which was valid we can use old() method.

```

  Name
