PHP code example of insolita / yii2-array-structure-validator

1. Go to this page and download the library: Download insolita/yii2-array-structure-validator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

insolita / yii2-array-structure-validator example snippets



public function rules() 
{
   return [
   //...
       ['simpleArray', ArrayStructureValidator::class, 
          'rules'=>[
                 'id'=>[[']]
            ]]
       ],
   ];
}


public function rules() 
{
   return [
   //...
       [['multiArray', 'some', 'attrs'], ''rules'=>[
                 'id'=>[['range'=>['male','female']]
            ]]
       ]
   ];
}


public function rules() 
{
   return [
   //...
       ['complexArray', ArrayStructureValidator::class, 
        'rules'=>[
                 'user'=>[[ArrayStructureValidator::class, 
                       'rules'=>[
                           'id'=>[['   'x'=>[['

public function rules() 
{
   return [
   //...
        ['conditional', ArrayStructureValidator::class, 
        'rules'=>[
                  'a'=>[['integer','min'=>0]], //will be checked on any scenario
                  'b'=>[
                          ['default', 'value'=>1, 'on'=>['create']],
                          ['integer', 'max'=>10, 'except'=>['create']],
                          ['

public function rules() 
{
   return [
      ['array', ArrayStructureValidator::class,  'rules'=>[
             'item'=>[['- Dynamic model with attributes equals value data, or value row, if used with each=>true
 * $attribute - current keyName
 * $index - current array index for multidimensional arrays, or null
 * $baseModel - instance of initial model, where validator was attached
 * $baseAttribute - name of initial attributed, where validator was attached

 * access to validated value - $model->$attribute
 * access to whole validated array  $baseModel->$baseAttribute
 * $model->addError($attribute, '[{index}][{attribute}] Error message', ['index'=>$index]);
*/
}


public function rules() 
{
   return [
      ['conditional', ArrayStructureValidator::class, 
          'rules'=>[
               'x'=>[['safe']],
               'y'=>[
                   ['default', 'value'=>1, 'when'=>fn(DynamicModel $model) => $model->x < 10],
                   [
                      'default',
                      'value'=>5,
                      'when'=>function($model, $attribute, $index, $baseModel, $baseAttribute){
                              return count($baseModel->$baseAttribute) > 5;
                         }],
                   ]
           ]]
   ];
}