{"id":19385842,"url":"https://github.com/maxlab/validator","last_synced_at":"2026-05-13T08:37:01.867Z","repository":{"id":62525664,"uuid":"76780958","full_name":"Maxlab/validator","owner":"Maxlab","description":"RespectValidation for Simple validation of data (post,forms, etc.) ","archived":false,"fork":false,"pushed_at":"2016-12-18T12:24:06.000Z","size":15,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-07T08:12:46.691Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/Maxlab.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":"2016-12-18T12:15:56.000Z","updated_at":"2017-03-11T21:48:36.000Z","dependencies_parsed_at":"2022-11-02T14:16:09.360Z","dependency_job_id":null,"html_url":"https://github.com/Maxlab/validator","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Maxlab%2Fvalidator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Maxlab%2Fvalidator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Maxlab%2Fvalidator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Maxlab%2Fvalidator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Maxlab","download_url":"https://codeload.github.com/Maxlab/validator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240527200,"owners_count":19815802,"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-10T10:03:23.850Z","updated_at":"2026-05-13T08:37:01.824Z","avatar_url":"https://github.com/Maxlab.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# RespectValidation for Simple validation of data (post,forms, etc.) ([ENG doc](README.md) | [RU doc](README_RU.md)) \n\n## What is it?\nThis is a small library based on [RespectValidation](https://github.com/Respect/Validation) to simplify the assignment of validation rules receipt and error. Therefore, if you are not familiar with RespectValidation library, for a greater understanding of the example below, it is recommended to familiarize yourself first with her.\n\n\n## Exemple\nWe got the dataset from $_POST:\n```php\n    \n    $data = array(\n        'first_name' =\u003e 'Nikolay',\n        'last_name' =\u003e 1,\n        'addresses' =\u003e array(\n            2 =\u003e array(\n                'city' =\u003e 'Some City',\n                'street' =\u003e 'Some Street 4',\n                'home' =\u003e '4b',\n                'apartment' =\u003e 2,\n                'email' =\u003e 'notvalidemail',\n            ),\n            5 =\u003e array(\n                'city' =\u003e 'Some City',\n                'street' =\u003e 'Some Street 10',\n                'home' =\u003e '1a',\n                'apartment' =\u003e 10,\n                'email' =\u003e 'valid@email.com',\n                'siblings_emails' =\u003e array(\n                    2 =\u003e array(\n                        'email' =\u003e 'notvalidemail'\n                    ),\n                    8 =\u003e array(\n                        'email' =\u003e 'valid@email.com'\n                    ),\n                )\n            ),\n            10 =\u003e array(\n                'city' =\u003e 'Ясный - 1$$',\n                'street' =\u003e 'Some Street 10',\n                'home' =\u003e '1a',\n                'apartment' =\u003e 10,\n                'email' =\u003e 'valid@email.com',\n                'siblings_emails' =\u003e array(\n                    2 =\u003e array(\n                        'email' =\u003e 'notvalidemail'\n                    ),\n                    8 =\u003e array(\n                        'email' =\u003e 'valid@email.com'\n                    ),\n                )\n            ),\n        ),\n    );\n    \n```\n\nOften, in the course of work on the project there is a need to connect your own validator classes.\n- In this example, the folder `\\Maxlab\\Validation\\Examples\\Rules` is the class Alcinum.php\n- Below, we talk RespectValidation the location of the additional rules and exceptions.(see details in the documentation [RespectValidation](https://github.com/Respect/Validation))\n```php\n    \n    \\Respect\\Validation\\Validator::with('\\\\Maxlab\\\\Validation\\\\Examples\\\\Rules', true);\n    \n```\n\nSet validation rules:\n```php\n    \n    $rules = [\n        'first_name' =\u003e \\Respect\\Validation\\Validator::stringType()\n            -\u003epostalCode('RU'),\n        'last_name' =\u003e \\Respect\\Validation\\Validator::stringType()\n            -\u003enotEmpty(),\n        '\\..*.email' =\u003e \\Respect\\Validation\\Validator::email(),\n        'addresses.*.city' =\u003e \\Respect\\Validation\\Validator::stringType()-\u003elength(1, 150)-\u003ealcinum('- '),\n    ];\n    \n```\n\nDefine our messages for errors:\n```php\n    \n    $locale = new \\Maxlab\\Validation\\Examples\\Lang\\Ru();\n    $validator = new \\Maxlab\\Validation\\DataValidator($data, $rules, $locale);\n    \n    if($errors = $validator-\u003egetErrors()) {\n        var_dump($errors);\n    }\n    \n```\n\nGet in $errors:\n```no-highlight\narray(3) {\n  [\"first_name\"]=\u003e string(46) \"first_name must be a valid postal code on \"RU\"\"\n  [\"last_name\"]=\u003e string(51) \"должно быть строковым типом\"\n  [\"addresses\"]=\u003e\n  array(3) {\n    [2]=\u003e\n    array(1) {\n      [\"email\"]=\u003e\n      string(59) \"не является валидным email адресом\"\n    }\n    [5]=\u003e\n    array(1) {\n      [\"siblings_emails\"]=\u003e\n      array(2) {\n        [2]=\u003e\n        array(1) {\n          [\"email\"]=\u003e\n          string(59) \"не является валидным email адресом\"\n        }\n        [8]=\u003e\n        string(59) \"не является валидным email адресом\"\n      }\n    }\n    [10]=\u003e\n    array(2) {\n      [\"city\"]=\u003e\n      string(62) \"только символы (А-я), (A-z), цифры и \"- \"\"\n      [\"siblings_emails\"]=\u003e\n      array(2) {\n        [2]=\u003e\n        array(1) {\n          [\"email\"]=\u003e\n          string(59) \"не является валидным email адресом\"\n        }\n        [8]=\u003e\n        string(59) \"не является валидным email адресом\"\n      }\n    }\n  }\n}\n\n```\n\n## In the templating engine (twig)\n```twig\n    \n    {% if formerrors['addresses'][k]['city'] is defined %}\n        \u003cdiv class=\"b_form_error\"\u003e\n            {{ formerrors['addresses'][k]['city'] }}\n        \u003c/div\u003e\n    {% endif %}\n    \n    \n```\n\n## Explanation\nAs you can see, the simplicity and power of assignment rules make a regular expression. This feature eliminates the need to copy-paste rules and to describe each field separately. Also, this approach makes it more convenient to assign rules for nested data.\nUsing regexps, you can do crazy things:\n- `\\..*.email` - to providerwith all the data with a key `email` recursively\n- to do so `addresses.*.id` or so `addresses.[\\d].id` or even so `addresses.[0-9]{1,3}.id`\n- using regular expressions opens wide horizons in terms of binding the validation rules to nested data\n\n## Conclusion\nThis approach fits well for the validation of the nested data. It could be a $_POST array or the data from the Api, the data from the DB and so on.\nIf you have ideas or suggestions on how to improve or optimize the operation of the library, then you should offer them in the form of a pullrequest.\n\n## More examples!\n\n```php\n    \n    public function getFormRules($formdata)\n    {\n        $drule = [];\n        $drule['address'] = $formdata['formstate']['new_addr'] == 'no_edit' ? '\\.(?!new).*\\.' : '\\..*\\.';\n        \n        $rules = [\n            'user\\..*' =\u003e v::stringType(),\n            'user.first_name' =\u003e v::length(0, 150)-\u003ealcinum('\\'\" '),\n            'user.last_name' =\u003e v::length(0, 150)-\u003ealcinum('\\'\" '),\n            'user.phone' =\u003e v::optional(v::length(1, 150)-\u003ephone()),\n            'user.email' =\u003e v::notEmpty()-\u003elength(1, 150)-\u003eemail(),\n            \n            'user.b_day' =\u003e [\n                'rule' =\u003e v::optional(v::length(1, 2)-\u003ebetween(1, 31)),\n                'name' =\u003e 'user.birth_day',\n            ],\n            'user.b_month' =\u003e [\n                'rule' =\u003e v::optional(v::length(1, 2)-\u003ebetween(1, 12)),\n                'name' =\u003e 'user.birth_day',\n            ],\n            'user.b_year' =\u003e [\n                'rule' =\u003e v::optional(v::length(1, 4)-\u003ebetween(1910, date('Y'))),\n                'name' =\u003e 'user.birth_day',\n            ],\n            'user.birth_day' =\u003e v::optional(v::date()),\n            \n            'addresses'.$drule['address'].'(?!id|index|comment)' =\u003e v::stringType(),\n            'addresses'.$drule['address'].'city' =\u003e v::notEmpty()-\u003elength(1, 255)-\u003ealcinum('\\'\"\\\\- '),\n            'addresses'.$drule['address'].'street' =\u003e v::notEmpty()-\u003elength(1, 255)-\u003ealcinum('\\'\"\\\\- '),\n            'addresses'.$drule['address'].'home' =\u003e v::notEmpty()-\u003elength(1, 20),\n            'addresses'.$drule['address'].'build' =\u003e v::optional(v::length(1, 20)),\n            'addresses'.$drule['address'].'room' =\u003e v::optional(v::length(1, 20)),\n            'customer_details\\..*' =\u003e v::stringType()-\u003eoptional(v::length(1, 500)),\n            \n            'change_password.old_password' =\u003e v::optional(v::stringType()-\u003elength(1, 255)-\u003euserPasswordCheck()),\n            'change_password.new_password' =\u003e v::optional(v::stringType()-\u003elength(1, 255)-\u003ealnum(\"#*\u0026%$\")-\u003enoWhitespace()),\n        ];\n        \n        return $rules;\n    }\n    \n```\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaxlab%2Fvalidator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmaxlab%2Fvalidator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaxlab%2Fvalidator/lists"}