PHPackages                             tamedevelopers/validator - 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. tamedevelopers/validator

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

tamedevelopers/validator
========================

PHP Form Validator is a small no-depencies library for PHP codes.

5.0.8(7mo ago)411211MITPHPPHP &gt;=8.0CI passing

Since Jun 27Pushed 7mo ago1 watchersCompare

[ Source](https://github.com/tamedevelopers/validator)[ Packagist](https://packagist.org/packages/tamedevelopers/validator)[ Docs](https://github.com/tamedevelopers/validator)[ RSS](/packages/tamedevelopers-validator/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (1)Versions (49)Used By (1)

PHP Form Validator - PFV
========================

[](#php-form-validator----pfv)

[![Total Downloads](https://camo.githubusercontent.com/f216b29962d86ab8db962891a97f3c4ba00448061a8719ced6c7e51980c46283/68747470733a2f2f706f7365722e707567782e6f72672f74616d65646576656c6f706572732f76616c696461746f722f646f776e6c6f616473)](https://packagist.org/packages/tamedevelopers/validator)[![Latest Stable Version](https://camo.githubusercontent.com/09848b1ae7aaec9f56d0e5772f3259f307660c9b0d40a98a30884d9474200bef/68747470733a2f2f706f7365722e707567782e6f72672f74616d65646576656c6f706572732f76616c696461746f722f76657273696f6e)](https://packagist.org/packages/tamedevelopers/validator)[![License](https://camo.githubusercontent.com/78ebe5764e70098517d63ec26bb0247e166b5f06f7bd88426fc365e829a9feee/68747470733a2f2f706f7365722e707567782e6f72672f74616d65646576656c6f706572732f76616c696461746f722f6c6963656e7365)](https://packagist.org/packages/tamedevelopers/validator)[![Code Coverage](https://camo.githubusercontent.com/c9c43736b1c81a1b9eee096d552ef4663abdace06b2c597222cbfe9e3aa437b2/68747470733a2f2f636f6465636f762e696f2f6769746875622f74616d65646576656c6f706572732f76616c696461746f722f67726170682f62616467652e7376673f746f6b656e3d3356434748515a483433)](https://codecov.io/github/tamedevelopers/validator)

Documentation
-------------

[](#documentation)

- [Requirements](#requirements)
- [Installation](#installation)
- [Instantiate](#instantiate)
- [Laravel Support](#laravel-support)
- [Methods That Should Always Come First](#methods-that-should-always-come-first)
- [Global Configuration](#methods-that-should-always-come-first)
- [Csrf](#csrf)
    - [Csrf Form Input](#csrf-form-input)
    - [Csrf Token](#csrf-token)
- [Usage](#usage)
    - [Error Type](#error-type)
    - [Token](#token)
    - [POST](#post)
    - [GET](#get)
    - [ALL](#all)
    - [ANY](#any)
    - [Rules](#rules)
    - [Validate](#validate)
    - [Save](#save)
    - [Data Types](#data-types)
    - [Operators](#operators)
    - [noInterface](#nointerface)
    - [Before](#before)
    - [After](#after)
    - [Has Error](#has-error)
    - [Is Validated](#is-validated)
- [Reset Error](#reset-error)
- [Only](#only)
- [Except](#except)
- [Has](#has)
- [Old](#old)
- [Merge](#merge)
- [Only Data](#only-data)
- [Except Data](#except-data)
- [GetForm](#getForm)
- [Get Message and Class](#get-message-and-class)
- [Collection](#collection)
- [Collection Methods](#collection-methods)
- [toObject](#toobject)
- [toArray](#toarray)
- [toJson](#tojson)
- [Helpers](#helpers)
- [Useful links](#useful-links)

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

[](#requirements)

- `>= php 8.0+`

Installation
------------

[](#installation)

Prior to installing `validator package` get the [Composer](https://getcomposer.org) dependency manager for PHP because it'll simplify installation.

```
composer require tamedevelopers/validator
```

Instantiate — `Instantiate class using`
---------------------------------------

[](#instantiate--instantiate-class-using)

- It's helper class can be called, using -- `form()`

```
require_once __DIR__ . '/vendor/autoload.php';

use \Tamedevelopers\Validator\Validator;

$form = new Validator();
```

Laravel Support
---------------

[](#laravel-support)

- Now supports Laravel and with same Functionalities no different
    - `use Tamedevelopers\Validator\Validator;`

```
public function save(Request $request){

    $form = new Validator();
    or
    $form = form();
}
```

Methods That Should Always Come First
-------------------------------------

[](#methods-that-should-always-come-first)

- All are Optional `method`
    - These methods are only mandatory on usage and should always come first before others.

MethodsDescription-&gt;errorType()`bool` to format error's on display: `single or multiple`-&gt;token()`bool` to Enable or Disable `csrf_token` for each request-&gt;post()Convert Form request to `POST` only-&gt;get()Convert Form request to `GET` only-&gt;all()Convert Form request to `any````
$form->post()->rules([
    //
]);
```

Global Configuration
--------------------

[](#global-configuration)

- It's helper class can be called, using -- `config_form()`

KeysDescriptionrequestString `POST|GET|ALL` Default `POST`error\_typeBoolean `true|false` Default `false`csrf\_tokenBoolean `true|false` Default `true`classAssoc Array `error|success` error class type to be returned on both success and failure```
config_form(
    request       : 'POST',
    error_type    : true,
    csrf_token    : true,
    class         : [
        'error'     => 'alert alert-danger',
        'success'   => 'alert alert-success'
    ]
);
```

Csrf
----

[](#csrf)

- Implementing `Csrf` (Cross-Site Request Forgery)
    - By default the form requires all request to have a token attached.
        - You can disable the usage with the `config_form()` Helper

### Csrf Form Input

[](#csrf-form-input)

- This will create html input element with valid `csrf token`
    - It's a function and you don't need to `echo`
        - Use anywhere inside your HTML form

```
csrf();

```

[![Sample Csrf Form Input](https://raw.githubusercontent.com/tamedevelopers/validator/main/getErrorMessage.png)](https://raw.githubusercontent.com/tamedevelopers/validator/main/getErrorMessage.png)

### Csrf Token

[](#csrf-token)

- This will return the `csrf token` string

```
csrf_token();
```

USAGE
-----

[](#usage)

- All Methods of usage

### Error Type

[](#error-type)

- Takes a param as `bool` Default is `false`
    - You can call separately or Call Before any other method, if intend to use.

ErrorDescriptionfalse`Default` Errors displayed one after anothertrueThis allow all errors to be displayed once, `as an array````
$form->errorType(false);
```

### Token

[](#token)

- Takes a param as `bool` Default is `false`
    - Allow disability of `csrf_token` on each form request

ErrorDescriptionfalse`Default` Will disable `csrf_token` usagetrueThis allow `csrf_token` per request only```
$form->token(false);
```

### POST

[](#post)

- Set the Form Request to `POST`
    - This will always override the `config_form()` settings

```
$form->post();
```

### GET

[](#get)

- Set the Form Request to `GET`

```
$form->get();
```

### All

[](#all)

- Will automatically detect if request type is `GET\|POST` and get it's data.

```
$form->all()->rules([
    //
])
```

### Any

[](#any)

- same as `all`

```
$form->any()->rules([
    //
])
```

### Rules

[](#rules)

- By default only `DATA TYPE` and `[INPUT_NAME]` is required
    - Always seperate each indicator with a 'colon' `:` or 'pipe' `|`

DATA TYPEINPUT\_NAMECOMPARISON OPERATORVALUE TO COMPAREstring: country: ==: 0email: email:```
$form->rules([
    "string|country|==|0"   => 'Please Select a Country',
    "email:email"           => 'Please enter a valid email address',
])
```

- HTML FORM Structure

```

        Select Country
        Nigeria
        United States of America

```

### Validate

[](#validate)

- Takes an \[optional\] `closure` function as the param

```
$form->rules([
    "s:name" => 'Please enter a name',
])->validate(function($response){

    $response->param; //Collection of form data
    $response->getMessage(); //message property
});
```

### Save

[](#save)

- Expects a `closure` function as the param
    - Message property will be empty string on success `$response->message`

```
$form->rules([
    "s:name" => 'Please enter a name',
])->save(function(){
    //on success
});
```

### Data Types

[](#data-types)

- `Supports 9 Data Flags type`

Data typesabbrDescriptionemaile`Email` data validationboolb`Boolean` data validationstrings`String` data validationhtml-`html` CMS/Blog content validationdev-`dev` CMS/Blog/Dev like content validationraw-`raw` Collect content without validationstr\_lensl`String Length` validationenumen`Enum` Forms `checkbox | radio` or any form data that normally has no value when not checkedarraya`Array` data validationfloatf`Float` data validationinti`Int` data validationurlu`Url` data validation### Operators

[](#operators)

- `Supports 10 operational statement`

signDescription==Equal to===Strictly Equal to!=Not Equal To!==Not Strictly Equal To&gt;Greater than&gt;=Greater than or Equal to&lt;Less than&lt;=Less than or Equal to&lt; or &gt;Less than or Greater than&lt; and &gt;Less than and Greater than### noInterface

[](#nointerface)

- Expects a `closure` function as the param
    - have access to form data without any validation

```
$form->noInterface(function($response){

    if($response->has('amount')){
        // exec code
    }
});
```

### Before

[](#before)

- Expects a `closure` function as the param
    - Will only execute code within when Request is \[GET\]
        - CSRF Token `does'nt` apply to this method

```
$form->rules([
    "s:name" => 'Please enter a name',
])->before(function($response){

    // execute code
});

```

### After

[](#after)

- Expects a `closure` function as the param
    - Will always execute no matter the request method type
        - CSRF Token `does'nt` apply to this method

```
$form->after(function(){
    // execute code
});

```

### Has Error

[](#has-error)

- Returns bool `true\|false`

```
$form->hasError();

```

### Is Validated

[](#is-validated)

- Returns bool `true\|false`, When Form has already been validated

```
$form->isValidated();

```

Reset Error
-----------

[](#reset-error)

- Even if you're inside the `success() method`
- With this helper, you can be able to reset the class, to error class

```
->save(function($response){

    $availableUserAmount = 900;

    if($response->amount > $availableUserAmount){
        $response->reset();
        $response->message = "Your wallet balance is too low, Please recharge before you can Subscribe to Plan!";
        return;
    }

    // perform other request before
});

```

Only
----

[](#only)

- Takes a param as an `array`
    - `keys` of data only needed, from the `form param`

```
->save(function($response){
    //
    $data = $response->only(['password', 'username']);
});

```

Except
------

[](#except)

- Exact opposite of `only()` method

```
->save(function($response){

    $data = $response->except(['_token']);
});

```

Has
---

[](#has)

- Takes a param as `string` input name
    - Returns boolean as `true|\false`

```
->save(function($response){

    if($response->has('remeber_me')){
        // execute code
    }
});

```

Old
---

[](#old)

- Takes a param as `string` and return old inserted data
    - Second parameter is \[optional\] `mixed data`.
    - It's helper class can be called, using -- `old()`

```
$form->rules([
    "s:password" => 'Please enter a name',
    "s:retype_pass:!==:{$form->old('password')}" => 'Password mismatch, Please enter same password',
]);
```

- or

```
