CreateValidatorFromForm.php
2 years ago
CreateValidatorFromFormFields.php
2 years ago
UpdateValidationRulesWithOptionalAsDefault.php
2 years ago
CreateValidatorFromForm.php
34 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 3.0.0 Update validation rules with optional as default. |
| 14 | * @since 2.24.0 |
| 15 | */ |
| 16 | public function __invoke(Form $form, array $values): Validator |
| 17 | { |
| 18 | $labels = []; |
| 19 | $rules = []; |
| 20 | |
| 21 | foreach ($form->getFields() as $field) { |
| 22 | $rules[$field->getName()] = (new UpdateValidationRulesWithOptionalAsDefault())( |
| 23 | $field->getValidationRules() |
| 24 | ); |
| 25 | |
| 26 | if (method_exists($field, 'getLabel')) { |
| 27 | $labels[$field->getName()] = $field->getLabel(); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | return new Validator($rules, $values, $labels); |
| 32 | } |
| 33 | } |
| 34 |