Skip to content

Commit

Permalink
Merge pull request #50 from SandroMiguel/feat/multi-lang-fields
Browse files Browse the repository at this point in the history
feat(validator.php): add support for specific variant multilingual fi…
  • Loading branch information
SandroMiguel authored Jan 20, 2025
2 parents d13b0b3 + cbcb293 commit dac4f6b
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 12 deletions.
41 changes: 40 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 4 additions & 2 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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";
Expand Down
40 changes: 31 additions & 9 deletions tests/RequiredTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
* @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 <[email protected]>
* @version 2.0.0 (2020/09/16)
*/

declare(strict_types=1);
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit dac4f6b

Please sign in to comment.