From cbcb2933ebcc25d2059e67f4deae825d4f667c95 Mon Sep 17 00:00:00 2001 From: SandroMiguel Date: Mon, 20 Jan 2025 19:29:44 +0000 Subject: [PATCH] feat(validator.php): add support for specific variant multilingual fields --- README.md | 41 ++++++++++++++++++++++++++++++++++++++++- src/Validator.php | 6 ++++-- tests/RequiredTest.php | 40 +++++++++++++++++++++++++++++++--------- 3 files changed, 75 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 1e683b6..a797bf0 100755 --- a/README.md +++ b/README.md @@ -382,7 +382,11 @@ $validator->addCustomMessages( #### Handling Multi-Name Fields -With Verum PHP, you can handle multi-name fields more effectively. These are fields that include language identifiers or other variations in their names. For example, if you have fields like `title.en`, `title.pt`, `description.en`, and `description.pt`, you can specify rules for them using wildcards. +With Verum PHP, you can handle multi-name fields more effectively. These are fields that include language identifiers or other variations in their names. For example, if you have fields like `title.en`, `title.pt`, `description.en`, and `description.pt`, you can specify rules for them using wildcards or by targeting specific fields. + +##### Using Wildcards to Apply Rules to All Variants + +You can define rules for all variants of a field using the `*` wildcard. For example: ```php $rules = [ @@ -439,6 +443,41 @@ Output example: } ``` +##### Validating Specific Variants + +If you only want to validate a specific variant of a field, you can target it directly. For example, to validate only the English title and make titles in other languages optional: + +```php +$rules = [ + 'title.en' => [ + 'rules' => [ + RuleEnum::REQUIRED, + ], + ], +]; + +$validator = new Validator($_POST, $rules); +// ... +``` + +Output example: + +```json +{ + "valid": false, + "errors": { + "title.en": { + "label": null, + "rules": { + "required": "This field is required." + } + } + } +} +``` + +This allows you to apply rules more precisely, ensuring flexibility in how multi-name fields are handled based on your application's needs. + ## Custom validations You can use your custom validations and inject the error message. diff --git a/src/Validator.php b/src/Validator.php index 48f5cd4..a71ce98 100755 --- a/src/Validator.php +++ b/src/Validator.php @@ -205,8 +205,8 @@ public function validate(): bool foreach ($fieldConfig['rules'] as $key => $value) { [$ruleName, $ruleValues] = $this->getRuleData($key, $value); - // Check if is a multi-name field - $isMultiNameField = \strpos($fieldName, '.') !== false; + // Check if the field is a multi-language field with uniform rules + $isMultiNameField = \strpos($fieldName, '.*') !== false; $fieldValue = $isMultiNameField ? $this->getMultiNameFieldValues($fieldName) @@ -242,6 +242,8 @@ public function validate(): bool $this->debugFieldName($fullFieldName, $baseFieldName); + // Check if the rule is applicable for this field + // echo "\n - Rule: " . $ruleName . ' - ' . $fieldName . ' - ' . $baseFieldName; if (\strpos($fieldName, $baseFieldName) !== 0) { if ($this->debugMode) { echo "\n4 Rule not applicable for this field"; diff --git a/tests/RequiredTest.php b/tests/RequiredTest.php index 598fbd1..a24f554 100755 --- a/tests/RequiredTest.php +++ b/tests/RequiredTest.php @@ -3,15 +3,10 @@ /** * RequiredTest. * - * PHP Version 7.2.11-3 - * - * @package Verum-PHP - * @license MIT https://github.com/SandroMiguel/verum-php/blob/master/LICENSE - * @author Sandro Miguel Marques - * @copyright 2020 Sandro - * @since Verum-PHP 1.0.0 - * @version 2.0.0 (2020/09/16) - * @link https://github.com/SandroMiguel/verum-php + * @package Verum-PHP + * @license MIT https://github.com/SandroMiguel/verum-php/blob/master/LICENSE + * @author Sandro Miguel Marques + * @version 2.0.0 (2020/09/16) */ declare(strict_types=1); @@ -234,6 +229,33 @@ public function testValidateNullForMixedFields(): void $this->assertFalse($isValid); } + /** + * A required rule for "subject.pt" should fail if its value is null, even if + * "subject.en" is valid. + */ + public function testValidateRequiredForSpecificCompositeField(): void + { + $fieldValues = [ + 'subject.en' => 'hello en', + ]; + + $fieldRules = [ + 'subject.pt' => [ + 'rules' => [RuleEnum::REQUIRED], + ], + ]; + + $validator = new Validator($fieldValues, $fieldRules); + $isValid = $validator->validate(); + + $this->assertFalse($isValid); + $this->assertArrayHasKey('subject.pt', $validator->getErrors()); + $this->assertEquals( + 'This field is required.', + $validator->getErrors()['subject.pt']['rules']['required'] + ); + } + /** * A field with a rule defined but not present in the payload should not * pass validation.