cakephp/cakephp · error · CakeException
A validation rule with the name
Error message
A validation rule with the name `{$name}` already exists What it means
ValidationSet manages named validation rules for a field. ValidationSet::add() refuses to overwrite an existing rule name and throws CakeException when the name already exists in its rules. Rules must be added once with unique names per field.
Solutions
- Check has($name) (or offsetExists) before adding and skip or rename the rule
- Call remove($name) first if replacement is intended
- Ensure loops/conditional logic don't add the same rule twice
- Use unique rule names per field (e.g. prefix custom rules)
Example fix
// before
$set->add('notBlank', ['rule' => 'notBlank']);
$set->add('notBlank', ['rule' => 'notBlank']); // throws
// after
if (!$set->has('notBlank')) {
$set->add('notBlank', ['rule' => 'notBlank']);
} Defensive patterns
Strategy: try-catch
Validate before calling
if ($set->has($name)) {
$set->remove($name); // or skip
}
$set->add($name, $rule); Type guard
function canAddRule(Cake\Validation\ValidationSet $set, string $name): bool
{
return !$set->has($name);
} Try / catch
try {
$set->add($name, $rule);
} catch (Cake\Core\Exception\CakeException $e) {
if (str_contains($e->getMessage(), 'already exists')) {
$set->remove($name);
$set->add($name, $rule); // replace intentionally
} else {
throw $e;
}
} Prevention
- Guard with has()/offsetExists before adding rules
- Make rule names unique per field, especially in plugin + app combinations
- Avoid re-running rule-registration code (loops, repeated bootstrap)
- Call remove() explicitly when replacement is intended
When it happens
Trigger: Calling add('notBlank', ...) twice on the same ValidationSet, or offsetSet[] assignment reusing an existing rule name for the same field — often when building validators programmatically or in loops.
Common situations: Conditionally adding rules in loops that execute twice; plugin/provider code and app code both adding the same rule name to a field; validators reconfigured at runtime without removing existing rules first.
Understand the failure class
Background: "already exists" / EEXIST / FileAlreadyExistsException: what the 'file already exists' error means and how to fix it — this error's family across 37 libraries.
Related errors
- A rule with the name
- Argument 2 is expected to have a `repository` key that…
- Argument 2 is expected to match one of the…
- Argument ` ` must be an alphanumeric string
- At least one case needed for `enumExcept()` validation.
AI-assisted analysis of cakephp/cakephp@1128eba9b0 (2026-09-12).
Data as JSON: /api/errors/3a06ed0bbae8f1c7.
Report an issue: GitHub.
Appendix: source
Thrown at src/Validation/ValidationSet.php:161
*
* ```
* $set
* ->add('notBlank', ['rule' => 'notBlank'])
* ->add('inRange', ['rule' => ['between', 4, 10])
* ```
*
* @param string $name The name under which the rule should be set
* @param \Cake\Validation\ValidationRule|array $rule The validation rule to be set
* @return $this
* @throws \Cake\Core\Exception\CakeException If a rule with the same name already exists
*/
public function add(string $name, ValidationRule|array $rule)
{
if (!($rule instanceof ValidationRule)) {
$rule = new ValidationRule($rule);
}
if (array_key_exists($name, $this->_rules)) {
throw new CakeException("A validation rule with the name `{$name}` already exists");
}
$this->_rules[$name] = $rule;
return $this;
}
/**
* Removes a validation rule from the set
*
* ### Example:
*
* ```
* $set
* ->remove('notBlank')
* ->remove('inRange')
* ```
*
* @param string $name The name under which the rule should be unsetView on GitHub (pinned to 1128eba9b0)