{"id":20471012,"url":"https://github.com/cangelis/simple-validator","last_synced_at":"2025-05-05T16:20:10.604Z","repository":{"id":7470750,"uuid":"8818569","full_name":"cangelis/simple-validator","owner":"cangelis","description":"Simple Validator is an awesome and easy to use validator for php","archived":false,"fork":false,"pushed_at":"2017-06-29T10:09:59.000Z","size":43,"stargazers_count":72,"open_issues_count":1,"forks_count":22,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-05-05T16:19:59.536Z","etag":null,"topics":["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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cangelis.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-03-16T12:42:00.000Z","updated_at":"2025-01-29T19:29:21.000Z","dependencies_parsed_at":"2022-08-24T05:11:09.547Z","dependency_job_id":null,"html_url":"https://github.com/cangelis/simple-validator","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cangelis%2Fsimple-validator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cangelis%2Fsimple-validator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cangelis%2Fsimple-validator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cangelis%2Fsimple-validator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cangelis","download_url":"https://codeload.github.com/cangelis/simple-validator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252531879,"owners_count":21763293,"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","validation","validator"],"created_at":"2024-11-15T14:14:41.192Z","updated_at":"2025-05-05T16:20:10.584Z","avatar_url":"https://github.com/cangelis.png","language":"PHP","readme":"# Simple Validator Documentation\n\n[![Build Status](https://travis-ci.org/cangelis/simple-validator.png?branch=master)](https://travis-ci.org/cangelis/simple-validator)\n\nValidation is the process that checks for correctness, meaningfulness and security of the input data. \nSimpleValidator is a library that handles validation process easily.\n\n## Install\n\n### Including to your current composer.json\n\nAdd this line into `require` in your ***composer.json***:\n\n```\n\"simple-validator/simple-validator\": \"1.0.*\"\n```\n\nand call \n\n```\nphp composer.phar update\n```\n\n### Installing directly\n\nDownload  `composer.phar` and call\n\n```\nphp composer.phar install\n```\n\nand use autoload.php to include the classes\n\n```php\nrequire 'vendor/autoload.php'\n```\n\n## A few examples:\n\n```php\n\u003c?php\n$rules = [\n    'name' =\u003e [\n        'required',\n        'alpha',\n        'max_length(50)'\n    ],\n    'age' =\u003e [\n        'required',\n        'integer',\n    ],\n    'email' =\u003e [\n        'required',\n        'email'\n    ],\n    'password' =\u003e [\n        'required',\n        'equals(:password_verify)'\n    ],\n    'password_verify' =\u003e [\n        'required'\n    ]\n];\n$validation_result = SimpleValidator\\Validator::validate($_POST, $rules);\nif ($validation_result-\u003eisSuccess() == true) {\n    echo \"validation ok\";\n} else {\n    echo \"validation not ok\";\n    var_dump($validation_result-\u003egetErrors());\n}\n```\n\n## Custom Rules with anonymous functions\n\nAnonymous functions make the custom validations easier to be implemented. \n\n### Example\n\n```php\n$rules = [\n    'id' =\u003e [\n        'required',\n        'integer',\n        'post_exists' =\u003e function($input) {\n            $query = mysqli_query(\"SELECT * FROM post WHERE id = \" . $input);\n            if (mysqli_num_rows($query) == 0)\n                return false;\n            return true;\n        },\n        'between(5,15)' =\u003e function($input, $param1, $param2) {\n            if (($input \u003e $param1) \u0026\u0026 ($input \u003c $param2))\n                return true;\n            return false;\n        }\n    ]\n];\n```\n\nand you need to add an error text for your rule to the error file (default: errors/en.php).\n\n```php\n'post_exists' =\u003e \"Post does not exist\"\n```\n    \nor add a custom error text for that rule\n\n```php\n$validation_result-\u003ecustomErrors([\n    'post_exists' =\u003e 'Post does not exist'\n]);\n```\n    \n### Another example to understand scoping issue\n\n```php\n// my local variable\n$var_to_compare = \"1234\";\n$rules = [\n    'password' =\u003e [\n        'required',\n        'integer',\n        // pass my local variable to anonymous function\n        'is_match' =\u003e function($input) use (\u0026$var_to_compare) {\n            if ($var_to_compare == $input)\n                return true;\n            return false;\n        }\n    ]\n];\n```\n\n## Custom Validators\n\nYou can assume SimpleValidator as a tool or an interface to create a validator for yourself.\n\nCustom validators can have their own rules, own error files or default language definitions. In addition, you can override default rules in your custom validator.\n\n```php\n\nclass MyValidator extends \\SimpleValidator\\Validator {\n\n    // methods have to be static !!!\n    protected static function is_awesome($input) {\n        if ($input == \"awesome\")\n            return true;\n        return false;\n    }\n\n    // overriding a default rule (url)\n    protected static function url($input) {\n        return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $input);\n    }\n\n    // set default language for your validator\n    // if you don't override this method, the default language is \"en\"\n    protected function getDefaultLang() {\n        return getMyApplicationsDefaultLanguage();\n    }\n\n    // defining error files for your validator\n    // in this example your files should live in \"{class_path}/errors/{language}/validator.php\n    protected function getErrorFilePath($lang) {\n        return __DIR__ . \"/errors/\" . $lang . \"/validator.php\";\n    }\n\n}\n\n```\n\n**Create an error file:**\n\n```php\nreturn [\n    'is_awesome' =\u003e 'the :attribute is not awesome'\n    // error text for url is already defined in default error text file you don't have to define it here, but optionally you can\n];\n```\n\nAnd then, call the `validate` method.\n   \n```php\n$rules = [\n    'website' =\u003e [\n        'is_awesome',\n        'url'\n    ]\n];\n$validation_result = MyValidator::validate($_POST, $rules);\n```\n\n## Custom Rule parameters\n\nA rule can have multiple parameters. An example:\n\n```php\n$rule = [\n    'id' =\u003e [\n        'rule1(:input1,:input2,2,5,:input3)' =\u003e function($input, $input1, $input2, $value1, $value2, $input3) {\n            // validation here\n        }\n    ],\n    // and so on..\n];\n\n\n```\n\n## Custom Error messages\n\n### Using Error file\nCustom rules provides localization for the error messages.\nCreate a new file under **errors** folder, example: ```errors/es.php```\nand call ```getErrors()``` method using:\n\n```php\n$validation_result-\u003egetErrors('es');\n```\n### Using customErrors method\nYou can add custom errors using customErrors method.\n#### Examples:\n```php\n$validation_result-\u003ecustomErrors([\n    // input_name.rule =\u003e error text\n    'website.required' =\u003e 'We need to know your web site',\n    // rule =\u003e error text\n    'required' =\u003e ':attribute field is required',\n    'name.alpha' =\u003e 'Name field must contain alphabetical characters',\n    'email_addr.email' =\u003e 'Email should be valid',\n    'email_addr.min_length' =\u003e 'Hey! Email is shorter than :params(0)',\n    'min_length' =\u003e ':attribute must be longer than :params(0)'\n]);\n```\n## Naming Inputs\n\n```php\n$naming =\u003e [\n    'name' =\u003e 'Name',\n    'url' =\u003e 'Web Site',\n    'password' =\u003e 'Password',\n    'password_verify' =\u003e 'Password Verification'\n];\n$validation_result = SimpleValidator\\Validator::validate($_POST, $rules, $naming);\n```\n#### Output sample:\n\n* Name field is required \u003ci\u003e-instead of \"name field is required\"-\u003c/i\u003e\n* Web Site field is required \u003ci\u003e-instead of \"url field is required\"-\u003c/i\u003e\n* Password field should be same as Password Verification \u003ci\u003e-equals(:password_verify) rule-\u003c/i\u003e\n\n## More\n\nYou can explicitly check out the validations using `has` method that might be useful for Unit Testing purposes.\n    \n\n```php\n// All return boolean\n$validation_result-\u003ehas('email');\n$validation_result-\u003ehas('email','required');\n$validation_result-\u003ehas('password','equals');\n```\n\n## Default validations\n\n\u003ctable\u003e\n    \u003ctr\u003e\n        \u003cth\u003eRule\u003c/th\u003e\n        \u003cth\u003eParameter\u003c/th\u003e\n        \u003cth\u003eDescription\u003c/th\u003e\n        \u003cth\u003eExample\u003c/th\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003erequired\u003c/td\u003e\n        \u003ctd\u003eNo\u003c/td\u003e\n        \u003ctd\u003eReturns FALSE if the input is empty\u003c/td\u003e\n        \u003ctd\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003enumeric\u003c/td\u003e\n        \u003ctd\u003eNo\u003c/td\u003e\n        \u003ctd\u003eReturns FALSE if the input is not numeric\u003c/td\u003e\n        \u003ctd\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003eemail\u003c/td\u003e\n        \u003ctd\u003eNo\u003c/td\u003e\n        \u003ctd\u003eReturns FALSE if the input is not a valid email address\u003c/td\u003e\n        \u003ctd\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003einteger\u003c/td\u003e\n        \u003ctd\u003eNo\u003c/td\u003e\n        \u003ctd\u003eReturns FALSE if the input is not an integer value\u003c/td\u003e\n        \u003ctd\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003efloat\u003c/td\u003e\n        \u003ctd\u003eNo\u003c/td\u003e\n        \u003ctd\u003eReturns FALSE if the input is not a float value\u003c/td\u003e\n        \u003ctd\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003ealpha\u003c/td\u003e\n        \u003ctd\u003eNo\u003c/td\u003e\n        \u003ctd\u003eReturns FALSE if the input contains non-alphabetical characters\u003c/td\u003e\n        \u003ctd\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n   \u003ctr\u003e\n        \u003ctd\u003ealpha_numeric\u003c/td\u003e\n        \u003ctd\u003eNo\u003c/td\u003e\n        \u003ctd\u003eReturns FALSE if the input contains non-alphabetical and numeric characters\u003c/td\u003e\n        \u003ctd\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003eip\u003c/td\u003e\n        \u003ctd\u003eNo\u003c/td\u003e\n        \u003ctd\u003eReturns FALSE if the input is not a valid IP (IPv6 supported)\u003c/td\u003e\n        \u003ctd\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003eurl\u003c/td\u003e\n        \u003ctd\u003eNo\u003c/td\u003e\n        \u003ctd\u003eReturns FALSE if the input is not a valid URL\u003c/td\u003e\n        \u003ctd\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003emax_length\u003c/td\u003e\n        \u003ctd\u003eYes\u003c/td\u003e\n        \u003ctd\u003eReturns FALSE if the input is longer than the parameter\u003c/td\u003e\n        \u003ctd\u003emax_length(10)\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003emin_length\u003c/td\u003e\n        \u003ctd\u003eYes\u003c/td\u003e\n        \u003ctd\u003eReturns FALSE if the input is shorter than the parameter\u003c/td\u003e\n        \u003ctd\u003emin_length(10)\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003eexact_length\u003c/td\u003e\n        \u003ctd\u003eYes\u003c/td\u003e\n        \u003ctd\u003eReturns FALSE if the input is not exactly parameter value long\u003c/td\u003e\n        \u003ctd\u003eexact_length(10)\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n        \u003ctd\u003eequals\u003c/td\u003e\n        \u003ctd\u003eYes\u003c/td\u003e\n        \u003ctd\u003eReturns FALSE if the input is not same as the parameter\u003c/td\u003e\n        \u003ctd\u003eequals(:password_verify) or equals(foo)\u003c/td\u003e\n    \u003c/tr\u003e\n\u003c/table\u003e\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcangelis%2Fsimple-validator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcangelis%2Fsimple-validator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcangelis%2Fsimple-validator/lists"}