monicahq/monica · error · Exception

The name order must contain at least one variable.

Error message

The name order must contain at least one variable.

What it means

The name_order preference is a template mixing literal text with %-delimited placeholders (%first_name%, %last_name%, %middle_name%, %nickname%, %maiden_name% — see NameHelper). checkNameOrderValidity() first requires at least one '%' character; a template containing no placeholder at all is rejected with this \Exception (rendered as a 500).

Source

Thrown at app/Domains/Settings/ManageUserPreferences/Services/StoreNameOrderPreference.php:53

    /**
     * Store name order preference for the given user.
     */
    public function execute(array $data): User
    {
        $this->data = $data;

        $this->validateRules($data);
        $this->checkNameOrderValidity();
        $this->updateUser();

        return $this->author;
    }

    private function checkNameOrderValidity(): void
    {
        // there should be at least one variable in the name order
        if (substr_count($this->data['name_order'], '%') < 1) {
            throw new \Exception('The name order must contain at least one variable.');
        }

        if (substr_count($this->data['name_order'], '%') % 2 == 1) {
            throw new \Exception('At least one % is missing to have a valid name order.');
        }
    }

    private function updateUser(): void
    {
        $this->author->name_order = $this->data['name_order'];
        $this->author->save();
    }
}

View on GitHub (pinned to e08e917341)

Solutions

  1. Include at least one placeholder pair, e.g. '%first_name% %last_name%'
  2. Validate client-side before submit: the string must match /%[a-z_]+%/
  3. Build the value from the known placeholder vocabulary instead of accepting free text

Example fix

// before
$data = ['name_order' => 'John Smith']; // no placeholder -> throws

// after
$data = ['name_order' => '%first_name% %last_name%'];
Defensive patterns

Strategy: validation

Validate before calling

// Validate before calling: require at least one %-delimited placeholder
$nameOrder = $data['name_order'];

if (! preg_match('/%[a-z_]+%/', $nameOrder)) {
    throw ValidationException::withMessages([
        'name_order' => 'The name order must contain at least one variable like %first_name%.',
    ]);
}

Type guard

function containsNamePlaceholder(string $nameOrder): bool
{
    return preg_match('/%[a-z_]+%/', $nameOrder) === 1;
}

Try / catch

try {
    app(StoreNameOrderPreference::class)->execute($data);
} catch (\Exception $e) {
    if (str_contains($e->getMessage(), 'name order')) {
        throw ValidationException::withMessages(['name_order' => $e->getMessage()]);
    }
    throw $e;
}

Prevention

When it happens

Trigger: Saving the name order preference with a plain literal like 'John Smith' or a template from which every placeholder was removed — zero '%' characters in the submitted name_order string.

Common situations: Users clearing the template and typing their own name, frontends allowing submit without any placeholder selected, or API tests posting free-text strings.

Related errors


AI-assisted analysis of monicahq/monica@e08e917341 (2026-08-17). Data as JSON: /api/errors/7b0c2de5dd2391e7. Report an issue: GitHub.