Skip to content

Commit

Permalink
feat: use compact errors by default
Browse files Browse the repository at this point in the history
  • Loading branch information
mychidarko committed Nov 9, 2024
1 parent c8e10ba commit 1e35754
Showing 1 changed file with 61 additions and 21 deletions.
82 changes: 61 additions & 21 deletions src/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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 = [];

Expand All @@ -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]);
Expand All @@ -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;
}
Expand All @@ -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;
Expand All @@ -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,
);
}
}
}

Expand Down

0 comments on commit 1e35754

Please sign in to comment.