diff --git a/src/Form.php b/src/Form.php index f6853d7..227a0ae 100644 --- a/src/Form.php +++ b/src/Form.php @@ -139,6 +139,8 @@ public function __construct() protected function test($rule, $valueToTest, $fieldName = 'item'): bool { + $expandedErrors = false; + if (is_string($rule)) { $rule = preg_match_all('/[^|<>]+(?:<[^>]+>)?/', $rule, $matches); $rule = $matches[0]; @@ -148,6 +150,10 @@ protected function test($rule, $valueToTest, $fieldName = 'item'): bool return true; } + if (in_array('expanded', $rule)) { + $expandedErrors = true; + } + foreach ($rule as $currentRule) { $param = []; @@ -157,6 +163,10 @@ protected function test($rule, $valueToTest, $fieldName = 'item'): bool continue; } + if ($currentRule === 'expanded') { + continue; + } + if (preg_match('/^[a-zA-Z]+<(.*(\|.*)*)>$/', $currentRule)) { $ruleParts = explode('<', $currentRule); $ruleParams = str_replace('>', '', $ruleParts[1]); @@ -182,11 +192,19 @@ protected function test($rule, $valueToTest, $fieldName = 'item'): bool } if (!$valueToTest) { - $this->addError($fieldName, str_replace( - ['{field}', '{Field}', '{value}'], - [$fieldName, ucfirst($fieldName), is_array($valueToTest) ? json_encode($valueToTest) : $valueToTest], - $this->messages['required'] ?? '{Field} is invalid!' - )); + if ($expandedErrors) { + $this->addError($fieldName, str_replace( + ['{field}', '{Field}', '{value}'], + [$fieldName, ucfirst($fieldName), is_array($valueToTest) ? json_encode($valueToTest) : $valueToTest], + $this->messages['required'] ?? '{Field} is invalid!' + )); + } else { + $this->errors[$fieldName] = str_replace( + ['{field}', '{Field}', '{value}'], + [$fieldName, ucfirst($fieldName), is_array($valueToTest) ? json_encode($valueToTest) : $valueToTest], + $this->messages['required'] ?? '{Field} is invalid!' + ); + } return false; } @@ -201,14 +219,25 @@ protected function test($rule, $valueToTest, $fieldName = 'item'): bool $param = [$param]; } - $this->addError($fieldName, sprintf( - str_replace( - ['{field}', '{Field}', '{value}'], - [$fieldName, ucfirst($fieldName), is_array($valueToTest) ? json_encode($valueToTest) : $valueToTest], - $this->messages[$currentRule] ?? '{Field} is invalid!' - ), - ...$param, - )); + if ($expandedErrors) { + $this->addError($fieldName, sprintf( + str_replace( + ['{field}', '{Field}', '{value}'], + [$fieldName, ucfirst($fieldName), is_array($valueToTest) ? json_encode($valueToTest) : $valueToTest], + $this->messages[$currentRule] ?? '{Field} is invalid!' + ), + ...$param, + )); + } else { + $this->errors[$fieldName] = sprintf( + str_replace( + ['{field}', '{Field}', '{value}'], + [$fieldName, ucfirst($fieldName), is_array($valueToTest) ? json_encode($valueToTest) : $valueToTest], + $this->messages[$currentRule] ?? '{Field} is invalid!' + ), + ...$param, + ); + } } continue; @@ -228,14 +257,25 @@ protected function test($rule, $valueToTest, $fieldName = 'item'): bool FILTER_VALIDATE_BOOLEAN ) ) { - $this->addError($fieldName, sprintf( - str_replace( - ['{field}', '{Field}', '{value}'], - [$fieldName, ucfirst($fieldName), is_array($valueToTest) ? json_encode($valueToTest) : $valueToTest], - $this->messages[$currentRule] ?? '{Field} is invalid!' - ), - ...$param, - )); + if ($expandedErrors) { + $this->addError($fieldName, sprintf( + str_replace( + ['{field}', '{Field}', '{value}'], + [$fieldName, ucfirst($fieldName), is_array($valueToTest) ? json_encode($valueToTest) : $valueToTest], + $this->messages[$currentRule] ?? '{Field} is invalid!' + ), + ...$param, + )); + } else { + $this->errors[$fieldName] = sprintf( + str_replace( + ['{field}', '{Field}', '{value}'], + [$fieldName, ucfirst($fieldName), is_array($valueToTest) ? json_encode($valueToTest) : $valueToTest], + $this->messages[$currentRule] ?? '{Field} is invalid!' + ), + ...$param, + ); + } } }