{"id":266994,"date":"2015-10-28T18:42:02","date_gmt":"2015-10-28T15:42:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=266994"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=266994","title":{"rendered":"\u0412\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f \u0441\u043b\u043e\u0436\u043d\u044b\u0445 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440 \u0441 PHPixie Validate"},"content":{"rendered":"<p>       <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/7cd\/47c\/66c\/7cd47c66c5df9f4b83f7886240a1cba2.png\" alt=\"image\" align=\"right\"\/><br \/>  \u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0432\u044b\u0448\u0435\u043b \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 PHPixie 3, \u0432 \u044d\u0442\u043e\u0442 \u0440\u0430\u0437 \u0434\u043b\u044f \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438 \u0434\u0430\u043d\u043d\u044b\u0445. \u0411\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a \u0434\u043b\u044f PHP \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442\u0441\u044f \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0435\u0439 \u0443\u0436\u0435 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e, \u0437\u0430\u0447\u0435\u043c \u0442\u043e\u0433\u0434\u0430 \u043f\u0438\u0441\u0430\u0442\u044c \u0435\u0449\u0435 \u043e\u0434\u0438\u043d? \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0443 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0430 \u0438\u0437 \u043d\u0438\u0445 \u0435\u0441\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u043a \u2014 \u043e\u043d\u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0441 \u043e\u0434\u043d\u043e\u043c\u0435\u0440\u043d\u044b\u043c\u0438 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u0443\u044f\u0441\u044c \u0432 \u043f\u0435\u0440\u0432\u0443\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u0444\u043e\u0440\u043c\u0430\u043c\u0438. \u0422\u0430\u043a\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u043d\u0435\u0438\u0437\u0431\u0435\u0436\u043d\u043e \u0443\u0441\u0442\u0430\u0440\u0435\u043b \u0432 \u043c\u0438\u0440\u0435 API \u0438 REST, \u0432\u0441\u0435 \u0447\u0430\u0449\u0435 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u044c\u0441\u044f \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u043e\u0431\u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0430\u043c\u0438 \u0441\u043e \u0441\u043b\u043e\u0436\u043d\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043e\u0439. Validate \u0441 \u0441\u0430\u043c\u043e\u0433\u043e \u043d\u0430\u0447\u0430\u043b\u0430 \u0431\u044b\u043b \u0441\u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d \u043a\u0430\u043a \u0440\u0430\u0437 \u0447\u0442\u043e\u0431\u044b \u0441\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c\u0441\u044f \u0441 \u0442\u0430\u043a\u0438\u043c\u0438 \u0437\u0430\u0434\u0430\u0447\u0430\u043c\u0438. \u0418 \u0434\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u0432\u044b \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 PHPixie \u044d\u0442\u043e\u0442 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u043c\u043e\u0436\u0435\u0442 \u0432\u0430\u043c \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u0438\u0433\u043e\u0434\u0438\u0442\u044c\u0441\u044f.<br \/>  <a name=\"habracut\"><\/a><br \/>  \u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u043f\u0440\u0438\u043c\u0435\u0440\u0430, \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u043e\u0434\u043d\u043e\u043c\u0435\u0440\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430:<\/p>\n<pre><code class=\"php\">\/\/ \u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0441\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0435  $data = array(     'name' =&gt; 'Pixie',     'home' =&gt; 'Oak',     'age'  =&gt; 200,     'type' =&gt; 'fairy' );  $validate = new \\PHPixie\\Validate();  \/\/ \u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440 $validator = $validate-&gt;validator();  \/\/ \u041f\u043e \u0441\u0443\u0442\u0438 \u043e\u0434\u043d\u043e\u043c\u0435\u0440\u043d\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442 $document = $validator-&gt;rule()-&gt;addDocument();  \/\/ \u0414\u043b\u044f \u0437\u0430\u0434\u0430\u043d\u0438\u044f \u0441\u0430\u043c\u044b\u0445 \u043f\u0440\u0430\u0432\u0438\u043b \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \/\/ \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043e\u0432 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441\u0430. \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0439  \/\/ \u041e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u043f\u043e\u043b\u0435 \u0441 \u0444\u0438\u043b\u044c\u0442\u0440\u0430\u043c\u0438 $document-&gt;valueField('name')     -&gt;required()     -&gt;addFilter()         -&gt;alpha()         -&gt;minLength(3);  \/\/ \u0424\u0438\u043b\u044c\u0442\u0440\u044b \u0442\u0430\u043a\u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u0434\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u043c $document-&gt;valueField('home')     -&gt;required()     -&gt;addFilter()         -&gt;filters(array(             'alpha',             'minLength' =&gt; array(3)         ));  \/\/ \u0418\u043b\u0438 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u043e\u0434\u043d\u043e\u0433\u043e \u0444\u0438\u043b\u044c\u0442\u0440\u0430 \/\/ \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0435\u0433\u043e \u0441\u0440\u0430\u0437\u0443 $document-&gt;valueField('age')     -&gt;required()     -&gt;filter('numeric');  \/\/ \u0441\u0432\u043e\u0439 \u043a\u043e\u043b\u0431\u0435\u043a \u0434\u043b\u044f \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0433\u043e \u043f\u043e\u043b\u044f $document-&gt;valueField('type')     -&gt;required()     -&gt;callback(function($result, $value) {         if(!in_array($value, array('fairy', 'pixie'))) {             \/\/ \u0417\u0430\u0434\u0430\u0435\u043c \u0441\u0432\u043e\u044e \u043e\u0448\u0438\u0431\u043a\u0443             $result-&gt;addMessageError(&quot;Type can be either 'fairy' or 'pixie'&quot;);         }     });  \/\/ \u041f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u0432\u0430\u043b\u0438\u0434\u0430\u0442\u043e\u0440 \u043d\u0435 \u043f\u0440\u043e\u043f\u0443\u0441\u0442\u0438\u0442 \u043f\u043e\u043b\u044f \/\/ \u0434\u043b\u044f \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043d\u0435\u0442 \u043f\u0440\u0430\u0432\u0438\u043b \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438. \/\/ \u041d\u043e \u044d\u0442\u0443 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0443 \u043c\u043e\u0436\u043d\u043e \u043e\u0442\u043a\u043b\u044e\u0447\u0438\u0442\u044c $document-&gt;allowExtraFields();  \/\/ \u0441\u0432\u043e\u0439 \u043a\u043e\u043b\u0431\u0435\u043a \u0434\u043b\u044f \u0432\u0441\u0435\u0433\u043e \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430 $validator-&gt;rule()-&gt;callback(function($result, $value) {     if($value['type'] === 'fairy' && $value['home'] !== 'Oak') {         $result-&gt;addMessageError(&quot;Fairies live only inside oaks&quot;);     } }); <\/code><\/pre>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0422\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435 \u043d\u043e \u0441 \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u043d\u044b\u043c \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441\u043e\u043c<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"php\">$validator = $validate-&gt;validator(function($value) {     $value-&gt;document(function($document) {         $document             -&gt;allowExtraFields()             -&gt;field('name', function($name) {                 $name                     -&gt;required()                     -&gt;filter(function($filter) {                         $filter                             -&gt;alpha()                             -&gt;minLength(3);                     });             })             -&gt;field('home', function($home) {                 $home                     -&gt;required()                     -&gt;filter(array(                         'alpha',                         'minLength' =&gt; array(3)                     ));             })             -&gt;field('age', function($age) {                 $age                     -&gt;required()                     -&gt;filter('numeric');             })             -&gt;field('type', function($home) {                 $home                     -&gt;required()                     -&gt;callback(function($result, $value) {                         if(!in_array($value, array('fairy', 'pixie'))) {                             $result-&gt;addMessageError(&quot;Type can be either 'fairy' or 'pixie'&quot;);                         }                     });             });     })     -&gt;callback(function($result, $value) {         if($value['type'] === 'fairy' && $value['home'] !== 'Oak') {             $result-&gt;addMessageError(&quot;Fairies live only inside oaks&quot;);         }     }); }); <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0418 \u0441\u0430\u043c\u0430 \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044f:<\/p>\n<pre><code class=\"php\">$result = $validator-&gt;validate($data); var_dump($result-&gt;isValid());  \/\/ \u0414\u043e\u0431\u0430\u0432\u0438\u043c \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043e\u0448\u044b\u0431\u043e\u043a $data['name'] = 'Pi'; $data['home'] = 'Maple'; $result = $validator-&gt;validate($data); var_dump($result-&gt;isValid());  \/\/ \u0412\u044b\u0432\u0435\u0434\u0435\u043c \u043e\u0448\u0438\u0431\u043a\u0438 foreach($result-&gt;errors() as $error) {     echo $error.&quot;\\n&quot;; } foreach($result-&gt;invalidFields() as $fieldResult) {     echo $fieldResult-&gt;path().&quot;:\\n&quot;;     foreach($fieldResult-&gt;errors() as $error) {         echo $error.&quot;\\n&quot;;     } }  \/* bool(true) bool(false) Fairies live only inside oaks name: Value did not pass filter 'minLength' *\/ <\/code><\/pre>\n<h4>\u0420\u0430\u0431\u043e\u0442\u0430 \u0441 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430\u043c\u0438<\/h4>\n<p>  \u041a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u0432\u044b\u0448\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u043a\u043b\u044e\u0447\u0430\u0435\u0442 \u0432 \u0441\u0435\u0431\u044f \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0441\u0432\u043e\u0438 \u043e\u0448\u0438\u0431\u043a\u0438 \u0438 \u0442\u0430\u043a\u0436\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u0432\u0441\u0435\u0445 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0445 \u043f\u043e\u043b\u0435\u0439. \u041c\u043e\u0436\u0435\u0442 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u0447\u0442\u043e \u0441\u0430\u043c\u0438 \u043e\u0448\u0438\u0431\u043a\u0438 \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u0435 \u0441\u0442\u0440\u043e\u043a\u0438, \u043d\u043e \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u043e\u043d\u0438 \u043a\u043b\u0430\u0441\u0441\u044b \u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0438\u0440\u0443\u044e\u0449\u0438\u0435 \u043c\u0430\u0433\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043c\u0435\u0442\u043e\u0434 __toString() \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 \u0432\u044b\u0432\u043e\u0434\u0430. \u041f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u0444\u043e\u0440\u043c\u0430\u043c\u0438 \u0432\u044b \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043d\u0438\u043a\u043e\u0433\u0434\u0430 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442\u0435 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e \u044d\u0442\u043e\u0442 \u0434\u0435\u0444\u043e\u043b\u0442\u043d\u044b\u0439 \u0442\u0435\u043a\u0441\u0442. \u0412\u043c\u0435\u0441\u0442\u043e \u044d\u0442\u043e\u0433\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0435 \u0438\u0437 \u043a\u043b\u0430\u0441\u0441\u0430 \u043e\u0448\u0438\u0431\u043a\u0438 \u0435\u0435 \u0442\u0438\u043f \u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0430 \u0437\u0430\u0442\u0435\u043c \u0443\u0436\u0435 \u043a\u0440\u0430\u0441\u0438\u0432\u043e \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u0443\u0439\u0442\u0435, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"php\">if($error-&gt;type() === 'filter') {     if($error-&gt;filter() === 'minLength') {        $params = $error-&gt;parameters();        echo &quot;Please enter at least {$params[0]} characters&quot;;     } } <\/code><\/pre>\n<p>  \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u043c \u0445\u0435\u043b\u043f\u0435\u0440 \u043a\u043b\u0430\u0441\u0441\u043e\u043c \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043a\u0440\u0430\u0441\u0438\u0432\u0443\u044e \u043b\u043e\u043a\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u043e\u0448\u0438\u0431\u043e\u043a.<\/p>\n<h4>\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0434\u0430\u043d\u043d\u044b\u0445<\/h4>\n<p>  \u041d\u0443 \u0432\u043e\u0442 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u00ab\u043a\u0438\u043b\u043b\u0435\u0440 \u0444\u0438\u0447\u0430\u00bb, \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043f\u0440\u043e\u0432\u0430\u043b\u0438\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u043e\u0442 \u0442\u0430\u043a\u0443\u044e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443:<\/p>\n<pre><code class=\"php\">$data = array(     'name' =&gt; 'Pixie',          \/\/ 'home' \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0441\u0443\u0431\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442     'home' =&gt; array(         'location' =&gt; 'forest',         'name'     =&gt; 'Oak'     ),          \/\/ 'spells' \u043c\u0430\u0441\u0441\u0438\u0432 \u0441\u0443\u0431\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u043e\u0434\u043d\u043e\u0433\u043e \u0442\u0438\u043f\u0430,     \/\/ \u0438 \u0442\u0435\u043a\u0441\u0442\u043e\u0432\u044b\u043c \u043a\u043b\u044e\u0447\u043e\u043c (\u0435\u0433\u043e \u0442\u043e\u0436\u0435 \u043d\u0430\u0434\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c)     \/\/ of the same type     'spells' =&gt; array(         'charm' =&gt; array(             'name' =&gt; 'Charm Person',             'type' =&gt; 'illusion'         ),         'blast' =&gt; array(             'name' =&gt; 'Fire Blast',             'type' =&gt; 'evocation'         ),         \/\/ ....     ) );  $validator = $validate-&gt;validator(); $document = $validator-&gt;rule()-&gt;addDocument();  $document-&gt;valueField('name')     -&gt;required()     -&gt;addFilter()         -&gt;alpha()         -&gt;minLength(3);  \/\/ \u0421\u0443\u0431\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442 $homeDocument = $document-&gt;valueField('home')     -&gt;required()     -&gt;addDocument();  $homeDocument-&gt;valueField('location')     -&gt;required()     -&gt;addFilter()         -&gt;in(array('forest', 'meadow'));  $homeDocument-&gt;valueField('name')     -&gt;required()     -&gt;addFilter()         -&gt;alpha();  \/\/ \u041c\u0430\u0441\u0441\u0438\u0432 \u0441\u0443\u0431\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432 $spellsArray = $document-&gt;valueField('spells')     -&gt;required()     -&gt;addArrayOf()     -&gt;minCount(1);  \/\/ \u041f\u0440\u0430\u0432\u0438\u043b\u0430 \u0434\u043b\u044f \u043a\u043b\u044e\u0447\u0430 $spellDocument = $spellsArray     -&gt;valueKey()     -&gt;filter('alpha');  \/\/ \u041f\u0440\u0430\u0432\u0438\u043b\u0430 \u0434\u043b\u044f \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430         $spellDocument = $spellsArray     -&gt;valueItem()     -&gt;addDocument();  $spellDocument-&gt;valueField('name')     -&gt;required()     -&gt;addFilter()         -&gt;minLength(3);  $spellDocument-&gt;valueField('type')     -&gt;required()     -&gt;addFilter()         -&gt;alpha(); <\/code><\/pre>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0422\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u0430\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u043d\u044b\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"php\">$validator = $validate-&gt;validator(function($value) {     $value-&gt;document(function($document) {         $document             -&gt;field('name', function($name) {                 $name                     -&gt;required()                     -&gt;filter(array(                         'alpha',                         'minLength' =&gt; array(3)                     ));             })             -&gt;field('home', function($home) {                 $home                     -&gt;required()                     -&gt;document(function($home) {                                                  $home-&gt;field('location', function($location) {                             $location                                 -&gt;required()                                 -&gt;addFilter()                                     -&gt;in(array('forest', 'meadow'));                             });                                                  $home-&gt;field('name', function($name) {                             $name                                 -&gt;required()                                 -&gt;filter('alpha');                         });                     });             })             -&gt;field('spells', function($spells) {                 $spells-&gt;required()-&gt;arrayOf(function($spells){                     $spells                         -&gt;minCount(1)                         -&gt;key(function($key) {                             $key-&gt;filter('alpha');                         })                         -&gt;item(function($spell) {                             $spell-&gt;required()-&gt;document(function($spell) {                                 $spell-&gt;field('name', function($name) {                                     $name                                         -&gt;required()                                         -&gt;addFilter()                                             -&gt;minLength(3);                                 });                                                                      $spell-&gt;field('type', function($type) {                                     $type                                         -&gt;required()                                         -&gt;filter('alpha');                                 });                             });                     });                 });             });     }); }); <\/code><\/pre>\n<p>  <\/div>\n<\/div>\n<p>  \u0410\u043b\u044c\u0442\u0435\u0440\u043d\u0430\u0442\u0438\u0432\u043d\u0438\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 \u043d\u0430 \u043c\u043e\u0439 \u0432\u0437\u0433\u043b\u044f\u0434 \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u0447\u0438\u0442\u0430\u0431\u0435\u043b\u044c\u043d\u0435\u0435 \u0432 \u0442\u0430\u043a\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u0442\u0430\u043a \u043a\u0430\u043a \u0442\u0430\u0431\u0443\u043b\u044f\u0446\u0438\u044f \u043a\u043e\u0434\u0430 \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0435\u0442 \u0441 \u0442\u0430\u0431\u0443\u043b\u044f\u0446\u0438\u0435\u0439 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430.<\/p>\n<p>  \u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b<\/p>\n<pre><code class=\"php\">$result = $validator-&gt;validate($data);  var_dump($result-&gt;isValid()); \/\/bool(true)  \/\/ \u0414\u043e\u0431\u0430\u0432\u0438\u043c \u043e\u0448\u0438\u0431\u043e\u043a $data['name'] = ''; $data['spells']['charm']['name'] = '1';  \/\/ \u041d\u0435\u0432\u0430\u043b\u0438\u0434\u043d\u044b\u0439 \u0447\u0438\u0441\u0435\u043b\u044c\u043d\u044b\u0439 \u043a\u043b\u044e\u0447 $data['spells'][3] = $data['spells']['blast'];  $result = $validator-&gt;validate($data);  var_dump($result-&gt;isValid()); \/\/bool(false)  \/\/ \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 \u043e\u0448\u044b\u0431\u043e\u043a function printErrors($result) {     foreach($result-&gt;errors() as $error) {         echo $result-&gt;path().': '.$error.&quot;\\n&quot;;     }          foreach($result-&gt;invalidFields() as $result) {         printErrors($result);     } } printErrors($result);  \/* name: Value is empty spells.charm.name: Value did not pass filter 'minLength' spells.3: Value did not pass filter 'alpha' *\/ <\/code><\/pre>\n<h4>\u0414\u0435\u043c\u043e<\/h4>\n<p>  \u0427\u0442\u043e\u0431\u044b \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c Validate \u0441\u0432\u043e\u0438\u043c\u0438 \u0440\u0443\u043a\u0430\u043c\u0438 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e:<\/p>\n<pre><code class=\"diff\">git clone https:\/\/github.com\/phpixie\/validate cd validate\/examples   #\u0435\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0435\u0449\u0435 \u043d\u0435\u0442 \u041a\u043e\u043c\u043f\u043e\u0437\u0435\u0440\u0430 curl -sS https:\/\/getcomposer.org\/installer | php   php composer.phar install php simple.php php document.php <\/code><\/pre>\n<p>  \u0418 \u043a\u0441\u0442\u0430\u0442\u0438 \u043a\u0430\u043a \u0438 \u0443 \u0432\u0441\u0435\u0445 \u0434\u0440\u0443\u0433\u0438\u0445 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430\u0445 \u043e\u0442 PHPixie \u0432\u0430\u0441 \u0436\u0434\u0435\u0442 100% \u043f\u043e\u043a\u0440\u044b\u0442\u0438\u0435 \u043a\u043e\u0434\u0430 \u0442\u0435\u0441\u0442\u0430\u043c\u0438 \u0438 \u0440\u0430\u0431\u043e\u0442\u0430 \u043f\u043e\u0434 \u043b\u044e\u0431\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0435\u0439 PHP \u0441\u0442\u0430\u0440\u0448\u0435 5.3 (\u0432\u043a\u043b\u044e\u0447\u0430\u044f \u043d\u043e\u0432\u0443\u044e 7 \u0438 HHVM).               <\/p>\n<div class=\"clear\"><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/269709\/\"> http:\/\/habrahabr.ru\/post\/269709\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>       <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/post_images\/7cd\/47c\/66c\/7cd47c66c5df9f4b83f7886240a1cba2.png\" alt=\"image\" align=\"right\"\/><br \/>  \u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0432\u044b\u0448\u0435\u043b \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 PHPixie 3, \u0432 \u044d\u0442\u043e\u0442 \u0440\u0430\u0437 \u0434\u043b\u044f \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0438 \u0434\u0430\u043d\u043d\u044b\u0445. \u0411\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a \u0434\u043b\u044f PHP \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0437\u0430\u043d\u0438\u043c\u0430\u044e\u0442\u0441\u044f \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u0435\u0439 \u0443\u0436\u0435 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e, \u0437\u0430\u0447\u0435\u043c \u0442\u043e\u0433\u0434\u0430 \u043f\u0438\u0441\u0430\u0442\u044c \u0435\u0449\u0435 \u043e\u0434\u0438\u043d? \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0443 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0430 \u0438\u0437 \u043d\u0438\u0445 \u0435\u0441\u0442\u044c \u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u043a \u2014 \u043e\u043d\u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0441 \u043e\u0434\u043d\u043e\u043c\u0435\u0440\u043d\u044b\u043c\u0438 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445 \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u0443\u044f\u0441\u044c \u0432 \u043f\u0435\u0440\u0432\u0443\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u043d\u0430 \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u0444\u043e\u0440\u043c\u0430\u043c\u0438. \u0422\u0430\u043a\u043e\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u043d\u0435\u0438\u0437\u0431\u0435\u0436\u043d\u043e \u0443\u0441\u0442\u0430\u0440\u0435\u043b \u0432 \u043c\u0438\u0440\u0435 API \u0438 REST, \u0432\u0441\u0435 \u0447\u0430\u0449\u0435 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u044c\u0441\u044f \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u043e\u0431\u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u0437\u0430\u043f\u0440\u043e\u0441\u0430\u043c\u0438 \u0441\u043e \u0441\u043b\u043e\u0436\u043d\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043e\u0439. Validate \u0441 \u0441\u0430\u043c\u043e\u0433\u043e \u043d\u0430\u0447\u0430\u043b\u0430 \u0431\u044b\u043b \u0441\u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d \u043a\u0430\u043a \u0440\u0430\u0437 \u0447\u0442\u043e\u0431\u044b \u0441\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c\u0441\u044f \u0441 \u0442\u0430\u043a\u0438\u043c\u0438 \u0437\u0430\u0434\u0430\u0447\u0430\u043c\u0438. \u0418 \u0434\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u0432\u044b \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0435 PHPixie \u044d\u0442\u043e\u0442 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u043c\u043e\u0436\u0435\u0442 \u0432\u0430\u043c \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u0438\u0433\u043e\u0434\u0438\u0442\u044c\u0441\u044f.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-266994","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/266994","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=266994"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/266994\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=266994"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=266994"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=266994"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}