Envalid

A Framework agnostic form validation library

Why Envalid

  • 100% free & Open Source
  • Super fluent API
  • Loaded with built-in rules
  • Easy to extend
  • Unit Tested
  • Works on most PHP versions

Built-in Rules

Change the dropdown to check the syntax with different rules

  • Required
  • Email
  • Password
  • Same
  • File
  • Alpha
  • Alnum
  • Length
  • Min
  • Max
  • Number
  • IP
  • Boolean
  • Array

    use azi/Envalid;
    $validator = new Envalid;
    $validator->validate($_POST, [
                       'username' => 'required'
                     ]);

    if($validator->failed()){
        // redirect back or return errors json as response
    }
                                

    use azi/Envalid;
    $validator = new Envalid;
    $validator->validate($_POST, [
                       'username' => 'required',
                       'password' => 'password:strong'
                     ]);

    if($validator->failed()){
        // redirect back or return errors json as response
    }
                                

    use azi/Envalid;
    $validator = new Envalid;
    $validator->validate(array_merge($_FILES, $_POST), [
                       'bucket'     => 'required',
                       'attachment' => 'file:image'
                     ]);

    if($validator->failed()){
        // redirect back or return errors json as response
    }
                                

    use azi/Envalid;
    $validator = new Envalid;
    $validator->validate($_POST, [
                      'name' => 'alpha'
                     ]);

    if($validator->failed()){
        // redirect back or return errors json as response
    }
                                

    use azi/Envalid;
    $validator = new Envalid;
    $validator->validate($_POST, [
                      'slug' => 'alnum'
                     ]);

    if($validator->failed()){
        // redirect back or return errors json as response
    }
                                

    use azi/Envalid;
    $validator = new Envalid;
    $validator->validate($_POST, [
                      'description' => 'length:350'
                     ]);

    if($validator->failed()){
        // redirect back or return errors json as response
    }
                                

    use azi/Envalid;
    $validator = new Envalid;
    $validator->validate($_POST, [
                      'salary' => 'min:50000'
                     ]);

    if($validator->failed()){
        // redirect back or return errors json as response
    }
                                

    use azi/Envalid;
    $validator = new Envalid;
    $validator->validate($_POST, [
                      'salary' => 'max:5000000'
                     ]);

    if($validator->failed()){
        // redirect back or return errors json as response
    }
                                

    use azi/Envalid;
    $validator = new Envalid;
    $validator->validate($_POST, [
                      'hobbies' => 'array'
                     ]);

    if($validator->failed()){
        // redirect back or return errors json as response
    }
                                

    use azi/Envalid;
    $validator = new Envalid;
    $validator->validate($_POST, [
                      'zip_code' => 'number'
                     ]);

    if($validator->failed()){
        // redirect back or return errors json as response
    }
                                

    use azi/Envalid;
    $validator = new Envalid;
    $validator->validate($_POST, [
                      'is_human' => 'boolean:true'
                     ]);

    if($validator->failed()){
        // redirect back or return errors json as response
    }
                                

    use azi/Envalid;
    $validator = new Envalid;
    $validator->validate($_POST, [
                      'ip' => 'ip'
                     ]);

    if($validator->failed()){
        // redirect back or return errors json as response
    }
                                

    use azi/Envalid;
    $validator = new Envalid;
    $validator->validate($_POST, [
                      'password'         => 'password:medium',
                      'confirm_password' => 'same:password'
                     ]);

    if($validator->failed()){
        // redirect back or return errors json as response
    }
                                

Extend Envalid

You can extend envalid by registering your custom rules based on your business requirements

<?php

use azi\Arguments;
use azi\Rules\Contracts\RuleInterface;

class NationalId implements RuleInterface {
     /**
     * @param $field
     * @param $value
     * @param Arguments $args
     * @return mixed
     */
    public function validate( $field, $value, Arguments $args )
    {
        # validate national ID card number and return true or false
    }

    /**
     * @return mixed
     */
    public function message()
    {
        return '{field} must be a valid NIC number';
    }
}

                            

Once your custom rule is ready, register it with Envalid and start using it


<php
$validator = new azi/Envalid();
$validator->addRule('cnic', new NationalId());
// now the rule is registered with Envalid let's use it
$validator->validate($_POST, [
    'national_id' => 'cnic'
]);