diff --git a/README.md b/README.md index 870e239..fb7731f 100644 --- a/README.md +++ b/README.md @@ -58,12 +58,32 @@ try { } ``` +You can also customize the validation error messages + +```php +$data = [ + 'username' => 'BlakvGhost', +]; + +$validator = new Validator( + $data, + [ + 'username' => 'required|string', + ], + [ + 'username' => [ + 'required' => 'Votre nom d\'utilisateur doit être présent', + 'string' => 'Votre nom d\'utilisateur doit forcément être une chaîne de caractère', + ], + ] + ); +``` ## Features - `Predefined Rules`: PHPValidator comes with a set of predefined validation rules such as required, string, email, maxLength etc. -- `Custom Rules`: Easily create custom validation rules by implementing the `RuleInterface`. +- `Custom Rules`: Easily create custom validation rules by implementing the `Rule` Interface. - `Multilingual Support`: Customize validation error messages based on the application's language using the `LangManager`. @@ -259,7 +279,7 @@ PHPValidator provides a variety of predefined rules that you can use for data va ``` ## Custom Rule -In addition to the predefined rules, you can create custom validation rules by implementing the `RuleInterface`. Here's an example of how to create and use a custom rule: +In addition to the predefined rules, you can create custom validation rules by implementing the `Rule` Interface. Here's an example of how to create and use a custom rule: ### CustomPasswordRule.php @@ -267,10 +287,9 @@ In addition to the predefined rules, you can create custom validation rules by i // CustomPasswordRule.php namespace YourNameSpace\Rules; -use BlakvGhost\PHPValidator\Rules\RuleInterface; -use BlakvGhost\PHPValidator\LangManager; +use BlakvGhost\PHPValidator\Contracts\Rule; -class CustomPasswordRule implements RuleInterface +class CustomPasswordRule implements Rule { protected $field; @@ -300,6 +319,7 @@ class CustomPasswordRule implements RuleInterface use BlakvGhost\PHPValidator\Validator; use BlakvGhost\PHPValidator\ValidatorException; + use YourNameSpace\CustomPasswordRule; @@ -334,7 +354,8 @@ class CustomPasswordRule implements RuleInterface use BlakvGhost\PHPValidator\Validator; use BlakvGhost\PHPValidator\ValidatorException; - use BlakvGhost\PHPValidator\RulesMaped; + use BlakvGhost\PHPValidator\Mapping\RulesMaped; + use YourNameSpace\CustomPasswordRule; // Add your rule here using an alias and the full namespace of your custom class diff --git a/src/Rules/RuleInterface.php b/src/Contracts/Rule.php similarity index 81% rename from src/Rules/RuleInterface.php rename to src/Contracts/Rule.php index abd0f7d..3b99012 100644 --- a/src/Rules/RuleInterface.php +++ b/src/Contracts/Rule.php @@ -1,17 +1,17 @@ [ // French translations 'validation.empty_data' => 'Les données de validation ne peuvent pas être vides.', @@ -82,38 +82,4 @@ class LangManager 'validation.size' => "The :attribute field must have the required length :value.", ], ]; - - - /** - * Get the current language. - * - * @return string Current language code. - */ - private static function getLocal(): string - { - // Get the current language from environment variables, defaulting to 'en' (English) if not set. - return $_ENV['local'] ?? 'en'; - } - - /** - * Get a translated message for the given key. - * - * @param string $key Translation key. - * @param array|null $parameters Placeholder values to replace in the translated message. - * @return string Translated message. - */ - public static function getTranslation(string $key, ?array $parameters = []): string - { - // Get the translation for the current language and the provided key, or use the key itself if not found. - $translation = self::$translations[self::getLocal()][$key] ?? $key; - - // Replace placeholders in the translation with the provided values. - if ($parameters) { - foreach ($parameters as $placeholder => $value) { - $translation = str_replace(":$placeholder", $value, $translation); - } - } - - return $translation; - } } diff --git a/src/Lang/LangManager.php b/src/Lang/LangManager.php new file mode 100644 index 0000000..b014855 --- /dev/null +++ b/src/Lang/LangManager.php @@ -0,0 +1,51 @@ + $value) { + $translation = str_replace(":$placeholder", $value, $translation); + } + } + + return $translation; + } +} diff --git a/src/RulesMaped.php b/src/Mapping/RulesAlias.php similarity index 65% rename from src/RulesMaped.php rename to src/Mapping/RulesAlias.php index 7b6744e..23dd364 100644 --- a/src/RulesMaped.php +++ b/src/Mapping/RulesAlias.php @@ -1,18 +1,17 @@ UrlRule::class, 'ip' => ValidIpRule::class, ]; - - /** - * Get the mapping of rule aliases to their corresponding rule classes. - * - * @return array - */ - protected static function getRules(): array - { - return self::$rules; - } - - /** - * Get the rule class for a given alias. - * - * @param string $alias Rule alias to retrieve the corresponding rule class. - * @return string Rule class. - * @throws ValidatorException If the rule alias is not found. - */ - protected static function getRule(string $alias): string - { - if (isset(self::$rules[$alias]) && class_exists(self::$rules[$alias])) { - return self::$rules[$alias]; - } - - $translatedMessage = LangManager::getTranslation('validation.rule_not_found', [ - 'ruleName' => $alias, - ]); - - throw new ValidatorException($translatedMessage); - } - - /** - * Add a new rule to the mapping. - * - * @param string $alias Rule alias. - * @param string $className Rule class name. - * @return void - */ - public static function addRule(string $alias, string $className): void - { - self::$rules[$alias] = $className; - } } diff --git a/src/Mapping/RulesMaped.php b/src/Mapping/RulesMaped.php new file mode 100644 index 0000000..15be48c --- /dev/null +++ b/src/Mapping/RulesMaped.php @@ -0,0 +1,62 @@ + $alias, + ]); + + throw new ValidatorException($translatedMessage); + } + + /** + * Add a new rule to the mapping. + * + * @param string $alias Rule alias. + * @param string $className Rule class name. + * @return void + */ + public static function addRule(string $alias, string $className): void + { + self::$rules[$alias] = $className; + } +} diff --git a/src/Rules/AcceptedIfRule.php b/src/Rules/AcceptedIfRule.php index 3c22964..22aea29 100644 --- a/src/Rules/AcceptedIfRule.php +++ b/src/Rules/AcceptedIfRule.php @@ -11,9 +11,10 @@ namespace BlakvGhost\PHPValidator\Rules; -use BlakvGhost\PHPValidator\LangManager; +use BlakvGhost\PHPValidator\Contracts\Rule; +use BlakvGhost\PHPValidator\Lang\LangManager; -class AcceptedIfRule implements RuleInterface +class AcceptedIfRule implements Rule { /** * The name of the field being validated. @@ -60,8 +61,8 @@ public function passes(string $field, $value, array $data): bool /** * Get the validation error message for the accepted if rule. * - * @return string Validation error message. - */ + * @return string Validation error message. + */ public function message(): string { // Use LangManager to get a translated validation error message. diff --git a/src/Rules/AcceptedRule.php b/src/Rules/AcceptedRule.php index 80dc4b6..dc861f5 100644 --- a/src/Rules/AcceptedRule.php +++ b/src/Rules/AcceptedRule.php @@ -11,9 +11,10 @@ namespace BlakvGhost\PHPValidator\Rules; -use BlakvGhost\PHPValidator\LangManager; +use BlakvGhost\PHPValidator\Contracts\Rule; +use BlakvGhost\PHPValidator\Lang\LangManager; -class AcceptedRule implements RuleInterface +class AcceptedRule implements Rule { /** * The name of the field being validated. diff --git a/src/Rules/ActiveURLRule.php b/src/Rules/ActiveURLRule.php index 5b05489..f722e1e 100644 --- a/src/Rules/ActiveURLRule.php +++ b/src/Rules/ActiveURLRule.php @@ -11,9 +11,10 @@ namespace BlakvGhost\PHPValidator\Rules; -use BlakvGhost\PHPValidator\LangManager; +use BlakvGhost\PHPValidator\Contracts\Rule; +use BlakvGhost\PHPValidator\Lang\LangManager; -class ActiveURLRule implements RuleInterface +class ActiveURLRule implements Rule { /** * The name of the field being validated. diff --git a/src/Rules/AlphaNumericRule.php b/src/Rules/AlphaNumericRule.php index 8a2f74b..d2a0f8d 100644 --- a/src/Rules/AlphaNumericRule.php +++ b/src/Rules/AlphaNumericRule.php @@ -10,9 +10,10 @@ namespace BlakvGhost\PHPValidator\Rules; -use BlakvGhost\PHPValidator\LangManager; +use BlakvGhost\PHPValidator\Contracts\Rule; +use BlakvGhost\PHPValidator\Lang\LangManager; -class AlphaNumericRule implements RuleInterface +class AlphaNumericRule implements Rule { /** * Name of the field being validated. diff --git a/src/Rules/AlphaRule.php b/src/Rules/AlphaRule.php index 9e59632..10e3aeb 100644 --- a/src/Rules/AlphaRule.php +++ b/src/Rules/AlphaRule.php @@ -11,9 +11,10 @@ namespace BlakvGhost\PHPValidator\Rules; -use BlakvGhost\PHPValidator\LangManager; +use BlakvGhost\PHPValidator\Contracts\Rule; +use BlakvGhost\PHPValidator\Lang\LangManager; -class AlphaRule implements RuleInterface +class AlphaRule implements Rule { /** * Name of the field being validated. diff --git a/src/Rules/BooleanRule.php b/src/Rules/BooleanRule.php index 196c32e..df683e6 100644 --- a/src/Rules/BooleanRule.php +++ b/src/Rules/BooleanRule.php @@ -10,9 +10,10 @@ namespace BlakvGhost\PHPValidator\Rules; -use BlakvGhost\PHPValidator\LangManager; +use BlakvGhost\PHPValidator\Contracts\Rule; +use BlakvGhost\PHPValidator\Lang\LangManager; -class BooleanRule implements RuleInterface +class BooleanRule implements Rule { /** * Name of the field being validated. diff --git a/src/Rules/ConfirmedRule.php b/src/Rules/ConfirmedRule.php index 64df92a..372622f 100644 --- a/src/Rules/ConfirmedRule.php +++ b/src/Rules/ConfirmedRule.php @@ -11,9 +11,10 @@ namespace BlakvGhost\PHPValidator\Rules; -use BlakvGhost\PHPValidator\LangManager; +use BlakvGhost\PHPValidator\Contracts\Rule; +use BlakvGhost\PHPValidator\Lang\LangManager; -class ConfirmedRule implements RuleInterface +class ConfirmedRule implements Rule { /** * Name of the field being validated. diff --git a/src/Rules/EmailRule.php b/src/Rules/EmailRule.php index 3a14751..176ba1f 100644 --- a/src/Rules/EmailRule.php +++ b/src/Rules/EmailRule.php @@ -10,9 +10,10 @@ namespace BlakvGhost\PHPValidator\Rules; -use BlakvGhost\PHPValidator\LangManager; +use BlakvGhost\PHPValidator\Contracts\Rule; +use BlakvGhost\PHPValidator\Lang\LangManager; -class EmailRule implements RuleInterface +class EmailRule implements Rule { /** * Name of the field being validated. @@ -54,10 +55,10 @@ public function passes(string $field, $value, array $data): bool */ public function message(): string { - + // Use LangManager to get a translated validation error message. return LangManager::getTranslation('validation.email_rule', [ 'attribute' => $this->field, ]); } -} \ No newline at end of file +} diff --git a/src/Rules/FileRule.php b/src/Rules/FileRule.php index 1989873..2e1520d 100644 --- a/src/Rules/FileRule.php +++ b/src/Rules/FileRule.php @@ -11,9 +11,10 @@ namespace BlakvGhost\PHPValidator\Rules; -use BlakvGhost\PHPValidator\LangManager; +use BlakvGhost\PHPValidator\Contracts\Rule; +use BlakvGhost\PHPValidator\Lang\LangManager; -class FileRule implements RuleInterface +class FileRule implements Rule { /** * The name of the field being validated. diff --git a/src/Rules/InRule.php b/src/Rules/InRule.php index 2ca120b..0e654a1 100644 --- a/src/Rules/InRule.php +++ b/src/Rules/InRule.php @@ -11,9 +11,10 @@ namespace BlakvGhost\PHPValidator\Rules; -use BlakvGhost\PHPValidator\LangManager; +use BlakvGhost\PHPValidator\Contracts\Rule; +use BlakvGhost\PHPValidator\Lang\LangManager; -class InRule implements RuleInterface +class InRule implements Rule { /** * Name of the field being validated. diff --git a/src/Rules/JsonRule.php b/src/Rules/JsonRule.php index b2436d9..d63c059 100644 --- a/src/Rules/JsonRule.php +++ b/src/Rules/JsonRule.php @@ -10,9 +10,10 @@ namespace BlakvGhost\PHPValidator\Rules; -use BlakvGhost\PHPValidator\LangManager; +use BlakvGhost\PHPValidator\Contracts\Rule; +use BlakvGhost\PHPValidator\Lang\LangManager; -class JsonRule implements RuleInterface +class JsonRule implements Rule { /** * Name of the field being validated. @@ -44,11 +45,11 @@ public function passes(string $field, $value, array $data): bool // Set the field property for use in the message method. $this->field = $field; - if (!empty($value)) { - return is_string($value) && - is_array(json_decode($value, true)) ? true : false; - } - return false; + if (!empty($value)) { + return is_string($value) && + is_array(json_decode($value, true)) ? true : false; + } + return false; } /** diff --git a/src/Rules/LowerCaseRule.php b/src/Rules/LowerCaseRule.php index 223a58e..5bce432 100644 --- a/src/Rules/LowerCaseRule.php +++ b/src/Rules/LowerCaseRule.php @@ -11,9 +11,10 @@ namespace BlakvGhost\PHPValidator\Rules; -use BlakvGhost\PHPValidator\LangManager; +use BlakvGhost\PHPValidator\Contracts\Rule; +use BlakvGhost\PHPValidator\Lang\LangManager; -class LowerCaseRule implements RuleInterface +class LowerCaseRule implements Rule { /** * The name of the field being validated. diff --git a/src/Rules/MaxLengthRule.php b/src/Rules/MaxLengthRule.php index 7f6b76c..a19723b 100644 --- a/src/Rules/MaxLengthRule.php +++ b/src/Rules/MaxLengthRule.php @@ -11,9 +11,10 @@ namespace BlakvGhost\PHPValidator\Rules; -use BlakvGhost\PHPValidator\LangManager; +use BlakvGhost\PHPValidator\Contracts\Rule; +use BlakvGhost\PHPValidator\Lang\LangManager; -class MaxLengthRule implements RuleInterface +class MaxLengthRule implements Rule { /** * Name of the field being validated. diff --git a/src/Rules/MinLengthRule.php b/src/Rules/MinLengthRule.php index 66d4258..cf38a0c 100644 --- a/src/Rules/MinLengthRule.php +++ b/src/Rules/MinLengthRule.php @@ -11,9 +11,10 @@ namespace BlakvGhost\PHPValidator\Rules; -use BlakvGhost\PHPValidator\LangManager; +use BlakvGhost\PHPValidator\Contracts\Rule; +use BlakvGhost\PHPValidator\Lang\LangManager; -class MinLengthRule implements RuleInterface +class MinLengthRule implements Rule { /** * Name of the field being validated. diff --git a/src/Rules/NotInRule.php b/src/Rules/NotInRule.php index 460c403..e1a018c 100644 --- a/src/Rules/NotInRule.php +++ b/src/Rules/NotInRule.php @@ -10,9 +10,10 @@ namespace BlakvGhost\PHPValidator\Rules; -use BlakvGhost\PHPValidator\LangManager; +use BlakvGhost\PHPValidator\Contracts\Rule; +use BlakvGhost\PHPValidator\Lang\LangManager; -class NotInRule implements RuleInterface +class NotInRule implements Rule { /** * Name of the field being validated. diff --git a/src/Rules/NullableRule.php b/src/Rules/NullableRule.php index 41ad6c0..35b962a 100644 --- a/src/Rules/NullableRule.php +++ b/src/Rules/NullableRule.php @@ -11,9 +11,10 @@ namespace BlakvGhost\PHPValidator\Rules; -use BlakvGhost\PHPValidator\LangManager; +use BlakvGhost\PHPValidator\Contracts\Rule; +use BlakvGhost\PHPValidator\Lang\LangManager; -class NullableRule implements RuleInterface +class NullableRule implements Rule { /** * Name of the field being validated. diff --git a/src/Rules/NumericRule.php b/src/Rules/NumericRule.php index b6e0c56..654e5d4 100644 --- a/src/Rules/NumericRule.php +++ b/src/Rules/NumericRule.php @@ -11,9 +11,10 @@ namespace BlakvGhost\PHPValidator\Rules; -use BlakvGhost\PHPValidator\LangManager; +use BlakvGhost\PHPValidator\Contracts\Rule; +use BlakvGhost\PHPValidator\Lang\LangManager; -class NumericRule implements RuleInterface +class NumericRule implements Rule { /** * Name of the field being validated. diff --git a/src/Rules/PasswordRule.php b/src/Rules/PasswordRule.php index 7847cb1..1df6844 100644 --- a/src/Rules/PasswordRule.php +++ b/src/Rules/PasswordRule.php @@ -11,9 +11,10 @@ namespace BlakvGhost\PHPValidator\Rules; -use BlakvGhost\PHPValidator\LangManager; +use BlakvGhost\PHPValidator\Contracts\Rule; +use BlakvGhost\PHPValidator\Lang\LangManager; -class PasswordRule implements RuleInterface +class PasswordRule implements Rule { /** * Name of the field being validated. diff --git a/src/Rules/RequiredRule.php b/src/Rules/RequiredRule.php index 0c06056..08be9e2 100644 --- a/src/Rules/RequiredRule.php +++ b/src/Rules/RequiredRule.php @@ -11,9 +11,10 @@ namespace BlakvGhost\PHPValidator\Rules; -use BlakvGhost\PHPValidator\LangManager; +use BlakvGhost\PHPValidator\Contracts\Rule; +use BlakvGhost\PHPValidator\Lang\LangManager; -class RequiredRule implements RuleInterface +class RequiredRule implements Rule { /** * Name of the field being validated. diff --git a/src/Rules/RequiredWithRule.php b/src/Rules/RequiredWithRule.php index e2841b1..6b67435 100644 --- a/src/Rules/RequiredWithRule.php +++ b/src/Rules/RequiredWithRule.php @@ -10,9 +10,10 @@ namespace BlakvGhost\PHPValidator\Rules; -use BlakvGhost\PHPValidator\LangManager; +use BlakvGhost\PHPValidator\Contracts\Rule; +use BlakvGhost\PHPValidator\Lang\LangManager; -class RequiredWithRule implements RuleInterface +class RequiredWithRule implements Rule { /** * Name of the field being validated. diff --git a/src/Rules/SameRule.php b/src/Rules/SameRule.php index 8546a5c..73246d3 100644 --- a/src/Rules/SameRule.php +++ b/src/Rules/SameRule.php @@ -11,9 +11,10 @@ namespace BlakvGhost\PHPValidator\Rules; -use BlakvGhost\PHPValidator\LangManager; +use BlakvGhost\PHPValidator\Contracts\Rule; +use BlakvGhost\PHPValidator\Lang\LangManager; -class SameRule implements RuleInterface +class SameRule implements Rule { /** * Name of the field being validated. diff --git a/src/Rules/SizeRule.php b/src/Rules/SizeRule.php index 9648e2a..454eaee 100644 --- a/src/Rules/SizeRule.php +++ b/src/Rules/SizeRule.php @@ -10,9 +10,10 @@ namespace BlakvGhost\PHPValidator\Rules; -use BlakvGhost\PHPValidator\LangManager; +use BlakvGhost\PHPValidator\Contracts\Rule; +use BlakvGhost\PHPValidator\Lang\LangManager; -class SizeRule implements RuleInterface +class SizeRule implements Rule { /** * Name of the field being validated. @@ -55,7 +56,7 @@ public function passes(string $field, $value, array $data): bool return $value == $total; } - + if (is_array($value)) { return count($value) == $total; @@ -66,10 +67,10 @@ public function passes(string $field, $value, array $data): bool if (isset($_FILES[$value]) && $_FILES[$value]["error"] == 0) { // Get the file size in bytes $size = $_FILES[$value]["size"]; - + // Convert bytes to kilobytes $size_kb = $size / 1024; // kilobytes - + return $size_kb == $total; } diff --git a/src/Rules/StringRule.php b/src/Rules/StringRule.php index 64b90a7..6754906 100644 --- a/src/Rules/StringRule.php +++ b/src/Rules/StringRule.php @@ -11,9 +11,10 @@ namespace BlakvGhost\PHPValidator\Rules; -use BlakvGhost\PHPValidator\LangManager; +use BlakvGhost\PHPValidator\Contracts\Rule; +use BlakvGhost\PHPValidator\Lang\LangManager; -class StringRule implements RuleInterface +class StringRule implements Rule { /** * Name of the field being validated. diff --git a/src/Rules/UpperCaseRule.php b/src/Rules/UpperCaseRule.php index 3e0757f..5d7452f 100644 --- a/src/Rules/UpperCaseRule.php +++ b/src/Rules/UpperCaseRule.php @@ -11,9 +11,10 @@ namespace BlakvGhost\PHPValidator\Rules; -use BlakvGhost\PHPValidator\LangManager; +use BlakvGhost\PHPValidator\Contracts\Rule; +use BlakvGhost\PHPValidator\Lang\LangManager; -class UpperCaseRule implements RuleInterface +class UpperCaseRule implements Rule { /** * The name of the field being validated. diff --git a/src/Rules/UrlRule.php b/src/Rules/UrlRule.php index 3eacefe..768fa00 100644 --- a/src/Rules/UrlRule.php +++ b/src/Rules/UrlRule.php @@ -10,9 +10,10 @@ namespace BlakvGhost\PHPValidator\Rules; -use BlakvGhost\PHPValidator\LangManager; +use BlakvGhost\PHPValidator\Contracts\Rule; +use BlakvGhost\PHPValidator\Lang\LangManager; -class UrlRule implements RuleInterface +class UrlRule implements Rule { /** * Name of the field being validated. @@ -44,7 +45,7 @@ public function passes(string $field, $value, array $data): bool // Set the field property for use in the message method. $this->field = $field; - return filter_var($value, FILTER_VALIDATE_URL); + return filter_var($value, FILTER_VALIDATE_URL); } /** diff --git a/src/Rules/ValidIpRule.php b/src/Rules/ValidIpRule.php index ca68017..5097c67 100644 --- a/src/Rules/ValidIpRule.php +++ b/src/Rules/ValidIpRule.php @@ -10,9 +10,10 @@ namespace BlakvGhost\PHPValidator\Rules; -use BlakvGhost\PHPValidator\LangManager; +use BlakvGhost\PHPValidator\Contracts\Rule; +use BlakvGhost\PHPValidator\Lang\LangManager; -class ValidIpRule implements RuleInterface +class ValidIpRule implements Rule { /** * Name of the field being validated. @@ -44,7 +45,7 @@ public function passes(string $field, $value, array $data): bool // Set the field property for use in the message method. $this->field = $field; - return filter_var($value, FILTER_VALIDATE_IP); + return filter_var($value, FILTER_VALIDATE_IP); } /** diff --git a/src/Validator.php b/src/Validator.php index 3aa8a46..c315c46 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -11,6 +11,8 @@ namespace BlakvGhost\PHPValidator; +use BlakvGhost\PHPValidator\Lang\LangManager; +use BlakvGhost\PHPValidator\Mapping\RulesMaped; use BlakvGhost\PHPValidator\Rules\RuleInterface; use BlakvGhost\PHPValidator\ValidatorException; @@ -24,9 +26,13 @@ class Validator extends RulesMaped * * @param array $data Data to be validated. * @param array $rules Validation rules to apply. + * @param array $messages Validation rules messages to apply when failed. */ - public function __construct(private array $data, private array $rules) - { + public function __construct( + private array $data, + private array $rules, + private array $messages = [] + ) { $this->validateConstructorInputs(); $this->validate(); } @@ -97,7 +103,7 @@ protected function validate() $validator = new $ruleClass($parameters); - $this->checkPasses($validator, $field); + $this->checkPasses($validator, $field, $ruleName); } } } @@ -108,11 +114,17 @@ protected function validate() * * @param mixed $validator Instance of the rule to check. * @param string $field Field associated with the rule. + * @param ?string $ruleName Associated rule alias. */ - protected function checkPasses(mixed $validator, string $field) + protected function checkPasses(mixed $validator, string $field, ?string $ruleName = null) { if (isset($this->data[$field]) && !$validator->passes($field, $this->data[$field], $this->data)) { - $this->addError($field, $validator->message()); + + $assert = isset($ruleName) && isset($this->messages[$field][$ruleName]); + + $message = $assert ? $this->messages[$field][$ruleName] : $validator->message(); + + $this->addError($field, $message); } } diff --git a/tests/Feature/RuleTest.php b/tests/Feature/RuleTest.php index ae56eba..c80709b 100644 --- a/tests/Feature/RuleTest.php +++ b/tests/Feature/RuleTest.php @@ -1,6 +1,6 @@ 'value', 'other_field' => 'different_value'], ['field' => 'same:other_field']); expect($validator->isValid())->toBeFalse(); - + $validator = new Validator(['field' => 'value'], ['field' => 'same:other_field']); expect($validator->isValid())->toBeFalse(); @@ -143,7 +143,7 @@ $validator = new Validator(['password' => 'StrongPwd1'], ['password' => 'password:8']); expect($validator->isValid())->toBeTrue(); - + $validator = new Validator(['password' => 'StrongPwd1'], ['password' => 'password']); expect($validator->isValid())->toBeTrue(); @@ -152,10 +152,10 @@ $validator = new Validator(['password' => 'lowercase1'], ['password' => 'password']); expect($validator->isValid())->toBeFalse(); - + $validator = new Validator(['password' => 'UPPERCASE1'], ['password' => 'password']); expect($validator->isValid())->toBeFalse(); - + $validator = new Validator(['password' => 'NoDigit'], ['password' => 'password']); expect($validator->isValid())->toBeFalse(); @@ -363,7 +363,7 @@ $validator = new Validator(['field' => false], ['field' => 'bool']); expect($validator->isValid())->toBeTrue(); - + $validator = new Validator(['field' => 'string'], ['field' => 'bool']); expect($validator->isValid())->toBeFalse(); @@ -378,10 +378,10 @@ $validator = new Validator(['field' => ""], ['field' => 'json']); expect($validator->isValid())->toBeFalse(); - + $validator = new Validator(['field' => '{"name":"vishal", "email": "abc@gmail.com"}'], ['field' => 'json']); expect($validator->isValid())->toBeTrue(); - + $validator = new Validator(['field' => '{name:vishal, email: abc@gmail.com}'], ['field' => 'json']); expect($validator->isValid())->toBeFalse(); @@ -393,7 +393,7 @@ }); it('validates url rule successfully', function () { - + $validator = new Validator(['field' => 'http://google.com'], ['field' => 'url']); expect($validator->isValid())->toBeTrue(); diff --git a/tests/Feature/ValidatorTest.php b/tests/Feature/ValidatorTest.php index 6a2b5c8..1ae3d98 100644 --- a/tests/Feature/ValidatorTest.php +++ b/tests/Feature/ValidatorTest.php @@ -1,8 +1,8 @@ new Validator([], ['name' => 'string'])) @@ -18,3 +18,21 @@ expect(fn () => new Validator(['name' => 'John'], ['name' => 'nonexistent'])) ->toThrow(ValidatorException::class, LangManager::getTranslation('validation.rule_not_found', ['ruleName' => 'nonexistent'])); }); + +it('validates rule with custom error message', function () { + + $errorMessage = "Je teste une règle custom"; + + $validator = new Validator( + ['field' => ''], + ['field' => 'required'], + [ + 'field' => [ + 'required' => $errorMessage + ] + ] + ); + expect($validator->isValid())->toBeFalse(); + + expect($validator->getErrors()['field'][0])->toBe($errorMessage); +});