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

  1. Check has($name) (or offsetExists) before adding and skip or rename the rule
  2. Call remove($name) first if replacement is intended
  3. Ensure loops/conditional logic don't add the same rule twice
  4. 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

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


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 unset

View on GitHub (pinned to 1128eba9b0)