{"id":27245626,"url":"https://github.com/ignitekit/validation","last_synced_at":"2025-07-07T05:37:37.533Z","repository":{"id":41542549,"uuid":"275017342","full_name":"IgniteKit/validation","owner":"IgniteKit","description":"Laravel inspired Validation package for PHP","archived":false,"fork":false,"pushed_at":"2022-07-03T21:28:35.000Z","size":386,"stargazers_count":1,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-30T13:37:00.915Z","etag":null,"topics":["php","php-validation","validation"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/IgniteKit.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-06-25T21:20:03.000Z","updated_at":"2022-07-03T20:44:16.000Z","dependencies_parsed_at":"2022-09-06T09:21:20.889Z","dependency_job_id":null,"html_url":"https://github.com/IgniteKit/validation","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/IgniteKit%2Fvalidation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/IgniteKit%2Fvalidation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/IgniteKit%2Fvalidation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/IgniteKit%2Fvalidation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/IgniteKit","download_url":"https://codeload.github.com/IgniteKit/validation/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248300756,"owners_count":21080774,"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":["php","php-validation","validation"],"created_at":"2025-04-10T21:28:37.431Z","updated_at":"2025-04-10T21:28:38.470Z","avatar_url":"https://github.com/IgniteKit.png","language":"PHP","readme":"Validation - PHP Standalone Validation Library\n======================================================\n\n[![Build Status](https://img.shields.io/travis/ignitekit/wp-validation.svg?style=flat-square)](https://travis-ci.org/ignitekit/validation)\n[![License](http://img.shields.io/:license-mit-blue.svg?style=flat-square)](http://doge.mit-license.org)\n\n\nPHP Standalone library for validating data inspired by Laravel Validation\n\nThis is a fork of the original library written by [Rakit](https://github.com/rakit) with some more improvements and changes.\n\n## Features\n\n* API like Laravel validation.\n* Array validation.\n* `$_FILES` validation with multiple file support.\n* Custom attribute aliases.\n* Custom validation messages.\n* Custom rule.\n\n## Requirements\n\n* PHP 7.0 or higher\n* Composer for installation\n\n## Quick Start\n\n#### Installation\n\n```\ncomposer require ignitekit/validation\n```\n\n#### Usage\n\nThere are two ways to validating data with this library. Using `make` to make validation object, \nthen validate it using `validate`. Or just use `validate`. \nExamples:\n\nUsing `make`:\n\n```php\n\u003c?php\n\nrequire('vendor/autoload.php');\n\nuse IgniteKit\\Validation\\Validator;\n\n$validator = new Validator;\n\n// make it\n$validation = $validator-\u003emake($_POST + $_FILES, [\n    'name'                  =\u003e 'required',\n    'email'                 =\u003e 'required|email',\n    'password'              =\u003e 'required|min:6',\n    'confirm_password'      =\u003e 'required|same:password',\n    'avatar'                =\u003e 'required|uploaded_file:0,500K,png,jpeg',\n    'skills'                =\u003e 'array',\n    'skills.*.id'           =\u003e 'required|numeric',\n    'skills.*.percentage'   =\u003e 'required|numeric'\n]);\n\n// then validate\n$validation-\u003evalidate();\n\nif ($validation-\u003efails()) {\n    // handling errors\n    $errors = $validation-\u003eerrors();\n    echo \"\u003cpre\u003e\";\n    print_r($errors-\u003efirstOfAll());\n    echo \"\u003c/pre\u003e\";\n    exit;\n} else {\n    // validation passes\n    echo \"Success!\";\n}\n\n```\n\nor just `validate` it:\n\n```php\n\u003c?php\n\nrequire('vendor/autoload.php');\n\nuse IgniteKit\\Validation\\Validator;\n\n$validator = new Validator;\n\n$validation = $validator-\u003evalidate($_POST + $_FILES, [\n    'name'                  =\u003e 'required',\n    'email'                 =\u003e 'required|email',\n    'password'              =\u003e 'required|min:6',\n    'confirm_password'      =\u003e 'required|same:password',\n    'avatar'                =\u003e 'required|uploaded_file:0,500K,png,jpeg',\n    'skills'                =\u003e 'array',\n    'skills.*.id'           =\u003e 'required|numeric',\n    'skills.*.percentage'   =\u003e 'required|numeric'\n]);\n\nif ($validation-\u003efails()) {\n\t// handling errors\n\t$errors = $validation-\u003eerrors();\n\techo \"\u003cpre\u003e\";\n\tprint_r($errors-\u003efirstOfAll());\n\techo \"\u003c/pre\u003e\";\n\texit;\n} else {\n\t// validation passes\n\techo \"Success!\";\n}\n\n```\n\nIn this case, 2 examples above will output the same results. \n\nBut with `make` you can setup something like custom invalid message, custom attribute alias, etc before validation running.\n\n### Attribute Alias\n\nBy default we will transform your attribute into more readable text. For example `confirm_password` will be displayed as `Confirm password`.\nBut you can set it anything you want with `setAlias` or `setAliases` method.\n\nExample:\n\n```php\n$validator = new Validator;\n\n// To set attribute alias, you should use `make` instead `validate`.\n$validation-\u003emake([\n\t'province_id' =\u003e $_POST['province_id'],\n\t'district_id' =\u003e $_POST['district_id']\n], [\n\t'province_id' =\u003e 'required|numeric',\n\t'district_id' =\u003e 'required|numeric'\n]);\n\n// now you can set aliases using this way:\n$validation-\u003esetAlias('province_id', 'Province');\n$validation-\u003esetAlias('district_id', 'District');\n\n// or this way:\n$validation-\u003esetAliases([\n\t'province_id' =\u003e 'Province',\n\t'district_id' =\u003e 'District'\n]);\n\n// then validate it\n$validation-\u003evalidate();\n\n```\n\nNow if `province_id` value is empty, error message would be 'Province is required'.\n\n## Custom Validation Message\n\nBefore register/set custom messages, here are some variables you can use in your custom messages:\n\n* `:attribute`: will replaced into attribute alias.\n* `:value`: will replaced into stringify value of attribute. For array and object will replaced to json.\n\nAnd also there are several message variables depends on their rules.\n\nHere are some ways to register/set your custom message(s):\n\n#### Custom Messages for Validator\n\nWith this way, anytime you make validation using `make` or `validate` it will set your custom messages for it.\nIt is useful for localization.\n\nTo do this, you can set custom messages as first argument constructor like this:\n\n```php\n$validator = new Validator([\n\t'required' =\u003e ':attribute harus diisi',\n\t'email' =\u003e ':email tidak valid',\n\t// etc\n]);\n\n// then validation belows will use those custom messages\n$validation_a = $validator-\u003evalidate($dataset_a, $rules_for_a);\n$validation_b = $validator-\u003evalidate($dataset_b, $rules_for_b);\n\n```\n\nOr using `setMessages` method like this:\n\n```php\n$validator = new Validator;\n$validator-\u003esetMessages([\n\t'required' =\u003e ':attribute harus diisi',\n\t'email' =\u003e ':email tidak valid',\n\t// etc\n]);\n\n// now validation belows will use those custom messages\n$validation_a = $validator-\u003evalidate($dataset_a, $rules_for_dataset_a);\n$validation_b = $validator-\u003evalidate($dataset_b, $rules_for_dataset_b);\n\n```\n\n#### Custom Messages for Validation\n\nSometimes you may want to set custom messages for specific validation.\nTo do this you can set your custom messages as 3rd argument of `$validator-\u003emake` or `$validator-\u003evalidate` like this:\n\n```php\n$validator = new Validator;\n\n$validation_a = $validator-\u003evalidate($dataset_a, $rules_for_dataset_a, [\n\t'required' =\u003e ':attribute harus diisi',\n\t'email' =\u003e ':email tidak valid',\n\t// etc\n]);\n\n```\n\nOr you can use `$validation-\u003esetMessages` like this:\n\n```php\n$validator = new Validator;\n\n$validation_a = $validator-\u003emake($dataset_a, $rules_for_dataset_a);\n$validation_a-\u003esetMessages([\n\t'required' =\u003e ':attribute harus diisi',\n\t'email' =\u003e ':email tidak valid',\n\t// etc\n]);\n\n...\n\n$validation_a-\u003evalidate();\n```\n\n#### Custom Message for Specific Attribute Rule\n\nSometimes you may want to set custom message for specific rule attribute. \nTo do this you can use `:` as message separator or using chaining methods.\n\nExamples:\n\n```php\n$validator = new Validator;\n\n$validation_a = $validator-\u003emake($dataset_a, [\n\t'age' =\u003e 'required|min:18'\n]);\n\n$validation_a-\u003esetMessages([\n\t'age:min' =\u003e '18+ only',\n]);\n\n$validation_a-\u003evalidate();\n```\n\nOr using chaining methods:\n\n```php\n$validator = new Validator;\n\n$validation_a = $validator-\u003emake($dataset_a, [\n\t'photo' =\u003e [\n\t\t'required',\n\t\t$validator('uploaded_file')-\u003efileTypes('jpeg|png')-\u003emessage('Photo must be jpeg/png image')\n\t]\n]);\n\n$validation_a-\u003evalidate();\n```\n\n## Translation\n\nTranslation is different with custom messages. \nTranslation may needed when you use custom message for rule `in`, `not_in`, `mimes`, and `uploaded_file`.\n\nFor example if you use rule `in:1,2,3` we will set invalid message like \"The Attribute only allows '1', '2', or '3'\" \nwhere part \"'1', '2', or '3'\" is comes from \":allowed_values\" tag.\nSo if you have custom Indonesian message \":attribute hanya memperbolehkan :allowed_values\", \nwe will set invalid message like \"Attribute hanya memperbolehkan '1', '2', or '3'\" which is the \"or\" word is not part of Indonesian language.\n\nSo, to solve this problem, we can use translation like this:\n\n```php\n// Set translation for words 'and' and 'or'.\n$validator-\u003esetTranslations([\n    'and' =\u003e 'dan',\n    'or' =\u003e 'atau'\n]);\n\n// Set custom message for 'in' rule\n$validator-\u003esetMessage('in', \":attribute hanya memperbolehkan :allowed_values\");\n\n// Validate\n$validation = $validator-\u003evalidate($inputs, [\n    'nomor' =\u003e 'in:1,2,3'\n]);\n\n$message = $validation-\u003eerrors()-\u003efirst('nomor'); // \"Nomor hanya memperbolehkan '1', '2', atau '3'\" \n```\n\n\u003e Actually, our built-in rules only use words 'and' and 'or' that you may need to translates.\n\n## Working with Error Message\n\nErrors messages are collected in `IgniteKit\\Validation\\ErrorBag` object that you can get it using `errors()` method.\n\n```php\n$validation = $validator-\u003evalidate($inputs, $rules);\n\n$errors = $validation-\u003eerrors(); // \u003c\u003c ErrorBag\n```\n\nNow you can use methods below to retrieves errors messages:\n\n#### `all(string $format = ':message')`\n\nGet all messages as flatten array.\n\nExamples:\n\n```php\n$messages = $errors-\u003eall(); \n// [\n//     'Email is not valid email',\n//     'Password minimum 6 character',\n//     'Password must contains capital letters'\n// ]\n\n$messages = $errors-\u003eall('\u003cli\u003e:message\u003c/li\u003e');\n// [\n//     '\u003cli\u003eEmail is not valid email\u003c/li\u003e',\n//     '\u003cli\u003ePassword minimum 6 character\u003c/li\u003e',\n//     '\u003cli\u003ePassword must contains capital letters\u003c/li\u003e'\n// ]\n```\n\n#### `firstOfAll(string $format = ':message', bool $dotNotation = false)`\n\nGet only first message from all existing keys.\n\nExamples:\n\n```php\n$messages = $errors-\u003efirstOfAll(); \n// [\n//     'email' =\u003e Email is not valid email',\n//     'password' =\u003e 'Password minimum 6 character',\n// ]\n\n$messages = $errors-\u003efirstOfAll('\u003cli\u003e:message\u003c/li\u003e');\n// [\n//     'email' =\u003e '\u003cli\u003eEmail is not valid email\u003c/li\u003e',\n//     'password' =\u003e '\u003cli\u003ePassword minimum 6 character\u003c/li\u003e',\n// ]\n```\n\nArgument `$dotNotation` is for array validation. \nIf it is `false` it will return original array structure, if it `true` it will return flatten array with dot notation keys.\n\nFor example:\n\n```php\n$messages = $errors-\u003efirstOfAll(':message', false); \n// [\n//     'contacts' =\u003e [\n//          1 =\u003e [\n//              'email' =\u003e 'Email is not valid email',\n//              'phone' =\u003e 'Phone is not valid phone number'\n//          ],\n//     ],\n// ]\n\n$messages = $errors-\u003efirstOfAll(':message', true);\n// [\n//     'contacts.1.email' =\u003e 'Email is not valid email',\n//     'contacts.1.phone' =\u003e 'Email is not valid phone number',\n// ]\n```\n\n#### `first(string $key)`\n\nGet first message from given key. It will return `string` if key has any error message, or `null` if key has no errors.\n\nFor example:\n\n```php\nif ($emailError = $errors-\u003efirst('email')) {\n    echo $emailError;\n}\n```\n\n#### `toArray()`\n\nGet all messages grouped by it's keys.\n\nFor example:\n\n```php\n$messages = $errors-\u003etoArray();\n// [\n//     'email' =\u003e [\n//         'Email is not valid email'\n//     ],\n//     'password' =\u003e [\n//         'Password minimum 6 character',\n//         'Password must contains capital letters'\n//     ]\n// ]\n```\n\n#### `count()`\n\nGet count messages.\n\n#### `has(string $key)`\n\nCheck if given key has an error. It returns `bool` if a key has an error, and otherwise.\n\n\n## Getting Validated, Valid, and Invalid Data\n\nFor example you have validation like this:\n\n```php\n$validation = $validator-\u003evalidate([\n    'title' =\u003e 'Lorem Ipsum',\n    'body' =\u003e 'Lorem ipsum dolor sit amet ...',\n    'published' =\u003e null,\n    'something' =\u003e '-invalid-'\n], [\n    'title' =\u003e 'required',\n    'body' =\u003e 'required',\n    'published' =\u003e 'default:1|required|in:0,1',\n    'something' =\u003e 'required|numeric'\n]);\n```\n\nYou can get validated data, valid data, or invalid data using methods in example below:\n\n```php\n$validatedData = $validation-\u003egetValidatedData();\n// [\n//     'title' =\u003e 'Lorem Ipsum',\n//     'body' =\u003e 'Lorem ipsum dolor sit amet ...',\n//     'published' =\u003e '1' // notice this\n//     'something' =\u003e '-invalid-'\n// ]\n\n$validData = $validation-\u003egetValidData();\n// [\n//     'title' =\u003e 'Lorem Ipsum',\n//     'body' =\u003e 'Lorem ipsum dolor sit amet ...',\n//     'published' =\u003e '1'\n// ]\n\n$invalidData = $validation-\u003egetInvalidData();\n// [\n//     'something' =\u003e '-invalid-'\n// ]\n```\n\n## Available Rules\n\n\u003e Click to show details.\n\n\u003cdetails\u003e\u003csummary\u003e\u003cstrong\u003erequired\u003c/strong\u003e\u003c/summary\u003e\n\nThe field under this validation must be present and not 'empty'.\n\nHere are some examples:\n\n| Value         | Valid |\n|---------------|-------|\n| `'something'` | true  |\n| `'0'`         | true  |\n| `0`           | true  |\n| `[0]`         | true  |\n| `[null]`      | true  |\n| null          | false |\n| []            | false |\n| ''            | false |\n\nFor uploaded file, `$_FILES['key']['error']` must not `UPLOAD_ERR_NO_FILE`.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cstrong\u003erequired_if\u003c/strong\u003e:another_field,value_1,value_2,...\u003c/summary\u003e\n\nThe field under this rule must be present and not empty if the anotherfield field is equal to any value.\n\nFor example `required_if:something,1,yes,on` will be required if `something` value is one of `1`, `'1'`, `'yes'`, or `'on'`.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cstrong\u003erequired_unless\u003c/strong\u003e:another_field,value_1,value_2,...\u003c/summary\u003e\n\nThe field under validation must be present and not empty unless the anotherfield field is equal to any value.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cstrong\u003erequired_with\u003c/strong\u003e:field_1,field_2,...\u003c/summary\u003e\n\nThe field under validation must be present and not empty only if any of the other specified fields are present.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cstrong\u003erequired_without\u003c/strong\u003e:field_1,field_2,...\u003c/summary\u003e\n\nThe field under validation must be present and not empty only when any of the other specified fields are not present.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cstrong\u003erequired_with_all\u003c/strong\u003e:field_1,field_2,...\u003c/summary\u003e\n\nThe field under validation must be present and not empty only if all of the other specified fields are present.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cstrong\u003erequired_without_all\u003c/strong\u003e:field_1,field_2,...\u003c/summary\u003e\n\nThe field under validation must be present and not empty only when all of the other specified fields are not present.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cstrong\u003euploaded_file\u003c/strong\u003e:min_size,max_size,extension_a,extension_b,...\u003c/summary\u003e\n\nThis rule will validate data from `$_FILES`. \nField under this rule must be follows rules below to be valid:\n\n* `$_FILES['key']['error']` must be `UPLOAD_ERR_OK` or `UPLOAD_ERR_NO_FILE`. For `UPLOAD_ERR_NO_FILE` you can validate it with `required` rule. \n* If min size is given, uploaded file size **MUST NOT** be lower than min size.\n* If max size is given, uploaded file size **MUST NOT** be higher than max size.\n* If file types is given, mime type must be one of those given types.\n\nHere are some example definitions and explanations:\n\n* `uploaded_file`: uploaded file is optional. When it is not empty, it must be `ERR_UPLOAD_OK`. \n* `required|uploaded_file`: uploaded file is required, and it must be `ERR_UPLOAD_OK`. \n* `uploaded_file:0,1M`: uploaded file size must be between 0 - 1 MB, but uploaded file is optional.\n* `required|uploaded_file:0,1M,png,jpeg`: uploaded file size must be between 0 - 1MB and mime types must be `image/jpeg` or `image/png`.\n\nOptionally, if you want to have separate error message between size and type validation.\nYou can use `mimes` rule to validate file types, and `min`, `max`, or `between` to validate it's size.\n\nFor multiple file upload, PHP will give you undesirable array `$_FILES` structure ([here](http://php.net/manual/en/features.file-upload.multiple.php#53240) is the topic). So we make `uploaded_file` rule to automatically resolve your `$_FILES` value to be well-organized array structure. That means, you cannot only use `min`, `max`, `between`, or `mimes` rules to validate multiple file upload. You should put `uploaded_file` just to resolve it's value and make sure that value is correct uploaded file value.\n\nFor example if you have input files like this:\n\n```html\n\u003cinput type=\"file\" name=\"photos[]\"/\u003e\n\u003cinput type=\"file\" name=\"photos[]\"/\u003e\n\u003cinput type=\"file\" name=\"photos[]\"/\u003e\n```\n\nYou can  simply validate it like this:\n\n```php\n$validation = $validator-\u003evalidate($_FILES, [\n    'photos.*' =\u003e 'uploaded_file:0,2M,jpeg,png'\n]);\n\n// or\n\n$validation = $validator-\u003evalidate($_FILES, [\n    'photos.*' =\u003e 'uploaded_file|max:2M|mimes:jpeg,png'\n]);\n```\n\nOr if you have input files like this:\n\n```html\n\u003cinput type=\"file\" name=\"images[profile]\"/\u003e\n\u003cinput type=\"file\" name=\"images[cover]\"/\u003e\n```\n\nYou can validate it like this:\n\n```php\n$validation = $validator-\u003evalidate($_FILES, [\n    'images.*' =\u003e 'uploaded_file|max:2M|mimes:jpeg,png',\n]);\n\n// or\n\n$validation = $validator-\u003evalidate($_FILES, [\n    'images.profile' =\u003e 'uploaded_file|max:2M|mimes:jpeg,png',\n    'images.cover' =\u003e 'uploaded_file|max:5M|mimes:jpeg,png',\n]);\n```\n\nNow when you use `getValidData()` or `getInvalidData()` you will get well array structure just like single file upload.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cstrong\u003emimes\u003c/strong\u003e:extension_a,extension_b,...\u003c/summary\u003e\n\nThe `$_FILES` item under validation must have a MIME type corresponding to one of the listed extensions.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cstrong\u003edefault/defaults\u003c/strong\u003e\u003c/summary\u003e\n\nThis is special rule that doesn't validate anything. \nIt just set default value to your attribute if that attribute is empty or not present.\n\nFor example if you have validation like this\n\n```php\n$validation = $validator-\u003evalidate([\n    'enabled' =\u003e null\n], [\n    'enabled' =\u003e 'default:1|required|in:0,1'\n    'published' =\u003e 'default:0|required|in:0,1'\n]);\n\n$validation-\u003epasses(); // true\n\n// Get the valid/default data\n$valid_data = $validation-\u003egetValidData();\n$enabled = $valid_data['enabled'];\n$published = $valid_data['published'];\n\n```\n\nValidation passes because we sets default value for `enabled` and `published` to `1` and `0` which is valid.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cstrong\u003eemail\u003c/strong\u003e\u003c/summary\u003e\n\nThe field under this validation must be valid email address.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cstrong\u003euppercase\u003c/strong\u003e\u003c/summary\u003e\n\nThe field under this validation must be valid uppercase.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cstrong\u003elowercase\u003c/strong\u003e\u003c/summary\u003e\n\nThe field under this validation must be valid lowercase.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cstrong\u003ejson\u003c/strong\u003e\u003c/summary\u003e\n\nThe field under this validation must be valid JSON string.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cstrong\u003ealpha\u003c/strong\u003e\u003c/summary\u003e\n\nThe field under this rule must be entirely alphabetic characters.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cstrong\u003enumeric\u003c/strong\u003e\u003c/summary\u003e\n\nThe field under this rule must be numeric.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cstrong\u003ealpha_num\u003c/strong\u003e\u003c/summary\u003e\n\nThe field under this rule must be entirely alpha-numeric characters.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cstrong\u003ealpha_dash\u003c/strong\u003e\u003c/summary\u003e\n\nThe field under this rule may have alpha-numeric characters, as well as dashes and underscores.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cstrong\u003ealpha_spaces\u003c/strong\u003e\u003c/summary\u003e\n\nThe field under this rule may have alpha characters, as well as spaces.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cstrong\u003ein\u003c/strong\u003e:value_1,value_2,...\u003c/summary\u003e\n\nThe field under this rule must be included in the given list of values.\n\nThis rule is using `in_array` to check the value. \nBy default `in_array` disable strict checking. \nSo it doesn't check data type.\nIf you want enable strict checking, you can invoke validator like this:\n\n```php\n$validation = $validator-\u003evalidate($data, [\n    'enabled' =\u003e [\n        'required', \n        $validator('in', [true, 1])-\u003estrict()\n    ]\n]);\n```\n\nThen 'enabled' value should be boolean `true`, or int `1`.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cstrong\u003enot_in\u003c/strong\u003e:value_1,value_2,...\u003c/summary\u003e\n\nThe field under this rule must not be included in the given list of values.\n\nThis rule also using `in_array`. You can enable strict checking by invoking validator and call `strict()` like example in rule `in` above.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cstrong\u003emin\u003c/strong\u003e:number\u003c/summary\u003e\n\nThe field under this rule must have a size greater or equal than the given number. \n\nFor string value, size corresponds to the number of characters. For integer or float value, size corresponds to its numerical value. For an array, size corresponds to the count of the array. If your value is numeric string, you can put `numeric` rule to treat its size by numeric value instead of number of characters.\n\nYou can also validate uploaded file using this rule to validate minimum size of uploaded file.\nFor example:\n\n```php\n$validation = $validator-\u003evalidate([\n    'photo' =\u003e $_FILES['photo']\n], [\n    'photo' =\u003e 'required|min:1M'\n]);\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cstrong\u003emax\u003c/strong\u003e:number\u003c/summary\u003e\n\nThe field under this rule must have a size lower or equal than the given number. \nValue size calculated in same way like `min` rule.\n\nYou can also validate uploaded file using this rule to validate maximum size of uploaded file.\nFor example:\n\n```php\n$validation = $validator-\u003evalidate([\n    'photo' =\u003e $_FILES['photo']\n], [\n    'photo' =\u003e 'required|max:2M'\n]);\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cstrong\u003ebetween\u003c/strong\u003e:min,max\u003c/summary\u003e\n\nThe field under this rule must have a size between min and max params. \nValue size calculated in same way like `min` and `max` rule.\n\nYou can also validate uploaded file using this rule to validate size of uploaded file.\nFor example:\n\n```php\n$validation = $validator-\u003evalidate([\n    'photo' =\u003e $_FILES['photo']\n], [\n    'photo' =\u003e 'required|between:1M,2M'\n]);\n```\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cstrong\u003edigits\u003c/strong\u003e:value\u003c/summary\u003e\n\nThe field under validation must be numeric and must have an exact length of `value`.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cstrong\u003edigits_between\u003c/strong\u003e:min,max\u003c/summary\u003e\n\nThe field under validation must have a length between the given `min` and `max`.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cstrong\u003eurl\u003c/strong\u003e\u003c/summary\u003e\n\nThe field under this rule must be valid url format.\nBy default it check common URL scheme format like `any_scheme://...`.\nBut you can specify URL schemes if you want.\n\nFor example:\n\n```php\n$validation = $validator-\u003evalidate($inputs, [\n    'random_url' =\u003e 'url',          // value can be `any_scheme://...`\n    'https_url' =\u003e 'url:http',      // value must be started with `https://`\n    'http_url' =\u003e 'url:http,https', // value must be started with `http://` or `https://`\n    'ftp_url' =\u003e 'url:ftp',         // value must be started with `ftp://`\n    'custom_url' =\u003e 'url:custom',   // value must be started with `custom://`\n    'mailto_url' =\u003e 'url:mailto',   // value must conatin valid mailto URL scheme like `mailto:a@mail.com,b@mail.com`\n    'jdbc_url' =\u003e 'url:jdbc',       // value must contain valid jdbc URL scheme like `jdbc:mysql://localhost/dbname`\n]);\n```\n\n\u003e For common URL scheme and mailto, we combine `FILTER_VALIDATE_URL` to validate URL format and `preg_match` to validate it's scheme. \n  Except for JDBC URL, currently it just check a valid JDBC scheme.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cstrong\u003einteger\u003c/strong\u003e\u003c/summary\u003e\nThe field under this rule must be integer.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cstrong\u003eboolean\u003c/strong\u003e\u003c/summary\u003e\n\nThe field under this rule must be boolean. Accepted input are `true`, `false`, `\"true\"`, `\"false\"`, `1`, `0`, `\"0\"`, `\"1\"`, `\"y\"`, `\"n\"`.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cstrong\u003eip\u003c/strong\u003e\u003c/summary\u003e\n\nThe field under this rule must be valid ipv4 or ipv6.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cstrong\u003eipv4\u003c/strong\u003e\u003c/summary\u003e\n\nThe field under this rule must be valid ipv4.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cstrong\u003eipv6\u003c/strong\u003e\u003c/summary\u003e\n\nThe field under this rule must be valid ipv6.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cstrong\u003eextension\u003c/strong\u003e:extension_a,extension_b,...\u003c/summary\u003e\n\nThe field under this rule must end with an extension corresponding to one of those listed.\n\nThis is useful for validating a file type for a given a path or url. The `mimes` rule should be used for validating uploads.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cstrong\u003earray\u003c/strong\u003e\u003c/summary\u003e\n\nThe field under this rule must be array.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cstrong\u003esame\u003c/strong\u003e:another_field\u003c/summary\u003e\n\nThe field value under this rule must be same with `another_field` value.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cstrong\u003eregex\u003c/strong\u003e:/your-regex/\u003c/summary\u003e\n\nThe field under this rule must be match with given regex.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cstrong\u003edate\u003c/strong\u003e:format\u003c/summary\u003e\n\nThe field under this rule must be valid date format. Parameter `format` is optional, default format is `Y-m-d`.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cstrong\u003eaccepted\u003c/strong\u003e\u003c/summary\u003e\n\nThe field under this rule must be one of `'on'`, `'yes'`, `'1'`, `'true'`, or `true`.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cstrong\u003epresent\u003c/strong\u003e\u003c/summary\u003e\n\nThe field under this rule must be exists, whatever the value is.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cstrong\u003edifferent\u003c/strong\u003e:another_field\u003c/summary\u003e\n\nOpposite of `same`. The field value under this rule must be different with `another_field` value.\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cstrong\u003eafter\u003c/strong\u003e:tomorrow\u003c/summary\u003e\n\nAnything that can be parsed by `strtotime` can be passed as a parameter to this rule. Valid examples include :\n- after:next week\n- after:2016-12-31\n- after:2016\n- after:2016-12-31 09:56:02\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cstrong\u003ebefore\u003c/strong\u003e:yesterday\u003c/summary\u003e\n\nThis also works the same way as the [after rule](#after). Pass anything that can be parsed by `strtotime`\n\n\u003c/details\u003e\n\n\u003cdetails\u003e\u003csummary\u003e\u003cstrong\u003ecallback\u003c/strong\u003e\u003c/summary\u003e\n\nYou can use this rule to define your own validation rule.\nThis rule can't be registered using string pipe.\nTo use this rule, you should put Closure inside array of rules.\n\nFor example:\n\n```php\n$validation = $validator-\u003evalidate($_POST, [\n    'even_number' =\u003e [\n        'required',\n        function ($value) {\n            // false = invalid\n            return (is_numeric($value) AND $value % 2 === 0);\n        }\n    ]\n]);\n```\n\nYou can set invalid message by returning a string. \nFor example, example above would be:\n\n```php\n$validation = $validator-\u003evalidate($_POST, [\n    'even_number' =\u003e [\n        'required',\n        function ($value) {\n            if (!is_numeric($value)) {\n                return \":attribute must be numeric.\";\n            }\n            if ($value % 2 !== 0) {\n                return \":attribute is not even number.\";\n            }\n            // you can return true or don't return anything if value is valid\n        }\n    ]\n]);\n```\n\n\u003e Note: `IgniteKit\\Validation\\Rules\\Callback` instance is binded into your Closure. \n  So you can access rule properties and methods using `$this`.\n\n\u003c/details\u003e\n\n\n## Register/Override Rule\n\nAnother way to use custom validation rule is to create a class extending `IgniteKit\\Validation\\Rule`. \nThen register it using `setValidator` or `addValidator`.\n\nFor example, you want to create `unique` validator that check field availability from database. \n\nFirst, lets create `UniqueRule` class:\n\n```php\n\u003c?php\n\nuse IgniteKit\\Validation\\Rule;\n\nclass UniqueRule extends Rule\n{\n    protected $message = \":attribute :value has been used\";\n    \n    protected $fillableParams = ['table', 'column', 'except'];\n    \n    protected $pdo;\n    \n    public function __construct(PDO $pdo)\n    {\n        $this-\u003epdo = $pdo;\n    }\n    \n    public function check($value): bool\n    {\n        // make sure required parameters exists\n        $this-\u003erequireParameters(['table', 'column']);\n    \n        // getting parameters\n        $column = $this-\u003eparameter('column');\n        $table = $this-\u003eparameter('table');\n        $except = $this-\u003eparameter('except');\n\t\n        if ($except AND $except == $value) {\n            return true;\n        }\n\t\n        // do query\n        $stmt = $this-\u003epdo-\u003eprepare(\"select count(*) as count from `{$table}` where `{$column}` = :value\");\n        $stmt-\u003ebindParam(':value', $value);\n        $stmt-\u003eexecute();\n        $data = $stmt-\u003efetch(PDO::FETCH_ASSOC);\n\t\n        // true for valid, false for invalid\n        return intval($data['count']) === 0;\n    }\n}\n\n```\n\nThen you need to register `UniqueRule` instance into validator like this:\n\n```php\nuse IgniteKit\\Validation\\Validator;\n\n$validator = new Validator;\n\n$validator-\u003eaddValidator('unique', new UniqueRule($pdo));\n```\n\nNow you can use it like this:\n\n```php\n$validation = $validator-\u003evalidate($_POST, [\n    'email' =\u003e 'email|unique:users,email,exception@mail.com'\n]);\n```\n\nIn `UniqueRule` above, property `$message` is used for default invalid message. And property `$fillable_params` is used for `fillParameters` method (defined in `IgniteKit\\Validation\\Rule` class). By default `fillParameters` will fill parameters listed in `$fillable_params`. For example `unique:users,email,exception@mail.com` in example above, will set:\n\n```php\n$params['table'] = 'users';\n$params['column'] = 'email';\n$params['except'] = 'exception@mail.com';\n```\n\n\u003e If you want your custom rule accept parameter list like `in`,`not_in`, or `uploaded_file` rules, \n  you just need to override `fillParameters(array $params)` method in your custom rule class.\n\nNote that `unique` rule that we created above also can be used like this:\n\n```php\n$validation = $validator-\u003evalidate($_POST, [\n    'email' =\u003e [\n    \t'required', 'email',\n    \t$validator('unique', 'users', 'email')-\u003emessage('Custom message')\n    ]\n]);\n```\n\nSo you can improve `UniqueRule` class above by adding some methods that returning its own instance like this:\n\n```php\n\u003c?php\n\nuse IgniteKit\\Validation\\Rule;\n\nclass UniqueRule extends Rule\n{\n    ...\n    \n    public function table($table)\n    {\n        $this-\u003eparams['table'] = $table;\n        return $this;\n    }\n    \n    public function column($column)\n    {\n        $this-\u003eparams['column'] = $column;\n        return $this;\n    }\n    \n    public function except($value)\n    {\n        $this-\u003eparams['except'] = $value;\n        return $this;\n    }\n    \n    ...\n}\n\n```\n\nThen you can use it in more funky way like this:\n\n```php\n$validation = $validator-\u003evalidate($_POST, [\n    'email' =\u003e [\n    \t'required', 'email',\n    \t$validator('unique')-\u003etable('users')-\u003ecolumn('email')-\u003eexcept('exception@mail.com')-\u003emessage('Custom message')\n    ]\n]);\n```\n\n#### Implicit Rule\n\nImplicit rule is a rule that if it's invalid, then next rules will be ignored. For example if attribute didn't pass `required*` rules, mostly it's next rules will also be invalids. So to prevent our next rules messages to get collected, we make `required*` rules to be implicit.\n\nTo make your custom rule implicit, you can make `$implicit` property value to be `true`. For example:\n\n```php\n\u003c?php\n\nuse IgniteKit\\Validation\\Rule;\n\nclass YourCustomRule extends Rule\n{\n\n    protected $implicit = true;\n\n}\n``` \n\n#### Modify Value\n\nIn some case, you may want your custom rule to be able to modify it's attribute value like our `default/defaults` rule. So in current and next rules checks, your modified value will be used. \n\nTo do this, you should implements `IgniteKit\\Validation\\Rules\\Interfaces\\ModifyValue` and create method `modifyValue($value)` to your custom rule class.\n\nFor example:\n\n```php\n\u003c?php\n\nuse IgniteKit\\Validation\\Rule;\nuse IgniteKit\\Validation\\Rules\\Interfaces\\ModifyValue;\n\nclass YourCustomRule extends Rule implements ModifyValue\n{\n    ...\n\n    public function modifyValue($value)\n    {\n        // Do something with $value\n\n        return $value;\n    }\n\n    ...\n}\n```\n\n#### Before Validation Hook\n\nYou may want to do some preparation before validation running. For example our `uploaded_file` rule will resolves attribute value that come from `$_FILES` (undesirable) array structure to be well-organized array structure, so we can validate multiple file upload just like validating other data.\n\nTo do this, you should implements `IgniteKit\\Validation\\Rules\\Interfaces\\BeforeValidate` and create method `beforeValidate()` to your custom rule class.\n\nFor example:\n\n```php\n\u003c?php\n\nuse IgniteKit\\Validation\\Rule;\nuse IgniteKit\\Validation\\Rules\\Interfaces\\BeforeValidate;\n\nclass YourCustomRule extends Rule implements BeforeValidate\n{\n    ...\n\n    public function beforeValidate()\n    {\n        $attribute = $this-\u003egetAttribute(); // IgniteKit\\Validation\\Attribute instance\n        $validation = $this-\u003evalidation; // IgniteKit\\Validation\\Validation instance\n\n        // Do something with $attribute and $validation\n        // For example change attribute value\n        $validation-\u003esetValue($attribute-\u003egetKey(), \"your custom value\");\n    }\n\n    ...\n}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fignitekit%2Fvalidation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fignitekit%2Fvalidation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fignitekit%2Fvalidation/lists"}