{"id":33993314,"url":"https://github.com/jin2chen/yii-validator","last_synced_at":"2026-04-22T02:32:27.432Z","repository":{"id":56999162,"uuid":"377736102","full_name":"jin2chen/yii-validator","owner":"jin2chen","description":"Validators for nest validation.","archived":false,"fork":false,"pushed_at":"2023-03-10T11:59:32.000Z","size":23,"stargazers_count":0,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-12-14T19:00:15.393Z","etag":null,"topics":["nest","validators"],"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/jin2chen.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2021-06-17T07:01:54.000Z","updated_at":"2021-06-24T01:30:42.000Z","dependencies_parsed_at":"2023-01-30T00:30:36.633Z","dependency_job_id":null,"html_url":"https://github.com/jin2chen/yii-validator","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":"jin2chen/package-template","purl":"pkg:github/jin2chen/yii-validator","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jin2chen%2Fyii-validator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jin2chen%2Fyii-validator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jin2chen%2Fyii-validator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jin2chen%2Fyii-validator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jin2chen","download_url":"https://codeload.github.com/jin2chen/yii-validator/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jin2chen%2Fyii-validator/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32118104,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-22T00:31:26.853Z","status":"online","status_checked_at":"2026-04-22T02:00:05.693Z","response_time":58,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["nest","validators"],"created_at":"2025-12-13T07:10:01.272Z","updated_at":"2026-04-22T02:32:27.416Z","avatar_url":"https://github.com/jin2chen.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Yii Nest Validator\n\n[![Latest Stable Version](https://poser.pugx.org/jin2chen/yii-validator/v)](https://packagist.org/packages/jin2chen/yii-validator)\n[![Total Downloads](https://poser.pugx.org/jin2chen/yii-validator/downloads)](https://packagist.org/packages/jin2chen/yii-validator)\n[![License](https://poser.pugx.org/jin2chen/yii-validator/license)](https://packagist.org/packages/jin2chen/yii-validator)\n[![Build status](https://github.com/jin2chen/yii-validator/workflows/build/badge.svg)](https://github.com/jin2chen/yii-validator/actions?query=workflow%3Abuild)\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/jin2chen/yii-validator/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/jin2chen/yii-validator/?branch=master)\n[![Code Coverage](https://scrutinizer-ci.com/g/jin2chen/yii-validator/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/jin2chen/yii-validator/?branch=master)\n[![static analysis](https://github.com/jin2chen/yii-validator/workflows/static%20analysis/badge.svg)](https://github.com/jin2chen/yii-validator/actions?query=workflow%3A%22static+analysis%22)\n\nValidator for nest validation.\n\n## Requirements\n\n- PHP 7.4 or higher.\n\n## Installation\n\nThe package could be installed with composer:\n\n```shell\ncomposer require jin2chen/yii-validator --prefer-dist\n```\n\n## General usage\n### 1. Use `DataSetInterface` and `RulesProviderInterface`\n```php\nuse jin2chen\\YiiValidator\\Rule\\Many;\nuse jin2chen\\YiiValidator\\Rule\\One;\nuse jin2chen\\YiiValidator\\Validator;\nuse Yiisoft\\Validator\\DataSetInterface;\nuse Yiisoft\\Validator\\Rule;\nuse Yiisoft\\Validator\\RulesProviderInterface;\n\nabstract class Model implements DataSetInterface, RulesProviderInterface\n{\n    public function getAttributeValue(string $attribute)\n    {\n        return $this-\u003e{$attribute};\n    }\n\n    public function hasAttribute(string $attribute): bool\n    {\n        return property_exists($this, $attribute);\n    }\n}\n\n/**\n * @internal\n */\nfinal class UserForm extends Model\n{\n    /**\n     * @var string\n     */\n    public $firstname;\n    /**\n     * @var string\n     */\n    public $lastname;\n    /**\n     * @var string\n     */\n    public $email;\n    /**\n     * @var Profile\n     */\n    public $profile;\n    /**\n     * @var Address[]\n     */\n    public $addresses;\n\n    public function getRules(): array\n    {\n        return [\n            'firstname' =\u003e $this-\u003efirstnameRules(),\n            'lastname' =\u003e $this-\u003elastnameRules(),\n            'email' =\u003e $this-\u003eemailRules(),\n            'profile' =\u003e $this-\u003eprofileRules(),\n            'addresses' =\u003e $this-\u003eaddressesRules(),\n        ];\n    }\n\n    private function firstnameRules(): array\n    {\n        return [\n            Rule\\Required::rule(),\n            Rule\\MatchRegularExpression::rule('/[a-z]+\\s*[a-z]+/i'),\n        ];\n    }\n\n    private function lastnameRules(): array\n    {\n        return [\n            Rule\\Required::rule(),\n            Rule\\MatchRegularExpression::rule('/[a-z]+\\s*[a-z]+/i'),\n        ];\n    }\n\n    private function emailRules(): array\n    {\n        return [\n            Rule\\Required::rule(),\n            Rule\\Email::rule(),\n        ];\n    }\n\n    private function profileRules(): array\n    {\n        return [\n            Rule\\Required::rule(),\n            One::rule(),\n        ];\n    }\n\n    private function addressesRules(): array\n    {\n        return [\n            Many::rule()-\u003eskipOnEmpty(true),\n        ];\n    }\n}\n\nfinal class Profile extends Model\n{\n    /**\n     * @var string\n     */\n    public $website;\n    /**\n     * @var string\n     */\n    public $title;\n\n    public function getRules(): array\n    {\n        return [\n            'website' =\u003e $this-\u003ewebsiteRules(),\n            'title' =\u003e $this-\u003etitleRules(),\n        ];\n    }\n\n    private function websiteRules(): array\n    {\n        return [\n            Rule\\Url::rule()-\u003eskipOnEmpty(true),\n        ];\n    }\n\n    private function titleRules(): array\n    {\n        return [\n            Rule\\InRange::rule(['CEO', 'COO', 'CFO'])-\u003eskipOnEmpty(true),\n        ];\n    }\n}\n\nfinal class Address extends Model\n{\n    /**\n     * @var string\n     */\n    public $street;\n    /**\n     * @var string\n     */\n    public $city;\n    /**\n     * @var string\n     */\n    public $state;\n    /**\n     * @var string\n     */\n    public $zipcode;\n\n    public function getRules(): array\n    {\n        return [\n            'street' =\u003e $this-\u003estreetRules(),\n            'city' =\u003e $this-\u003ecityRules(),\n            'state' =\u003e $this-\u003estateRules(),\n            'zipcode' =\u003e $this-\u003ezipcodeRules(),\n        ];\n    }\n\n    private function streetRules(): array\n    {\n        return [\n            Rule\\Required::rule(),\n        ];\n    }\n\n    private function cityRules(): array\n    {\n        return [\n            Rule\\Required::rule(),\n        ];\n    }\n\n    private function stateRules(): array\n    {\n        return [\n            Rule\\Required::rule(),\n            Rule\\MatchRegularExpression::rule('/^[A-Z]{2}$/')\n        ];\n    }\n\n    private function zipcodeRules(): array\n    {\n        return [\n            Rule\\MatchRegularExpression::rule('/\\d{6}/')-\u003eskipOnEmpty(true),\n        ];\n    }\n}\n\n$form = new UserForm();\n$validator = new Validator();\n\n$form-\u003eprofile = new Profile();\n$form-\u003eprofile-\u003ewebsite = 'www.jinchen.me';\n\n$address1 = new Address();\n$address2 = new Address();\n$address2-\u003estate = 'ABC';\n$addresses = [\n    $address1,\n    $address2,\n];\n$form-\u003eaddresses = $addresses;\n\n$results = $validator-\u003evalidate($form);\nprint_r($results-\u003egetErrors());\n\n//[\n//    'firstname' =\u003e ['Value cannot be blank.'],\n//    'lastname' =\u003e ['Value cannot be blank.'],\n//    'email' =\u003e ['Value cannot be blank.'],\n//    'profile.website' =\u003e ['This value is not a valid URL.'],\n//    'addresses.0.street' =\u003e ['Value cannot be blank.'],\n//    'addresses.0.city' =\u003e ['Value cannot be blank.'],\n//    'addresses.0.state' =\u003e ['Value cannot be blank.'],\n//    'addresses.1.street' =\u003e ['Value cannot be blank.'],\n//    'addresses.1.city' =\u003e ['Value cannot be blank.'],\n//    'addresses.1.state' =\u003e ['Value is invalid.'],\n//];\n```\n\n### 2. Use array data\n```php\nuse jin2chen\\YiiValidator\\Rule\\Many;\nuse jin2chen\\YiiValidator\\Rule\\One;\nuse jin2chen\\YiiValidator\\Validator;\nuse Yiisoft\\Validator\\Rule;\n\n$data = [\n    'email' =\u003e 'abc.com',\n    'profile' =\u003e [\n        'website' =\u003e 'www.jinchen.me',\n    ],\n    'addresses' =\u003e [\n        [\n            'id' =\u003e 22,\n            'state' =\u003e 'AAA',\n        ],\n        [\n            'id' =\u003e 32,\n            'state' =\u003e 'BBB',\n        ],\n    ],\n];\n\n$rules = [\n    'email' =\u003e [\n        Rule\\Email::rule(),\n    ],\n    'profile' =\u003e [\n        One::rule()-\u003ewithRules(\n            [\n                'website' =\u003e [\n                    Rule\\Url::rule(),\n                ],\n            ]\n        ),\n    ],\n    'addresses' =\u003e [\n        Many::rule()-\u003ewithRules(\n            [\n                'state' =\u003e [\n                    Rule\\MatchRegularExpression::rule('/^[A-Z]{2}$/'),\n                ],\n            ]\n        )-\u003ewithIndexKey('id'),\n    ],\n];\n\n$validator = new Validator();\n$results = $validator-\u003evalidate($data, $rules);\nprint_r($results-\u003egetErrors());\n//[\n//    'email' =\u003e ['This value is not a valid email address.'],\n//    'profile.website' =\u003e ['This value is not a valid URL.'],\n//    'addresses.22.state' =\u003e ['Value is invalid.'],\n//    'addresses.32.state' =\u003e ['Value is invalid.'],\n//]\n```\n\n## Testing\n\n### Unit testing\n\nThe package is tested with [PHPUnit](https://phpunit.de/). To run tests:\n\n```shell\n./vendor/bin/phpunit\n```\n\n### Static analysis\n\nThe code is statically analyzed with [Psalm](https://psalm.dev/). To run static analysis:\n\n```shell\n./vendor/bin/psalm\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjin2chen%2Fyii-validator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjin2chen%2Fyii-validator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjin2chen%2Fyii-validator/lists"}