{"id":13410107,"url":"https://github.com/vlucas/valitron","last_synced_at":"2025-05-14T01:08:49.391Z","repository":{"id":6604321,"uuid":"7847550","full_name":"vlucas/valitron","owner":"vlucas","description":"Valitron is a simple, elegant, stand-alone validation library with NO dependencies","archived":false,"fork":false,"pushed_at":"2024-04-16T08:09:20.000Z","size":539,"stargazers_count":1585,"open_issues_count":54,"forks_count":255,"subscribers_count":61,"default_branch":"master","last_synced_at":"2025-04-03T01:43:07.786Z","etag":null,"topics":["hacktoberfest","php","validation","validator"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vlucas.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"tidelift":"packagist/vlucas/valitron"}},"created_at":"2013-01-27T03:28:43.000Z","updated_at":"2025-03-30T14:04:25.000Z","dependencies_parsed_at":"2024-06-18T10:58:50.296Z","dependency_job_id":null,"html_url":"https://github.com/vlucas/valitron","commit_stats":{"total_commits":306,"total_committers":105,"mean_commits":2.914285714285714,"dds":0.869281045751634,"last_synced_commit":"fadce39f5f235755bb9794b2573af2d5bfcba85f"},"previous_names":[],"tags_count":33,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vlucas%2Fvalitron","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vlucas%2Fvalitron/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vlucas%2Fvalitron/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vlucas%2Fvalitron/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vlucas","download_url":"https://codeload.github.com/vlucas/valitron/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248154606,"owners_count":21056540,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hacktoberfest","php","validation","validator"],"created_at":"2024-07-30T20:01:05.009Z","updated_at":"2025-04-10T03:40:00.940Z","avatar_url":"https://github.com/vlucas.png","language":"PHP","readme":"## Valitron: Easy Validation That Doesn't Suck\n\nValitron is a simple, minimal and elegant stand-alone validation library\nwith NO dependencies. Valitron uses simple, straightforward validation\nmethods with a focus on readable and concise syntax. Valitron is the\nsimple and pragmatic validation library you've been looking for.\n\n[![Build\nStatus](https://github.com/vlucas/valitron/actions/workflows/test.yml/badge.svg)](https://github.com/vlucas/valitron/actions/workflows/test.yml)\n[![Latest Stable Version](https://poser.pugx.org/vlucas/valitron/v/stable.png)](https://packagist.org/packages/vlucas/valitron)\n[![Total Downloads](https://poser.pugx.org/vlucas/valitron/downloads.png)](https://packagist.org/packages/vlucas/valitron)\n\n[Get supported vlucas/valitron with the Tidelift Subscription](https://tidelift.com/subscription/pkg/packagist-vlucas-valitron?utm_source=packagist-vlucas-valitron\u0026utm_medium=referral\u0026utm_campaign=readme) \n\n## Why Valitron?\n\nValitron was created out of frustration with other validation libraries\nthat have dependencies on large components from other frameworks like\nSymfony's HttpFoundation, pulling in a ton of extra files that aren't\nreally needed for basic validation. It also has purposefully simple\nsyntax used to run all validations in one call instead of individually\nvalidating each value by instantiating new classes and validating values\none at a time like some other validation libraries require.\n\nIn short, Valitron is everything you've been looking for in a validation\nlibrary but haven't been able to find until now: simple pragmatic\nsyntax, lightweight code that makes sense, extensible for custom\ncallbacks and validations, well tested, and without dependencies. Let's\nget started.\n\n## Installation\n\nValitron uses [Composer](http://getcomposer.org) to install and update:\n\n```\ncurl -s http://getcomposer.org/installer | php\nphp composer.phar require vlucas/valitron\n```\n\nThe examples below use PHP 5.4 syntax, but Valitron works on PHP 5.3+.\n\n## Usage\n\nUsage is simple and straightforward. Just supply an array of data you\nwish to validate, add some rules, and then call `validate()`. If there\nare any errors, you can call `errors()` to get them.\n\n```php\n$v = new Valitron\\Validator(array('name' =\u003e 'Chester Tester'));\n$v-\u003erule('required', 'name');\nif($v-\u003evalidate()) {\n    echo \"Yay! We're all good!\";\n} else {\n    // Errors\n    print_r($v-\u003eerrors());\n}\n```\n\nUsing this format, you can validate `$_POST` data directly and easily,\nand can even apply a rule like `required` to an array of fields:\n\n```php\n$v = new Valitron\\Validator($_POST);\n$v-\u003erule('required', ['name', 'email']);\n$v-\u003erule('email', 'email');\nif($v-\u003evalidate()) {\n    echo \"Yay! We're all good!\";\n} else {\n    // Errors\n    print_r($v-\u003eerrors());\n}\n```\n\nYou may use dot syntax to access members of multi-dimensional arrays,\nand an asterisk to validate each member of an array:\n\n```php\n$v = new Valitron\\Validator(array('settings' =\u003e array(\n    array('threshold' =\u003e 50),\n    array('threshold' =\u003e 90)\n)));\n$v-\u003erule('max', 'settings.*.threshold', 100);\nif($v-\u003evalidate()) {\n    echo \"Yay! We're all good!\";\n} else {\n    // Errors\n    print_r($v-\u003eerrors());\n}\n```\n\nOr use dot syntax to validate all members of a numeric array:\n\n```php\n$v = new Valitron\\Validator(array('values' =\u003e array(50, 90)));\n$v-\u003erule('max', 'values.*', 100);\nif($v-\u003evalidate()) {\n    echo \"Yay! We're all good!\";\n} else {\n    // Errors\n    print_r($v-\u003eerrors());\n}\n```\n\nYou can also access nested values using dot notation:\n\n```php\n$v = new Valitron\\Validator(array('user' =\u003e array('first_name' =\u003e 'Steve', 'last_name' =\u003e 'Smith', 'username' =\u003e 'Batman123')));\n$v-\u003erule('alpha', 'user.first_name')-\u003erule('alpha', 'user.last_name')-\u003erule('alphaNum', 'user.username');\nif($v-\u003evalidate()) {\n    echo \"Yay! We're all good!\";\n} else {\n    // Errors\n    print_r($v-\u003eerrors());\n}\n```\n\nSetting language and language dir globally:\n\n```php\n\n// boot or config file\n\nuse Valitron\\Validator as V;\n\nV::langDir(__DIR__.'/validator_lang'); // always set langDir before lang.\nV::lang('ar');\n\n```\n\nDisabling the {field} name in the output of the error message. \n\n```php\nuse Valitron\\Validator as V;\n\n$v = new Valitron\\Validator(['name' =\u003e 'John']);\n$v-\u003erule('required', ['name']);\n\n// Disable prepending the labels\n$v-\u003esetPrependLabels(false);\n\n// Error output for the \"false\" condition\n[\n    [\"name\"] =\u003e [\n        \"is required\"\n    ]\n]\n\n// Error output for the default (true) condition\n[\n    [\"name\"] =\u003e [\n        \"name is required\"\n    ]\n]\n\n```\n\nYou can conditionally require values using required conditional rules. In this example, for authentication, we're requiring either a token when both the email and password are not present, or a password when the email address is present.\n```php\n// this rule set would work for either data set...\n$data = ['email' =\u003e 'test@test.com', 'password' =\u003e 'mypassword'];\n// or...\n$data = ['token' =\u003e 'jashdjahs83rufh89y38h38h'];\n\n$v = new Valitron\\Validator($data);\n$v-\u003erules([\n    'requiredWithout' =\u003e [\n        ['token', ['email', 'password'], true]\n    ],\n    'requiredWith' =\u003e [\n        ['password', ['email']]\n    ],\n    'email' =\u003e [\n        ['email']\n    ]\n    'optional' =\u003e [\n        ['email']\n    ]\n]);\n$this-\u003eassertTrue($v-\u003evalidate());\n```\n\n## Built-in Validation Rules\n\n * `required` - Field is required\n * `requiredWith` - Field is required if any other fields are present\n * `requiredWithout` - Field is required if any other fields are NOT present\n * `equals` - Field must match another field (email/password confirmation)\n * `different` - Field must be different than another field\n * `accepted` - Checkbox or Radio must be accepted (yes, on, 1, true)\n * `numeric` - Must be numeric\n * `integer` - Must be integer number\n * `boolean` - Must be boolean\n * `array` - Must be array\n * `length` - String must be certain length\n * `lengthBetween` - String must be between given lengths\n * `lengthMin` - String must be greater than given length\n * `lengthMax` - String must be less than given length\n * `min` - Minimum\n * `max` - Maximum\n * `listContains` - Performs in_array check on given array values (the other way round than `in`)\n * `in` - Performs in_array check on given array values\n * `notIn` - Negation of `in` rule (not in array of values)\n * `ip` - Valid IP address\n * `ipv4` - Valid IP v4 address\n * `ipv6` - Valid IP v6 address\n * `email` - Valid email address\n * `emailDNS` - Valid email address with active DNS record\n * `url` - Valid URL\n * `urlActive` - Valid URL with active DNS record\n * `alpha` - Alphabetic characters only\n * `alphaNum` - Alphabetic and numeric characters only\n * `ascii` - ASCII characters only\n * `slug` - URL slug characters (a-z, 0-9, -, \\_)\n * `regex` - Field matches given regex pattern\n * `date` - Field is a valid date\n * `dateFormat` - Field is a valid date in the given format\n * `dateBefore` - Field is a valid date and is before the given date\n * `dateAfter` - Field is a valid date and is after the given date\n * `contains` - Field is a string and contains the given string\n * `subset` - Field is an array or a scalar and all elements are contained in the given array\n * `containsUnique` - Field is an array and contains unique values\n * `creditCard` - Field is a valid credit card number\n * `instanceOf` - Field contains an instance of the given class\n * `optional` - Value does not need to be included in data array. If it is however, it must pass validation.\n * `arrayHasKeys` - Field is an array and contains all specified keys.\n\n**NOTE**: If you are comparing floating-point numbers with min/max validators, you\nshould install the [BCMath](http://us3.php.net/manual/en/book.bc.php)\nextension for greater accuracy and reliability. The extension is not required\nfor Valitron to work, but Valitron will use it if available, and it is highly\nrecommended.\n\n## required fields usage\nthe `required` rule checks if a field exists in the data array, and is not null or an empty string.\n```php\n$v-\u003erule('required', 'field_name');\n```\n\nUsing an extra parameter, you can make this rule more flexible, and only check if the field exists in the data array.\n```php\n$v-\u003erule('required', 'field_name', true);\n```\n\nAlternate syntax.\n```php\n$v = new Valitron\\Validator(['username' =\u003e 'spiderman', 'password' =\u003e 'Gr33nG0Blin', 'required_but_null' =\u003e null]);\n$v-\u003erules([\n    'required' =\u003e [\n        ['username'],\n        ['password'],\n        ['required_but_null', true] // boolean flag allows empty value so long as the field name is set on the data array\n    ]\n]);\n$v-\u003evalidate();\n```\n\n## requiredWith fields usage\nThe `requiredWith` rule checks that the field is required, not null, and not the empty string, if any other fields are present, not null, and not the empty string.\n```php\n// password field will be required when the username field is provided and not empty\n$v-\u003erule('requiredWith', 'password', 'username');\n```\n\nAlternate syntax.\n```php\n$v = new Valitron\\Validator(['username' =\u003e 'spiderman', 'password' =\u003e 'Gr33nG0Blin']);\n$v-\u003erules([\n    'requiredWith' =\u003e [\n        ['password', 'username']\n    ]\n]);\n$v-\u003evalidate();\n```\n\n*Note* You can provide multiple values as an array. In this case if ANY of the fields are present the field will be required.\n```php\n// in this case the password field will be required if the username or email fields are present\n$v-\u003erule('requiredWith', 'password', ['username', 'email']);\n```\n\nAlternate syntax.\n```php\n$v = new Valitron\\Validator(['username' =\u003e 'spiderman', 'password' =\u003e 'Gr33nG0Blin']);\n$v-\u003erules([\n    'requiredWith' =\u003e [\n        ['password', ['username', 'email']]\n    ]\n]);\n$v-\u003evalidate();\n```\n\n### Strict flag\nThe strict flag will change the `requiredWith` rule to `requiredWithAll` which will require the field only if ALL of the other fields are present, not null, and not the empty string.\n```php\n// in this example the suffix field is required only when both the first_name and last_name are provided\n$v-\u003erule('requiredWith', 'suffix', ['first_name', 'last_name'], true);\n```\nAlternate syntax.\n```php\n$v = new Valitron\\Validator(['first_name' =\u003e 'steve', 'last_name' =\u003e 'holt', 'suffix' =\u003e 'Mr']);\n$v-\u003erules([\n    'requiredWith' =\u003e [\n        ['suffix', ['first_name', 'last_name'], true]\n    ]\n]);\n$v-\u003evalidate();\n```\n\nLikewise, in this case `validate()` would still return true, as the suffix field would not be required in strict mode, as not all of the fields are provided.\n```php\n$v = new Valitron\\Validator(['first_name' =\u003e 'steve']);\n$v-\u003erules([\n    'requiredWith' =\u003e [\n        ['suffix', ['first_name', 'last_name'], true]\n    ]\n]);\n$v-\u003evalidate();\n```\n\n## requiredWithout fields usage\nThe `requiredWithout` rule checks that the field is required, not null, and not the empty string, if any other fields are NOT present.\n```php\n// this rule will require the username field when the first_name is not present\n$v-\u003erule('requiredWithout', 'username', 'first_name')\n```\n\nAlternate syntax.\n```php\n// this will return true, as the username is provided when the first_name is not provided\n$v = new Valitron\\Validator(['username' =\u003e 'spiderman']);\n$v-\u003erules([\n    'requiredWithout' =\u003e [\n        ['username', 'first_name']\n    ]\n]);\n$v-\u003evalidate();\n```\n\n*Note* You can provide multiple values as an array. In this case if ANY of the fields are NOT present the field will be required.\n```php\n// in this case the username field will be required if either the first_name or last_name fields are not present\n$v-\u003erule('requiredWithout', 'username', ['first_name', 'last_name']);\n```\n\nAlternate syntax.\n```php\n// this passes validation because although the last_name field is not present, the username is provided\n$v = new Valitron\\Validator(['username' =\u003e 'spiderman', 'first_name' =\u003e 'Peter']);\n$v-\u003erules([\n    'requiredWithout' =\u003e [\n        ['username', ['first_name', 'last_name']]\n    ]\n]);\n$v-\u003evalidate();\n```\n\n### Strict flag\nThe strict flag will change the `requiredWithout` rule to `requiredWithoutAll` which will require the field only if ALL of the other fields are not present.\n```php\n// in this example the username field is required only when both the first_name and last_name are not provided\n$v-\u003erule('requiredWithout', 'username', ['first_name', 'last_name'], true);\n```\nAlternate syntax.\n```php\n$v = new Valitron\\Validator(['username' =\u003e 'BatMan']);\n$v-\u003erules([\n    'requiredWithout' =\u003e [\n        ['username', ['first_name', 'last_name'], true]\n    ]\n]);\n$v-\u003evalidate();\n```\n\nLikewise, in this case `validate()` would still return true, as the username field would not be required in strict mode, as all of the fields are provided.\n```php\n$v = new Valitron\\Validator(['first_name' =\u003e 'steve', 'last_name' =\u003e 'holt']);\n$v-\u003erules([\n    'requiredWithout' =\u003e [\n        ['suffix', ['first_name', 'last_name'], true]\n    ]\n]);\n$v-\u003evalidate();\n```\n\n## equals fields usage\nThe `equals` rule checks if two fields are equals in the data array, and that the second field is not null.\n```php\n$v-\u003erule('equals', 'password', 'confirmPassword');\n```\n\nAlternate syntax.\n```php\n$v = new Valitron\\Validator(['password' =\u003e 'youshouldnotseethis', 'confirmPassword' =\u003e 'youshouldnotseethis']);\n$v-\u003erules([\n    'equals' =\u003e [\n        ['password', 'confirmPassword']\n    ]\n]);\n$v-\u003evalidate();\n```\n\n## different fields usage\nThe `different` rule checks if two fields are not the same, or different, in the data array and that the second field is not null.\n```php\n$v-\u003erule('different', 'username', 'password');\n```\n\nAlternate syntax.\n```php\n$v = new Valitron\\Validator(['username' =\u003e 'spiderman', 'password' =\u003e 'Gr33nG0Blin']);\n$v-\u003erules([\n    'different' =\u003e [\n        ['username', 'password']\n    ]\n]);\n$v-\u003evalidate();\n```\n\n## accepted fields usage\nThe `accepted` rule checks if the field is either 'yes', 'on', 1, or true.\n```php\n$v-\u003erule('accepted', 'remember_me');\n```\n\nAlternate syntax.\n```php\n$v = new Valitron\\Validator(['remember_me' =\u003e true]);\n$v-\u003erules([\n    'accepted' =\u003e [\n        ['remember_me']\n    ]\n]);\n$v-\u003evalidate();\n```\n\n## numeric fields usage\nThe `numeric` rule checks if the field is number. This is analogous to php's is_numeric() function.\n```php\n$v-\u003erule('numeric', 'amount');\n```\n\nAlternate syntax.\n```php\n$v = new Valitron\\Validator(['amount' =\u003e 3.14]);\n$v-\u003erules([\n    'numeric' =\u003e [\n        ['amount']\n    ]\n]);\n$v-\u003evalidate();\n```\n\n## integer fields usage\nThe `integer` rule checks if the field is an integer number.\n```php\n$v-\u003erule('integer', 'age');\n```\n\nAlternate syntax.\n```php\n$v = new Valitron\\Validator(['age' =\u003e '27']);\n$v-\u003erules([\n    'integer' =\u003e [\n        ['age']\n    ]\n]);\n$v-\u003evalidate();\n```\n\n*Note* the optional boolean flag for strict mode makes sure integers are to be supplied in a strictly numeric form. So the following rule would evaluate to true:\n```php\n$v = new Valitron\\Validator(['negative' =\u003e '-27', 'positive'=\u003e'27']);\n$v-\u003erule('integer', 'age', true);\n$v-\u003erule('integer', 'height', true);\n$v-\u003evalidate();\n```\n\nWhereas the following will evaluate to false, as the + for the positive number in this case is redundant:\n```php\n$v = new Valitron\\Validator(['negative' =\u003e '-27', 'positive'=\u003e'+27']);\n$v-\u003erule('integer', 'age', true);\n$v-\u003erule('integer', 'height', true);\n$v-\u003evalidate();\n```\n\n## boolean fields usage\nThe `boolean` rule checks if the field is a boolean. This is analogous to php's is_bool() function.\n```php\n$v-\u003erule('boolean', 'remember_me');\n```\n\nAlternate syntax.\n```php\n$v = new Valitron\\Validator(['remember_me' =\u003e true]);\n$v-\u003erules([\n    'boolean' =\u003e [\n        ['remember_me']\n    ]\n]);\n$v-\u003evalidate();\n```\n\n## array fields usage\nThe `array` rule checks if the field is an array. This is analogous to php's is_array() function.\n```php\n$v-\u003erule('array', 'user_notifications');\n```\n\nAlternate Syntax.\n```php\n$v = new Valitron\\Validator(['user_notifications' =\u003e ['bulletin_notifications' =\u003e true, 'marketing_notifications' =\u003e false, 'message_notification' =\u003e true]]);\n$v-\u003erules([\n    'array' =\u003e [\n        ['user_notifications']\n    ]\n]);\n$v-\u003evalidate();\n```\n\n## length fields usage\nThe `length` rule checks if the field is exactly a given length and that the field is a valid string.\n```php\n$v-\u003erule('length', 'username', 10);\n```\n\nAlternate syntax.\n```php\n$v = new Valitron\\Validator(['username' =\u003e 'bobburgers']);\n$v-\u003erules([\n    'length' =\u003e [\n        ['username', 10]\n    ]\n]);\n$v-\u003evalidate();\n```\n\n## lengthBetween fields usage\nThe `lengthBetween` rule checks if the field is between a given length tange and that the field is a valid string.\n```php\n$v-\u003erule('lengthBetween', 'username', 1, 10);\n```\n\nAlternate syntax.\n```php\n$v = new Valitron\\Validator(['username' =\u003e 'bobburgers']);\n$v-\u003erules([\n    'lengthBetween' =\u003e [\n        ['username', 1, 10]\n    ]\n]);\n$v-\u003evalidate();\n```\n\n## lengthMin fields usage\nThe `lengthMin` rule checks if the field is at least a given length and that the field is a valid string.\n```php\n$v-\u003erule('lengthMin', 'username', 5);\n```\n\nAlternate syntax.\n```php\n$v = new Valitron\\Validator(['username' =\u003e 'martha']);\n$v-\u003erules([\n    'lengthMin' =\u003e [\n        ['username', 5]\n    ]\n]);\n$v-\u003evalidate();\n```\n\n## lengthMax fields usage\nThe `lengthMax` rule checks if the field is at most a given length and that the field is a valid string.\n```php\n$v-\u003erule('lengthMax', 'username', 10);\n```\n\nAlternate syntax.\n```php\n$v = new Valitron\\Validator(['username' =\u003e 'bruins91']);\n$v-\u003erules([\n    'lengthMax' =\u003e [\n        ['username', 10]\n    ]\n]);\n$v-\u003evalidate();\n```\n\n## min fields usage\nThe `min` rule checks if the field is at least a given value and that the provided value is numeric.\n```php\n$v-\u003erule('min', 'age', 18);\n```\n\nAlternate syntax.\n```php\n$v = new Valitron\\Validator(['age' =\u003e 28]);\n$v-\u003erules([\n    'min' =\u003e [\n        ['age', 18]\n    ]\n]);\n$v-\u003evalidate();\n```\n\n## max fields usage\nThe `max` rule checks if the field is at most a given value and that the provided value is numeric.\n```php\n$v-\u003erule('max', 'age', 12);\n```\n\nAlternate syntax.\n```php\n$v = new Valitron\\Validator(['age' =\u003e 10]);\n$v-\u003erules([\n    'max' =\u003e [\n        ['age', 12]\n    ]\n]);\n$v-\u003evalidate();\n```\n\n## listContains fields usage\nThe `listContains` rule checks that the field is present in a given array of values.\n```php\n$v-\u003erule('listContains', 'color', 'yellow');\n```\n\nAlternate syntax.\n```php\n$v = new Valitron\\Validator(['color' =\u003e ['blue', 'green', 'red', 'yellow']]);\n$v-\u003erules([\n    'listContains' =\u003e [\n        ['color', 'yellow']\n    ]\n]);\n$v-\u003evalidate();\n```\n\n## in fields usage\nThe `in` rule checks that the field is present in a given array of values.\n```php\n$v-\u003erule('in', 'color', ['blue', 'green', 'red', 'purple']);\n```\n\nAlternate syntax.\n```php\n$v = new Valitron\\Validator(['color' =\u003e 'purple']);\n$v-\u003erules([\n    'in' =\u003e [\n        ['color', ['blue', 'green', 'red', 'purple']]\n    ]\n]);\n$v-\u003evalidate();\n```\n\n## notIn fields usage\nThe `notIn` rule checks that the field is NOT present in a given array of values.\n```php\n$v-\u003erule('notIn', 'color', ['blue', 'green', 'red', 'yellow']);\n```\n\nAlternate syntax.\n```php\n$v = new Valitron\\Validator(['color' =\u003e 'purple']);\n$v-\u003erules([\n    'notIn' =\u003e [\n        ['color', ['blue', 'green', 'red', 'yellow']]\n    ]\n]);\n$v-\u003evalidate();\n```\n\n## ip fields usage\nThe `ip` rule checks that the field is a valid ip address. This includes IPv4, IPv6, private, and reserved ranges.\n```php\n$v-\u003erule('ip', 'user_ip');\n```\n\nAlternate syntax.\n```php\n$v = new Valitron\\Validator(['user_ip' =\u003e '127.0.0.1']);\n$v-\u003erules([\n    'ip' =\u003e [\n        ['user_ip']\n    ]\n]);\n$v-\u003evalidate();\n```\n\n## ipv4 fields usage\nThe `ipv4` rule checks that the field is a valid IPv4 address.\n```php\n$v-\u003erule('ipv4', 'user_ip');\n```\n\nAlternate syntax.\n```php\n$v = new Valitron\\Validator(['user_ip' =\u003e '127.0.0.1']);\n$v-\u003erules([\n    'ipv4' =\u003e [\n        ['user_ip']\n    ]\n]);\n$v-\u003evalidate();\n```\n\n## ipv6 fields usage\nThe `ipv6` rule checks that the field is a valid IPv6 address.\n```php\n$v-\u003erule('ipv6', 'user_ip');\n```\n\nAlternate syntax.\n```php\n$v = new Valitron\\Validator(['user_ip' =\u003e '0:0:0:0:0:0:0:1']);\n$v-\u003erules([\n    'ipv6' =\u003e [\n        ['user_ip']\n    ]\n]);\n$v-\u003evalidate();\n```\n\n## email fields usage\nThe `email` rule checks that the field is a valid email address.\n```php\n$v-\u003erule('email', 'user_email');\n```\n\nAlternate syntax.\n```php\n$v = new Valitron\\Validator(['user_email' =\u003e 'someone@example.com']);\n$v-\u003erules([\n    'email' =\u003e [\n        ['user_email']\n    ]\n]);\n$v-\u003evalidate();\n```\n\n## emailDNS fields usage\nThe `emailDNS` rule validates the field is a valid email address with an active DNS record or any type.\n```php\n$v-\u003erule('emailDNS', 'user_email');\n```\n\nAlternate syntax.\n```php\n$v = new Valitron\\Validator(['user_email' =\u003e 'some_fake_email_address@gmail.com']);\n$v-\u003erules([\n    'emailDNS' =\u003e [\n        ['user_email']\n    ]\n]);\n$v-\u003evalidate();\n```\n\n## url fields usage\nThe `url` rule checks the field is a valid url.\n```php\n$v-\u003erule('url', 'website');\n```\n\nAlternate syntax.\n```php\n$v = new Valitron\\Validator(['website' =\u003e 'https://example.com/contact']);\n$v-\u003erules([\n    'url' =\u003e [\n        ['website']\n    ]\n]);\n$v-\u003evalidate();\n```\n\n## urlActive fields usage\nThe `urlActive` rule checks the field is a valid url with an active A, AAAA, or CNAME record.\n```php\n$v-\u003erule('urlActive', 'website');\n```\n\nAlternate syntax.\n```php\n$v = new Valitron\\Validator(['website' =\u003e 'https://example.com/contact']);\n$v-\u003erules([\n    'urlActive' =\u003e [\n        ['website']\n    ]\n]);\n$v-\u003evalidate();\n```\n\n## alpha fields usage\nThe `alpha` rule checks the field is alphabetic characters only.\n```php\n$v-\u003erule('alpha', 'username');\n```\n\nAlternate syntax.\n```php\n$v = new Valitron\\Validator(['username' =\u003e 'batman']);\n$v-\u003erules([\n    'alpha' =\u003e [\n        ['username']\n    ]\n]);\n$v-\u003evalidate();\n```\n\n## alphaNum fields usage\nThe `alphaNum` rule checks the field contains only alphabetic or numeric characters.\n```php\n$v-\u003erule('alphaNum', 'username');\n```\n\nAlternate syntax.\n```php\n$v = new Valitron\\Validator(['username' =\u003e 'batman123']);\n$v-\u003erules([\n    'alphaNum' =\u003e [\n        ['username']\n    ]\n]);\n$v-\u003evalidate();\n```\n\n## ascii fields usage\nThe `ascii` rule checks the field contains only characters in the ascii character set.\n```php\n$v-\u003erule('ascii', 'username');\n```\n\nAlternate syntax.\n```php\n$v = new Valitron\\Validator(['username' =\u003e 'batman123']);\n$v-\u003erules([\n    'ascii' =\u003e [\n        ['username']\n    ]\n]);\n$v-\u003evalidate();\n```\n\n## slug fields usage\nThe `slug` rule checks that the field only contains URL slug characters (a-z, 0-9, -, _).\n```php\n$v-\u003erule('slug', 'username');\n```\n\nAlternate syntax.\n```php\n$v = new Valitron\\Validator(['username' =\u003e 'L337-H4ckZ0rz_123']);\n$v-\u003erules([\n    'slug' =\u003e [\n        ['username']\n    ]\n]);\n$v-\u003evalidate();\n```\n\n## regex fields usage\nThe `regex` rule ensures the field matches a given regex pattern.\n(This regex checks the string is alpha numeric between 5-10 characters).\n```php\n$v-\u003erule('regex', 'username', '/^[a-zA-Z0-9]{5,10}$/');\n```\n\nAlternate syntax.\n```php\n$v = new Valitron\\Validator(['username' =\u003e 'Batman123']);\n$v-\u003erules([\n    'regex' =\u003e [\n        ['username', '/^[a-zA-Z0-9]{5,10}$/']\n    ]\n]);\n$v-\u003evalidate();\n```\n\n## date fields usage\nThe `date` rule checks if the supplied field is a valid \\DateTime object or if the string can be converted to a unix timestamp via strtotime().\n```php\n$v-\u003erule('date', 'created_at');\n```\n\nAlternate syntax.\n```php\n$v = new Valitron\\Validator(['created_at' =\u003e '2018-10-13']);\n$v-\u003erules([\n    'date' =\u003e [\n        ['created_at']\n    ]\n]);\n$v-\u003evalidate();\n```\n\n## dateFormat fields usage\nThe `dateFormat` rule checks that the supplied field is a valid date in a specified date format.\n```php\n$v-\u003erule('dateFormat', 'created_at', 'Y-m-d');\n```\n\nAlternate syntax.\n```php\n$v = new Valitron\\Validator(['created_at' =\u003e '2018-10-13']);\n$v-\u003erules([\n    'dateFormat' =\u003e [\n        ['created_at', 'Y-m-d']\n    ]\n]);\n$v-\u003evalidate();\n```\n\n## dateBefore fields usage\nThe `dateBefore` rule checks that the supplied field is a valid date before a specified date.\n```php\n$v-\u003erule('dateBefore', 'created_at', '2018-10-13');\n```\n\nAlternate syntax.\n```php\n$v = new Valitron\\Validator(['created_at' =\u003e '2018-09-01']);\n$v-\u003erules([\n    'dateBefore' =\u003e [\n        ['created_at', '2018-10-13']\n    ]\n]);\n$v-\u003evalidate();\n```\n\n## dateAfter fields usage\nThe `dateAfter` rule checks that the supplied field is a valid date after a specified date.\n```php\n$v-\u003erule('dateAfter', 'created_at', '2018-10-13');\n```\n\nAlternate syntax.\n```php\n$v = new Valitron\\Validator(['created_at' =\u003e '2018-09-01']);\n$v-\u003erules([\n    'dateAfter' =\u003e [\n        ['created_at', '2018-01-01']\n    ]\n]);\n$v-\u003evalidate();\n```\n\n## contains fields usage\nThe `contains` rule checks that a given string exists within the field and checks that the field and the search value are both valid strings.\n```php\n$v-\u003erule('contains', 'username', 'man');\n```\n\nAlternate syntax.\n```php\n$v = new Valitron\\Validator(['username' =\u003e 'Batman123']);\n$v-\u003erules([\n    'contains' =\u003e [\n        ['username', 'man']\n    ]\n]);\n$v-\u003evalidate();\n```\n\n*Note* You can use the optional strict flag to ensure a case-sensitive match.\nThe following example will return true:\n```php\n$v = new Valitron\\Validator(['username' =\u003e 'Batman123']);\n$v-\u003erules([\n    'contains' =\u003e [\n        ['username', 'man']\n    ]\n]);\n$v-\u003evalidate();\n```\nWhereas, this would return false, as the M in the search string is not uppercase in the provided value:\n```php\n$v = new Valitron\\Validator(['username' =\u003e 'Batman123']);\n$v-\u003erules([\n    'contains' =\u003e [\n        ['username', 'Man', true]\n    ]\n]);\n$v-\u003evalidate();\n```\n\n## subset fields usage\nThe `subset` rule checks that the field is either a scalar or array field and that all of it's values are contained within a given set of values.\n```php\n$v-\u003erule('subset', 'colors', ['green', 'blue', 'orange']);\n```\n\nAlternate syntax.\n```php\n$v = new Valitron\\Validator(['colors' =\u003e ['green', 'blue']]);\n$v-\u003erules([\n    'subset' =\u003e [\n        ['colors', ['orange', 'green', 'blue', 'red']]\n    ]\n]);\n$v-\u003evalidate();\n```\nThis example would return false, as the provided color, purple, does not exist in the array of accepted values we're providing.\n```php\n$v = new Valitron\\Validator(['colors' =\u003e ['purple', 'blue']]);\n$v-\u003erules([\n    'subset' =\u003e [\n        ['colors', ['orange', 'green', 'blue', 'red']]\n    ]\n]);\n$v-\u003evalidate();\n```\n\n## containsUnique fields usage\nThe `containsUnique` rule checks that the provided field is an array and that all values contained within are unique, i.e. no duplicate values in the array.\n```php\n$v-\u003erule('containsUnique', 'colors');\n```\n\nAlternate syntax.\n```php\n$v = new Valitron\\Validator(['colors' =\u003e ['purple', 'blue']]);\n$v-\u003erules([\n    'containsUnique' =\u003e [\n        ['colors']\n    ]\n]);\n$v-\u003evalidate();\n```\nThis example would return false, as the values in the provided array are duplicates.\n```php\n$v = new Valitron\\Validator(['colors' =\u003e ['purple', 'purple']]);\n$v-\u003erules([\n    'containsUnique' =\u003e [\n        ['colors']\n    ]\n]);\n$v-\u003evalidate();\n```\n\n## Credit Card Validation usage\n\nCredit card validation currently allows you to validate a Visa `visa`,\nMastercard `mastercard`, Dinersclub `dinersclub`, American Express `amex`\nor Discover `discover`\n\nThis will check the credit card against each card type\n\n```php\n$v-\u003erule('creditCard', 'credit_card');\n```\n\nTo optionally filter card types, add the slug to an array as the next parameter:\n\n```php\n$v-\u003erule('creditCard', 'credit_card', ['visa', 'mastercard']);\n```\n\nIf you only want to validate one type of card, put it as a string:\n\n```php\n$v-\u003erule('creditCard', 'credit_card', 'visa');\n```\n\nIf the card type information is coming from the client, you might also want to\nstill specify an array of valid card types:\n\n```php\n$cardType = 'amex';\n$v-\u003erule('creditCard', 'credit_card', $cardType, ['visa', 'mastercard']);\n$v-\u003evalidate(); // false\n```\n\n## instanceOf fields usage\nThe `instanceOf` rule checks that the field is an instance of a given class.\n```php\n$v-\u003erule('instanceOf', 'date', \\DateTime);\n```\n\nAlternate syntax.\n```php\n$v = new Valitron\\Validator(['date' =\u003e new \\DateTime()]);\n$v-\u003erules([\n    'instanceOf' =\u003e [\n        ['date', 'DateTime']\n    ]\n]);\n$v-\u003evalidate();\n```\n*Note* You can also compare the value against a given object as opposed to the string class name.\nThis example would also return true:\n```php\n$v = new Valitron\\Validator(['date' =\u003e new \\DateTime()]);\n$existingDateObject = new \\DateTime();\n$v-\u003erules([\n    'instanceOf' =\u003e [\n        ['date', $existingDateObject]\n    ]\n]);\n$v-\u003evalidate();\n```\n\n## optional fields usage\nThe `optional` rule ensures that if the field is present in the data set that it passes all validation rules.\n```php\n$v-\u003erule('optional', 'username');\n```\n\nAlternate syntax.\nThis example would return true either when the 'username' field is not present or in the case where the username is only alphabetic characters.\n```php\n$v = new Valitron\\Validator(['username' =\u003e 'batman']);\n$v-\u003erules([\n    'alpha' =\u003e [\n        ['username']\n    ],\n    'optional' =\u003e [\n        ['username']\n    ]\n]);\n$v-\u003evalidate();\n```\nThis example would return false, as although the field is optional, since it is provided it must pass all the validation rules, which in this case it does not.\n```php\n$v = new Valitron\\Validator(['username' =\u003e 'batman123']);\n$v-\u003erules([\n    'alpha' =\u003e [\n        ['username']\n    ],\n    'optional' =\u003e [\n        ['username']\n    ]\n]);\n$v-\u003evalidate();\n```\n\n## arrayHasKeys fields usage\n\nThe `arrayHasKeys` rule ensures that the field is an array and that it contains all the specified keys.\nReturns false if the field is not an array or if no required keys are specified or if some key is missing.\n\n```php\n$v = new Valitron\\Validator([\n    'address' =\u003e [\n        'name' =\u003e 'Jane Doe',\n        'street' =\u003e 'Doe Square',\n        'city' =\u003e 'Doe D.C.'\n    ]\n]);\n$v-\u003erule('arrayHasKeys', 'address', ['name', 'street', 'city']);\n$v-\u003evalidate();\n```\n\n## Adding Custom Validation Rules\n\nTo add your own validation rule, use the `addRule` method with a rule\nname, a custom callback or closure, and a error message to display in\ncase of an error. The callback provided should return boolean true or\nfalse.\n\n```php\nValitron\\Validator::addRule('alwaysFail', function($field, $value, array $params, array $fields) {\n    return false;\n}, 'Everything you do is wrong. You fail.');\n```\n\nYou can also use one-off rules that are only valid for the specified\nfields.\n\n```php\n$v = new Valitron\\Validator(array(\"foo\" =\u003e \"bar\"));\n$v-\u003erule(function($field, $value, $params, $fields) {\n    return true;\n}, \"foo\")-\u003emessage(\"{field} failed...\");\n```\n\nThis is useful because such rules can have access to variables\ndefined in the scope where the `Validator` lives. The Closure's\nsignature is identical to `Validator::addRule` callback's\nsignature.\n\nIf you wish to add your own rules that are not static (i.e.,\nyour rule is not static and available to call `Validator`\ninstances), you need to use `Validator::addInstanceRule`.\nThis rule will take the same parameters as\n`Validator::addRule` but it has to be called on a `Validator`\ninstance.\n\n## Chaining rules\n\nYou can chain multiple rules together using the following syntax.\n```php\n$v = new Valitron\\Validator(['email_address' =\u003e 'test@test.com']);\n$v-\u003erule('required', 'email_address')-\u003erule('email', 'email_address');\n$v-\u003evalidate();\n```\n\n## Alternate syntax for adding rules\n\nAs the number of rules grows, you may prefer the alternate syntax\nfor defining multiple rules at once.\n\n```php\n$rules = [\n    'required' =\u003e 'foo',\n    'accepted' =\u003e 'bar',\n    'integer' =\u003e  'bar'\n];\n\n$v = new Valitron\\Validator(array('foo' =\u003e 'bar', 'bar' =\u003e 1));\n$v-\u003erules($rules);\n$v-\u003evalidate();\n```\n\nIf your rule requires multiple parameters or a single parameter\nmore complex than a string, you need to wrap the rule in an array.\n\n```php\n$rules = [\n    'required' =\u003e [\n        ['foo'],\n        ['bar']\n    ],\n    'length' =\u003e [\n        ['foo', 3]\n    ]\n];\n```\nYou can also specify multiple rules for each rule type.\n\n```php\n$rules = [\n    'length'   =\u003e [\n        ['foo', 5],\n        ['bar', 5]\n    ]\n];\n```\n\nPutting these techniques together, you can create a complete\nrule definition in a relatively compact data structure.\n\nYou can continue to add individual rules with the `rule` method\neven after specifying a rule definition via an array. This is\nespecially useful if you are defining custom validation rules.\n\n```php\n$rules = [\n    'required' =\u003e 'foo',\n    'accepted' =\u003e 'bar',\n    'integer' =\u003e  'bar'\n];\n\n$v = new Valitron\\Validator(array('foo' =\u003e 'bar', 'bar' =\u003e 1));\n$v-\u003erules($rules);\n$v-\u003erule('min', 'bar', 0);\n$v-\u003evalidate();\n```\n\nYou can also add rules on a per-field basis:\n```php\n$rules = [\n    'required',\n    ['lengthMin', 4]\n];\n\n$v = new Valitron\\Validator(array('foo' =\u003e 'bar'));\n$v-\u003emapFieldRules('foo', $rules);\n$v-\u003evalidate();\n```\n\nOr for multiple fields at once:\n\n```php\n$rules = [\n    'foo' =\u003e ['required', 'integer'],\n    'bar'=\u003e['email', ['lengthMin', 4]]\n];\n\n$v = new Valitron\\Validator(array('foo' =\u003e 'bar', 'bar' =\u003e 'mail@example.com));\n$v-\u003emapFieldsRules($rules);\n$v-\u003evalidate();\n```\n\n## Adding field label to messages\n\nYou can do this in two different ways, you can add a individual label to a rule or an array of all labels for the rules.\n\nTo add individual label to rule you simply add the `label` method after the rule.\n\n```php\n$v = new Valitron\\Validator(array());\n$v-\u003erule('required', 'name')-\u003emessage('{field} is required')-\u003elabel('Name');\n$v-\u003evalidate();\n```\n\nThere is a edge case to this method, you wouldn't be able to use a array of field names in the rule definition, so one rule per field. So this wouldn't work:\n\n```php\n$v = new Valitron\\Validator(array());\n$v-\u003erule('required', array('name', 'email'))-\u003emessage('{field} is required')-\u003elabel('Name');\n$v-\u003evalidate();\n```\n\nHowever we can use a array of labels to solve this issue by simply adding the `labels` method instead:\n\n```php\n$v = new Valitron\\Validator(array());\n$v-\u003erule('required', array('name', 'email'))-\u003emessage('{field} is required');\n$v-\u003elabels(array(\n    'name' =\u003e 'Name',\n    'email' =\u003e 'Email address'\n));\n$v-\u003evalidate();\n```\n\nThis introduces a new set of tags to your error language file which looks like `{field}`, if you are using a rule like `equals` you can access the second value in the language file by incrementing the field with a value like `{field1}`.\n\n\n## Re-use of validation rules\n\nYou can re-use your validation rules to quickly validate different data with the same rules by using the withData method:\n\n```php\n$v = new Valitron\\Validator(array());\n$v-\u003erule('required', 'name')-\u003emessage('{field} is required');\n$v-\u003evalidate(); //false\n\n$v2 = $v-\u003ewithData(array('name'=\u003e'example'));\n$v2-\u003evalidate(); //true\n```\n\n## Running Tests\n\nThe test suite depends on the Composer autoloader to load and run the\nValitron files. Please ensure you have downloaded and installed Composer\nbefore running the tests:\n\n1. Download Composer `curl -s http://getcomposer.org/installer | php`\n2. Run 'install' `php composer.phar install`\n3. Run the tests `phpunit`\n\n## Contributing\n\n1. Fork it\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Make your changes\n4. Run the tests, adding new ones for your own code if necessary (`phpunit`)\n5. Commit your changes (`git commit -am 'Added some feature'`)\n6. Push to the branch (`git push origin my-new-feature`)\n7. Create new Pull Request\n8. Pat yourself on the back for being so awesome\n\n## Security Disclosures and Contact Information\n\nTo report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure.\n","funding_links":["https://tidelift.com/funding/github/packagist/vlucas/valitron","https://tidelift.com/subscription/pkg/packagist-vlucas-valitron?utm_source=packagist-vlucas-valitron\u0026utm_medium=referral\u0026utm_campaign=readme","https://tidelift.com/security"],"categories":["Uncategorized","Filtering and Validation","过滤和验证","PHP","目录","Table of Contents","过滤和验证 Filtering and Validation","过滤和验证( Filtering ang Validation )","类库"],"sub_categories":["Uncategorized","过滤和验证 Filtering and Validation","Filtering and Validation","Filtering, Sanitizing and Validation","表单校验"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvlucas%2Fvalitron","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvlucas%2Fvalitron","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvlucas%2Fvalitron/lists"}