- Requirements
- Installation
- Instantiate
- Laravel Support
- Methods That Should Always Come First
- Global Configuration
- Csrf
- Usage
- Reset Error
- Only
- Except
- Has
- Old
- Merge
- Only Data
- Except Data
- GetForm
- Get Message and Class
- Collection
- Collection Methods
- toObject
- toArray
- toJson
- Helpers
- Useful links
- >= php 8.0+
Prior to installing validator package get the Composer dependency manager for PHP because it'll simplify installation.
composer require tamedevelopers/validator- It's helper class can be called, using -- form()
require_once __DIR__ . '/vendor/autoload.php';
use \Tamedevelopers\Validator\Validator;
$form = new Validator();- Now supports Laravel and with same Functionalities no different
- use Tamedevelopers\Validator\Validator;
 
public function save(Request $request){
    $form = new Validator();
    or
    $form = form();
}- All are Optional method- These methods are only mandatory on usage and should always come first before others.
 
| Methods | Description | 
|---|---|
| ->errorType() | boolto format error's on display:single or multiple | 
| ->token() | boolto Enable or Disablecsrf_tokenfor each request | 
| ->post() | Convert Form request to POSTonly | 
| ->get() | Convert Form request to GETonly | 
| ->all() | Convert Form request to any | 
$form->post()->rules([
    // 
]);- It's helper class can be called, using -- config_form()
| Keys | Description | 
|---|---|
| request | String POST|GET|ALLDefaultPOST | 
| error_type | Boolean true|falseDefaultfalse | 
| csrf_token | Boolean true|falseDefaulttrue | 
| class | Assoc Array error|successerror 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'
    ]
); - 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
 
- You can disable the usage with the 
 
- By default the form requires all request to have a token attached.
- 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
 
 
- It's a function and you don't need to 
csrf();
<input type="hidden" name="_token" value="efef4c9360f26848f0d102b478e51859cfce4c9b197eb9d6473abfcaa2c6da38">- This will return the csrf tokenstring
csrf_token();- All Methods of usage
- Takes a param as boolDefault isfalse- You can call separately or Call Before any other method, if intend to use.
 
| Error | Description | 
|---|---|
| false | DefaultErrors displayed one after another | 
| true | This allow all errors to be displayed once, as an array | 
$form->errorType(false);- Takes a param as boolDefault isfalse- Allow disability of csrf_tokenon each form request
 
- Allow disability of 
| Error | Description | 
|---|---|
| false | DefaultWill disablecsrf_tokenusage | 
| true | This allow csrf_tokenper request only | 
$form->token(false);- Set the Form Request to POST- This will always override the config_form()settings
 
- This will always override the 
$form->post();- Set the Form Request to GET
$form->get();- Will automatically detect if request type is GET\|POSTand get it's data.
$form->all()->rules([
    // 
])- same as all
$form->any()->rules([
    // 
])- By default only DATA TYPEand[INPUT_NAME]is required- Always seperate each indicator with a 'colon' :or 'pipe'|
 
- Always seperate each indicator with a 'colon' 
| DATA TYPE | INPUT_NAME | COMPARISON OPERATOR | VALUE TO COMPARE | 
|---|---|---|---|
| string | : country | : == | : 0 | 
| : | 
$form->rules([
    "string|country|==|0"   => 'Please Select a Country',
    "email:email"           => 'Please enter a valid email address',
])- HTML FORM Structure
<form>
    <select name="country">
        <option value="0">Select Country</option>
        <option value="NGA">Nigeria</option>
        <option value="USA">United States of America</option>
    </select>
    <input type="hidden" name="_token" value="749c345a1d407f29e777349f5e46a8d6d2cd51454b6719228b5ee28f94c30432">
    <input type="email" name="email" placeholder="Email Address">
</form>- Takes an [optional] closurefunction as the param
$form->rules([
    "s:name" => 'Please enter a name',
])->validate(function($response){
    $response->param; //Collection of form data
    $response->getMessage(); //message property
});- Expects a closurefunction as the param- Message property will be empty string on success $response->message
 
- Message property will be empty string on success 
$form->rules([
    "s:name" => 'Please enter a name',
])->save(function(){
    //on success
});- Supports 9 Data Flags type
| Data types | abbr | Description | 
|---|---|---|
| e | Emaildata validation | |
| bool | b | Booleandata validation | 
| string | s | Stringdata validation | 
| html | - | htmlCMS/Blog content validation | 
| dev | - | devCMS/Blog/Dev like content  validation | 
| raw | - | rawCollect content without validation | 
| str_len | sl | String Lengthvalidation | 
| enum | en | EnumFormscheckbox | radioor any form data that normally has no value when not checked | 
| array | a | Arraydata validation | 
| float | f | Floatdata validation | 
| int | i | Intdata validation | 
| url | u | Urldata validation | 
- Supports 10 operational statement
| sign | Description | 
|---|---|
| == | Equal to | 
| === | Strictly Equal to | 
| != | Not Equal To | 
| !== | Not Strictly Equal To | 
| > | Greater than | 
| >= | Greater than or Equal to | 
| < | Less than | 
| <= | Less than or Equal to | 
| < or > | Less than or Greater than | 
| < and > | Less than and Greater than | 
- Expects a closurefunction as the param- have access to form data without any validation
 
$form->noInterface(function($response){
    if($response->has('amount')){
        // exec code
    }
});- Expects a closurefunction as the param- Will only execute code within when Request is [GET]
- CSRF Token does'ntapply to this method
 
- CSRF Token 
 
- Will only execute code within when Request is [GET]
$form->rules([
    "s:name" => 'Please enter a name',
])->before(function($response){
    // execute code
});
- Expects a closurefunction as the param- Will always execute no matter the request method type
- CSRF Token does'ntapply to this method
 
- CSRF Token 
 
- Will always execute no matter the request method type
$form->after(function(){
    // execute code
});
- Returns bool true\|false
$form->hasError();
- Returns bool true\|false, When Form has already been validated
$form->isValidated();
- 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;
    <!-- Lets say for instance, users have wallet balance and the form request has no error -->
    <!-- But you need to perform another error validator before you allow request to pass through -->
    <!-- Don't forget the add the "return;" key to stop any other code from executing -->
    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
});
- Takes a param as an array- keysof data only needed, from the- form param
 
->save(function($response){
    //
    $data = $response->only(['password', 'username']);
});
- Exact opposite of only()method
->save(function($response){
    
    $data = $response->except(['_token']);
});
- Takes a param as stringinput name- Returns boolean as true|\false
 
- Returns boolean as 
->save(function($response){
    
    if($response->has('remeber_me')){
        // execute code
    }
});
- Takes a param as stringand return old inserted data- Second parameter is [optional] mixed data.
- It's helper class can be called, using -- old()
 
- Second parameter is [optional] 
$form->rules([
    "s:password" => 'Please enter a name',
    "s:retype_pass:!==:{$form->old('password')}" => 'Password mismatch, Please enter same password',
]);- or
<input type="email" name="email" value="<?= old('email', 'default_value')>">- Return all submitted form data as an array
->save(function($response){
    $data = $response->getForm();
});- Same as PHP function array_merge- Merge two array data together
- Second data will always repalace any matched key data in the first array
 
->save(function($response){
    
    $data = [
        'name' => 'Lorem Name',
        'user_id' => rand(10000, 99999),
    ];
    $param = $response->merge($data, [
        'password' => md5($param['password'])
    ]);
});- Return only data passedfrom set of given array elements.
| Keys | Data | 
|---|---|
| array | Main data to select from | 
->save(function($response){
    $data = $response->onlyData(['email', 'password'], [
        'email'     => 'mailer@mail.com', 
        '_token'    => md5('token'), 
        'age'       => 17,
        'password'  => 'test',
    ]);
// Only ['email', 'password'] will be returned.
});- Exact opposite of onlyData()method
| Keys | Data | 
|---|---|
| Keys are array | Main data to select from | 
->save(function($response){
    
    $data = $response->exceptData(['_token'], [
        'email'     => 'mailer@mail.com', 
        '_token'    => md5('token'), 
        'password'  => 'test'
    ]);
// Return all array element, except ['_token']
});| key | Description | 
|---|---|
| message | MessageThis convert all error messages and return as a string with<br> | 
| class | Class nameClass name on error and success | 
$form->getMessage();
$form->getClass();- Forms paramreturns a Collection Class- This enable us access property as an objectorarray index
 
- This enable us access property as an 
$form->rules([
    "string:country:==:0"   => 'Please Select a Country',
    "email:email"           => 'Please enter a valid email address',
])->save(function($response){
    $param = $response->param;
    $param->country;
    $param['country']
// As you can see, we're able to access data in both ways without errors
});| Methods | Description | 
|---|---|
| toArray() | arrayConvert items to array | 
| toObject() | objectConvert items to object | 
| toJson() | stringConvert items to json | 
- Takes a param as mixeddata- Converts to an Objectdata
 
- Converts to an 
$form->toObject([
    'food' => 'Basmati Rice'
]);- Takes a param as mixeddata- Converts to an Arraydata
 
- Converts to an 
$form->toArray([
    'food' => 'Basmati Rice'
]);- Takes a param as mixeddata- Converts to an Jsondata
 
- Converts to an 
$form->toJson([
    'food' => 'Basmati Rice'
]);| function | Description | 
|---|---|
| old() | Inherit instance of (new Validator)old() method | 
| form() | Return instance of (new Validator)class | 
- @author Fredrick Peterson (Tame Developers)
- Lightweight - PHP Form Validator
- If you love this PHP Library, you can Buy Tame Developers a coffee
- Link to Youtube Video Tutorial on usage will be available soon

