|
| 1 | +#### Yii2 validator for complex array structures |
| 2 | +Validator for array attributes, unlike builtin "each" validator, that support only one rule, this validator can |
| 3 | + * validate multiple array attributes and even nested data structures |
| 4 | + * All keys that should be present in array must be described, for optional keys default value should be set |
| 5 | + * When input array not contains key defined in rules, this key added automatically with null value |
| 6 | + * When input array contains key not defined in rules, "unexpected item" error will be defined |
| 7 | + |
| 8 | +#### Installation |
| 9 | + |
| 10 | +```composer require insolita/yii2-array-structure-validator ``` |
| 11 | + |
| 12 | +#### Usage |
| 13 | + |
| 14 | +For a simple array with known keys like `['id'=>1, 'name'=>'John Doe']`; |
| 15 | + |
| 16 | +```php |
| 17 | + |
| 18 | +public function rules() |
| 19 | +{ |
| 20 | + return [ |
| 21 | + //... |
| 22 | + ['simpleArray', ArrayStructureValidator::class, |
| 23 | + 'rules'=>[ |
| 24 | + 'id'=>[['required'], ['integer','min'=>0]], |
| 25 | + 'name'=>[['required'], ['string', 'max'=>100]], |
| 26 | + 'sex'=>[['default', 'value'=>'male'], ['in','range'=>['male','female']] |
| 27 | + ]] |
| 28 | + ], |
| 29 | + ]; |
| 30 | +} |
| 31 | +``` |
| 32 | +For multidimensional arrays like ` |
| 33 | +[ |
| 34 | + ['id'=>1, 'name'=>'John Doe'], |
| 35 | + ['id'=>2, 'name'=>'Jane Doe','sex'=>'female'], |
| 36 | + ... |
| 37 | +]` set each = true |
| 38 | + |
| 39 | +```php |
| 40 | + |
| 41 | +public function rules() |
| 42 | +{ |
| 43 | + return [ |
| 44 | + //... |
| 45 | + [['multiArray', 'some', 'attrs'], 'required'], |
| 46 | + ['multiArray', ArrayStructureValidator::class, |
| 47 | + 'each'=>true, |
| 48 | + 'rules'=>[ |
| 49 | + 'id'=>[['required'], ['integer','min'=>0]], |
| 50 | + 'name'=>[['required'], ['string', 'max'=>100]], |
| 51 | + 'sex'=>[['default', 'value'=>'male'], ['in','range'=>['male','female']] |
| 52 | + ]] |
| 53 | + ] |
| 54 | + ]; |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +For nested structures like |
| 59 | +``` |
| 60 | +[ |
| 61 | + 'user'=>['id'=>1, 'name'=>'John Doe'], |
| 62 | + 'coords'=>[['x'=>1, 'y'=>2],['x'=>3,'y'=>4]] |
| 63 | +] |
| 64 | +``` |
| 65 | +```php |
| 66 | + |
| 67 | +public function rules() |
| 68 | +{ |
| 69 | + return [ |
| 70 | + //... |
| 71 | + ['complexArray', ArrayStructureValidator::class, |
| 72 | + 'rules'=>[ |
| 73 | + 'user'=>[[ArrayStructureValidator::class, |
| 74 | + 'rules'=>[ |
| 75 | + 'id'=>[['required'], ['integer','min'=>0]], |
| 76 | + 'name'=>[['required'], ['string', 'max'=>100]], |
| 77 | + ]]], |
| 78 | + 'coords'=>[[ArrayStructureValidator::class, |
| 79 | + 'each'=>true, |
| 80 | + 'rules'=>[ |
| 81 | + 'x'=>[['required'], ['integer','min'=>0]], |
| 82 | + 'y'=>[['required'], ['integer','min'=>0]], |
| 83 | + ], 'min'=>1, 'max'=>5]], |
| 84 | + ], 'min'=>2, 'max'=>2] |
| 85 | + ]; |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +Model scenarios supported |
| 90 | + |
| 91 | +```php |
| 92 | +public function rules() |
| 93 | +{ |
| 94 | + return [ |
| 95 | + //... |
| 96 | + ['conditional', ArrayStructureValidator::class, |
| 97 | + 'rules'=>[ |
| 98 | + 'a'=>[['integer','min'=>0]], //will be checked on any scenario |
| 99 | + 'b'=>[ |
| 100 | + ['default', 'value'=>1, 'on'=>['create']], |
| 101 | + ['integer', 'max'=>10, 'except'=>['create']], |
| 102 | + ['required', 'on'=>['update']], |
| 103 | + ['integer', 'max'=>1000, 'on'=>['update']], |
| 104 | + ] |
| 105 | + ] |
| 106 | + ] |
| 107 | + ]; |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +Closure and Inline validators supported, but with signature different from default |
| 112 | + |
| 113 | +Inline method in model class |
| 114 | + |
| 115 | +```php |
| 116 | +public function rules() |
| 117 | +{ |
| 118 | + return [ |
| 119 | + ['array', ArrayStructureValidator::class, 'rules'=>[ |
| 120 | + 'item'=>[['required'], ['customValidator']] |
| 121 | + ]] |
| 122 | + ]; |
| 123 | +} |
| 124 | + |
| 125 | +public function customValidator($attribute, $model, $index, $baseModel, $baseAttribute){ |
| 126 | + /** |
| 127 | + * $model - Dynamic model with attributes equals value data, or value row, if used with each=>true |
| 128 | + * $attribute - current keyName |
| 129 | + * $index - current array index for multidimensional arrays, or null |
| 130 | + * $baseModel - instance of initial model, where validator was attached |
| 131 | + * $baseAttribute - name of initial attributed, where validator was attached |
| 132 | + |
| 133 | + * access to validated value - $model->$attribute |
| 134 | + * access to whole validated array $baseModel->$baseAttribute |
| 135 | + * $model->addError($attribute, '[{index}][{attribute}] Error message', ['index'=>$index]); |
| 136 | +*/ |
| 137 | +} |
| 138 | +``` |
| 139 | + |
| 140 | +When conditions supported (But not whenClient!) |
| 141 | + |
| 142 | +```php |
| 143 | + |
| 144 | +public function rules() |
| 145 | +{ |
| 146 | + return [ |
| 147 | + ['conditional', ArrayStructureValidator::class, |
| 148 | + 'rules'=>[ |
| 149 | + 'x'=>[['safe']], |
| 150 | + 'y'=>[ |
| 151 | + ['default', 'value'=>1, 'when'=>fn(DynamicModel $model) => $model->x < 10], |
| 152 | + [ |
| 153 | + 'default', |
| 154 | + 'value'=>5, |
| 155 | + 'when'=>function($model, $attribute, $index, $baseModel, $baseAttribute){ |
| 156 | + return count($baseModel->$baseAttribute) > 5; |
| 157 | + }], |
| 158 | + ] |
| 159 | + ]] |
| 160 | + ]; |
| 161 | +} |
| 162 | +``` |
| 163 | + |
| 164 | +#### Note: |
0 commit comments