{"id":18828140,"url":"https://github.com/frangeris/simple-validation","last_synced_at":"2025-04-14T02:31:04.236Z","repository":{"id":17301851,"uuid":"20072346","full_name":"frangeris/simple-validation","owner":"frangeris","description":"Simple, fast, beautiful way to make validations for forms or anything you can think of","archived":false,"fork":false,"pushed_at":"2015-03-13T21:32:46.000Z","size":816,"stargazers_count":4,"open_issues_count":7,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-27T16:21:57.060Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"danialfarid/ng-file-upload","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/frangeris.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-05-22T18:32:08.000Z","updated_at":"2015-03-13T21:32:23.000Z","dependencies_parsed_at":"2022-08-04T20:16:08.050Z","dependency_job_id":null,"html_url":"https://github.com/frangeris/simple-validation","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frangeris%2Fsimple-validation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frangeris%2Fsimple-validation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frangeris%2Fsimple-validation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/frangeris%2Fsimple-validation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/frangeris","download_url":"https://codeload.github.com/frangeris/simple-validation/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248810883,"owners_count":21165195,"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":[],"created_at":"2024-11-08T01:23:42.550Z","updated_at":"2025-04-14T02:31:03.993Z","avatar_url":"https://github.com/frangeris.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Simple validation for PHP 5.5+\n==============================\n\nThere are so many differents ways to make a validation for forms on php, each framework(Laravel 4+, Symfony 2+, CodeIgniter , FuelPHP, CakePHP, bla bla bla) has their own implementation on validations, ok, that's ok, it is expected that they supply us that funcionality, but, sometimes all those validations are complicated, sometimes we dont want to insert the rules inside a model when we just wanna validate a simple html form and get the possible failures, just simple like that, it ok that you insert your rules inside your model, this is another way just with super powers...\n\n### Installation\n\nEasiest way would be using [Composer](http://getcomposer.org) and adding this to\nthe composer.json requirement section.\n\n```json\n{\n  \"require\": {\n    \"frangeris/simple-validation\": \"dev-master\"\n  }\n}\n```\n\nIt's PSR-0 compliant, so you can also use your own custom autoloader.\n\n### Usage\n\nIn general, every form could have **rules**, what is a rule?, rule is just what you want to validate, just like that, here is the list of rules we support:\n\n* regex (for regular expression purposes)\n* min (validate that is upper than)\n* max (validate that don't exced a number)\n* required (validate that the value is not null or empty)\n* filter ([php native filter](http://www.php.net/manual/en/filter.filters.php) :D)\n* uploaded (validate that the file has been uploaded)\n* file_max (validate the max size of the file using MB for comparation)\n* allow_format (specify with formats are allowed for the file uploaded)\n* not_equal (verify that the value is not equal to expressed pattern)\n\n### So let's rock and roll:\n\nHere is an example of the structure for write your own validations, in this case I wanna validate a form for just save users over 18 years old, and of course a valid first name, last name and email.\n\n```html\n\u003cform method=\"post\" action=\"#\"\u003e\t\n\t\u003cinput type=\"text\" name=\"email\"\u003e\n\t\u003cinput type=\"text\" name=\"f_name\"\u003e\n\t\u003cinput type=\"text\" name=\"l_name\"\u003e\n\t\u003cinput type=\"text\" name=\"age\"\u003e\n\u003c/form\u003e  \n\n```\n\nfile with rules:\n\n```php\n\n\u003c?php\n\n/*\n|--------------------------------------------------------------------------\n| Secure validations\n|--------------------------------------------------------------------------\n| Here you need to define the rules of each form that you want to validate, \n| each index represent a form \u003cform_name\u003e, followed by an associative \n| array with the fields that contains; each field will have [requirements] \n| and [messages], each [requirements] must have a message to respond (in \n| case of failure). Putting the prefix on + \u003crequirement\u003e following \n| cammell case convention for the event\n|\n*/\n\nreturn array(\n\t'frm_user_add' =\u003e array(\n\t\t'email' =\u003e array(\n\t\t\t'requirements' =\u003e array('required' =\u003e true, 'filter' =\u003e FILTER_VALIDATE_EMAIL),\n\t\t\t'messages' =\u003e array(\t\t\t\t\n\t\t\t\t'on_required' =\u003e 'Insert the email or username for begin',\n\t\t\t\t'on_filter' =\u003e 'Invalid email, please try another'\n\t\t\t)\t\t\t\n\t\t),\n\n\t\t'f_name' =\u003e array(\n\t\t\t'requirements' =\u003e array('required' =\u003e true, 'min' =\u003e 3, 'max' =\u003e 30, 'regex' =\u003e '/^[a-z ]+$/i'),\n\t\t\t'messages' =\u003e array(\n\t\t\t\t'on_required' =\u003e 'Last name is required for create a user',\n\t\t\t\t'on_min' =\u003e 'Last name is very short',\n\t\t\t\t'on_max' =\u003e 'Last name is too long',\n\t\t\t\t'on_regex' =\u003e 'Invalid user name, please try again'\n\t\t\t)\n\t\t),\t\t\n\n\t\t'l_name' =\u003e array(\n\t\t\t'requirements' =\u003e array('required' =\u003e true, 'min' =\u003e 3, 'max' =\u003e 30 'regex' =\u003e '/^[a-z ]+$/i'),\n\t\t\t'messages' =\u003e array(\n\t\t\t\t'on_required' =\u003e 'Last name is required for create a user',\n\t\t\t\t'on_min' =\u003e 'Last name is very short',\n\t\t\t\t'on_max' =\u003e 'Last name is too long',\n\t\t\t\t'on_regex' =\u003e 'Invalid user name, please try again'\n\t\t\t)\n\t\t),\n\n\t\t'age' =\u003e array(\n\t\t\t'requirements' =\u003e array('required' =\u003e true, 'min' =\u003e 18),\n\t\t\t'messages' =\u003e array(\n\t\t\t\t'on_required' =\u003e 'Last name is required for create a user',\n\t\t\t\t'on_min' =\u003e 'Last name is very short',\n\t\t\t\t'on_max' =\u003e 'Last name is too long'\n\t\t\t)\n\t\t),\t\t\n\t),\n);\n\n```\n\nAs you can see `frm_user_add` is the name of a form, followed by an associative array for the fields requirements and messages.\n\n`email` is a field inside the form `frm_build_add`, we wanna that this field exist and must to be **valid** using the [filters inside PHP](http://www.php.net/manual/en/filter.filters.php), depending of each error, the string inside message is gonna be returned... Awesome right? :D\n\n`f_name` is a little complicated, is this case we want that the field be required, \u003e 3 and \u003c 30, and needs to match with the regular expression `/^[a-z ]+$/i`... \n\nFor `l_name` acts the same rule that `f_name`...\n\nAnd the last one `age`, I just wanna that exist, and the minimun value must to be 18, and that's it... :)\n\nIn the PHP side just call the validator class and pass the fields on request and the name of the form:\n\nThis example is using [phalcon framework](http://phalconphp.com/en/) making a POST request\n\n```php\n\n\u003c?php\n\nuse Simple\\Validation\\Validator;\n\nclass ApplicationController extends \\Phalcon\\Mvc\\Controller\n{\n\tpublic function addAction()\n\t{\n\t\tif($this-\u003erequest-\u003eisPost())\n\t\t{\n\t\t\t$request = $this-\u003erequest-\u003egetPost();\n\n\t\t\t// Here make the trick for pass the rules\n    \t\t$validator = Validator::getInstance(require_once(URL_TO_YOUR_FILE_WITH_RULES));\n\n    \t\t// Begin the validation (\u003cform_name\u003e, \u003carray_with_values\u003e)\n\t\t\tif($validator-\u003eisValid($request, $request))\n\t\t\t{\n\t\t\t\techo 'valid';\n\t\t\t\t// Do your stuff\n\t\t\t\t\n\t\t\t} else\n\t\t\t\techo 'Error: '.$validator-\u003egetLastError(true)); // This will give you the last error\n\t\t\t\n\t\t\t// If you wanna all the errors with messages just call\n\t\t\tprint_r($validator-\u003egetErrors());\n\t\t}\n\t}\t\n}\n\n```\nThe required function can be placed in another place, just remember that the `isValid` method needs this array for validate...\n\nPS: If you are using phalcon, dont do this in this way, create a DI inside your services and use it as di, someting like:\n\n```php\n\n// Set up the validator service\n$di-\u003eset('validator', function() use ($config)\n{\n    $validations = require_once($config-\u003esecurity-\u003evalidations);\n    return Simple\\Validation\\Validator::getInstance($validations);\n});","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrangeris%2Fsimple-validation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffrangeris%2Fsimple-validation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffrangeris%2Fsimple-validation/lists"}