MaplePHP - Validation is a lightweight and powerful PHP library designed to simplify the validation of various data inputs. Whether you're verifying if a value is a valid email or phone number, ensuring string lengths, or performing more advanced checks like credit card numbers and dates, MaplePHP - Validation offers a comprehensive and intuitive approach. With its wide range of built-in validators and simple syntax, it makes handling complex validation tasks easier, leading to cleaner and more reliable code.
composer require maplephp/validate
You will always initiate an instance with the static method _val followed by a value you want to validate.
use MaplePHP\Validate\Inp;
// Validate option 1
$inp = new Inp("Lorem ipsum dolor");
var_dump($inp->length(1, 200)); // true
// Validate option 2
$valid = Inp::value("Lorem ipsum dolor")->length(1, 200);
var_dump($valid); // true
Inp::value("Lorem ipsum dolor")->required();
Inp::value(0)->hasValue();
- Min only:
Inp::value("Lorem ipsum dolor")->length(1);
- Min and Max:
Inp::value("Lorem ipsum dolor")->length(1, 160);
Inp::value("Lorem ipsum dolor")->equalLength(10);
- Equals:
Inp::value("Lorem ipsum dolor")->equal("Lorem ipsum dolor");
- Not equals:
Inp::value("Lorem ipsum dolor")->notEqual("Lorem ipsum");
Inp::value("john@gmail.com")->email();
Allows numbers and special characters ("-", "+", " ").
Inp::value("+46709676040")->phone();
Inp::value("198808213412")->socialNumber();
Inp::value("197511043412")->orgNumber();
Inp::value("1616523623422334")->creditCard();
Inp::value("SE8272267913")->vatNumber();
Inp::value("3.1415")->isFloat();
Inp::value("42")->isInt();
Inp::value("42")->number();
- Positive:
Inp::value("20")->positive();
- Negative:
Inp::value("-20")->negative();
Inp::value("1.0.0")->validVersion(true); // strict semantic versioning
Inp::value("1.0.0")->versionCompare("2.0.0", '>=');
- Lossy password (minimum character set):
Inp::value("password123")->lossyPassword(8);
- Strict password (requires at least one lowercase, uppercase, digit, and special character):
Inp::value("Password#123!")->strictPassword(8);
- Both cases:
Inp::value("HelloWorld")->atoZ();
- Lowercase only:
Inp::value("helloworld")->lowerAtoZ();
- Uppercase only:
Inp::value("HELLOWORLD")->upperAtoZ();
Inp::value("#000000")->hex();
Inp::value("2022-02-13")->date("Y-m-d");
Inp::value("2022-02-13 14:15")->dateTime("Y-m-d H:i");
Inp::value("14:15")->time("H:i");
Inp::value("1988-05-22")->age(18);
Inp::value("example.com")->domain();
Inp::value("https://example.com/page")->url();
Inp::value("example.com")->dns();
- Check if it's a valid file:
Inp::value("/path/to/file.txt")->isFile();
- Check if it's a directory:
Inp::value("/path/to/directory")->isDir();
- Check if it's writable:
Inp::value("/path/to/file.txt")->isWritable();
- Check if it's readable:
Inp::value("/path/to/file.txt")->isReadable();
Inp::value("12345")->zip(5);
Inp::value("abc")->pregMatch("a-zA-Z");
- Array:
Inp::value([1, 2, 3])->isArray();
- Object:
Inp::value($obj)->isObject();
- Resource:
Inp::value($resource)->isResource();
- Is Boolean:
Inp::value(true)->isBool();
- Is Boolean-like value (e.g., "yes", "no", "1", "0"):
Inp::value("yes")->isBoolVal();
- Validate if one method passes:
Inp::value("12345")->oneOf(['isInt' => []]);
- Validate if all methods pass:
Inp::value("12345")->allOf(['isInt' => [], 'length' => [5]]);