diff --git a/src/LangManager.php b/src/LangManager.php index 6131160..d71c88f 100644 --- a/src/LangManager.php +++ b/src/LangManager.php @@ -27,6 +27,7 @@ class LangManager 'validation.string_rule' => "Le champ :attribute doit être une chaîne de caractères.", 'validation.required_rule' => "Le champ :attribute est requis.", 'validation.max_length_rule' => "Le champ :attribute ne doit pas dépasser :max caractères.", + 'validation.min_length_rule' => "Le champ :attribute doit dépasser :min caractères.", 'validation.email_rule' => "Le champ :attribute doit être un e-mail." ], 'en' => [ @@ -37,6 +38,7 @@ class LangManager 'validation.string_rule' => "The :attribute field must be a string.", 'validation.required_rule' => "The :attribute field is required.", 'validation.max_length_rule' => "The :attribute field must not exceed :max characters.", + 'validation.min_length_rule' => "The :attribute field must exceed :min characters.", 'validation.email_rule' => "The :attribute field must be a email.", ], ]; diff --git a/src/Rules/MinLengthRule.php b/src/Rules/MinLengthRule.php new file mode 100644 index 0000000..66d4258 --- /dev/null +++ b/src/Rules/MinLengthRule.php @@ -0,0 +1,68 @@ +field = $field; + + // Get the mininum length from the parameters, defaulting to 0 if not set. + $minLength = $this->parameters[0] ?? 0; + + // Check if the value is a string and its length is within the specified mininum. + return is_string($value) && mb_strlen($value) >= $minLength; + } + + /** + * Get the validation error message for the max length rule. + * + * @return string Validation error message. + */ + public function message(): string + { + // Use LangManager to get a translated validation error message. + return LangManager::getTranslation('validation.min_length_rule', [ + 'attribute' => $this->field, + 'min' => $this->parameters[0] ?? 0, + ]); + } +} diff --git a/tests/Feature/RuleTest.php b/tests/Feature/RuleTest.php index c98867b..0adc4cf 100644 --- a/tests/Feature/RuleTest.php +++ b/tests/Feature/RuleTest.php @@ -3,6 +3,7 @@ use BlakvGhost\PHPValidator\LangManager; use BlakvGhost\PHPValidator\Rules\EmailRule; use BlakvGhost\PHPValidator\Rules\MaxLengthRule; +use BlakvGhost\PHPValidator\Rules\MinLengthRule; use BlakvGhost\PHPValidator\Rules\RequiredRule; use BlakvGhost\PHPValidator\Rules\StringRule; @@ -49,4 +50,15 @@ expect($validator->message())->toBe( LangManager::getTranslation('validation.string_rule', ['attribute' => 'field']) ); +}); + +it('validates min length rule successfully', function () { + $validator = new MinLengthRule([10]); + + expect($validator->passes('field', 'toolongvalue', []))->toBeTrue(); + expect($validator->passes('field', 'less', []))->toBeFalse(); + + expect($validator->message())->toBe( + LangManager::getTranslation('validation.min_length_rule', ['attribute' => 'field', 'min' => 10]) + ); }); \ No newline at end of file