CreateValidatorFromForm.php
31 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Give\Framework\FieldsAPI\Actions; |
| 6 | |
| 7 | use Give\Framework\FieldsAPI\Form; |
| 8 | use Give\Vendors\StellarWP\Validation\Validator; |
| 9 | |
| 10 | class CreateValidatorFromForm |
| 11 | { |
| 12 | /** |
| 13 | * @since 2.24.0 |
| 14 | */ |
| 15 | public function __invoke(Form $form, array $values): Validator |
| 16 | { |
| 17 | $labels = []; |
| 18 | $rules = []; |
| 19 | |
| 20 | foreach ($form->getFields() as $field) { |
| 21 | $rules[$field->getName()] = $field->getValidationRules(); |
| 22 | |
| 23 | if (method_exists($field, 'getLabel')) { |
| 24 | $labels[$field->getName()] = $field->getLabel(); |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | return new Validator($rules, $values, $labels); |
| 29 | } |
| 30 | } |
| 31 |