{"record":{"id":"7b0c2de5dd2391e7","repo":"monicahq/monica","slug":"the-name-order-must-contain-at-least-one-variable","errorCode":null,"errorMessage":"The name order must contain at least one variable.","messagePattern":"The name order must contain at least one variable\\.","errorType":"exception","errorClass":"Exception","httpStatus":500,"severity":"error","filePath":"app/Domains/Settings/ManageUserPreferences/Services/StoreNameOrderPreference.php","lineNumber":53,"sourceCode":"    /**\n     * Store name order preference for the given user.\n     */\n    public function execute(array $data): User\n    {\n        $this->data = $data;\n\n        $this->validateRules($data);\n        $this->checkNameOrderValidity();\n        $this->updateUser();\n\n        return $this->author;\n    }\n\n    private function checkNameOrderValidity(): void\n    {\n        // there should be at least one variable in the name order\n        if (substr_count($this->data['name_order'], '%') < 1) {\n            throw new \\Exception('The name order must contain at least one variable.');\n        }\n\n        if (substr_count($this->data['name_order'], '%') % 2 == 1) {\n            throw new \\Exception('At least one % is missing to have a valid name order.');\n        }\n    }\n\n    private function updateUser(): void\n    {\n        $this->author->name_order = $this->data['name_order'];\n        $this->author->save();\n    }\n}\n","sourceCodeStart":35,"sourceCodeEnd":67,"githubUrl":"https://github.com/monicahq/monica/blob/e08e91734170b6bbd582cb578532c3948196124e/app/Domains/Settings/ManageUserPreferences/Services/StoreNameOrderPreference.php#L35-L67","documentation":"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).","triggerScenarios":"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.","commonSituations":"Users clearing the template and typing their own name, frontends allowing submit without any placeholder selected, or API tests posting free-text strings.","solutions":["Include at least one placeholder pair, e.g. '%first_name% %last_name%'","Validate client-side before submit: the string must match /%[a-z_]+%/","Build the value from the known placeholder vocabulary instead of accepting free text"],"exampleFix":"// before\n$data = ['name_order' => 'John Smith']; // no placeholder -> throws\n\n// after\n$data = ['name_order' => '%first_name% %last_name%'];","handlingStrategy":"validation","validationCode":"// Validate before calling: require at least one %-delimited placeholder\n$nameOrder = $data['name_order'];\n\nif (! preg_match('/%[a-z_]+%/', $nameOrder)) {\n    throw ValidationException::withMessages([\n        'name_order' => 'The name order must contain at least one variable like %first_name%.',\n    ]);\n}","typeGuard":"function containsNamePlaceholder(string $nameOrder): bool\n{\n    return preg_match('/%[a-z_]+%/', $nameOrder) === 1;\n}","tryCatchPattern":"try {\n    app(StoreNameOrderPreference::class)->execute($data);\n} catch (\\Exception $e) {\n    if (str_contains($e->getMessage(), 'name order')) {\n        throw ValidationException::withMessages(['name_order' => $e->getMessage()]);\n    }\n    throw $e;\n}","preventionTips":["Build the input from a fixed set of placeholder tokens in the UI rather than free text","Run the /%[a-z_]+%/ client-side check before enabling submit","Show live preview of the rendered name as the user edits the template","Add form-request validation so bad templates 422 instead of 500"],"tags":["monica","user-preferences","name-order","template"],"backgroundTag":"template-placeholder-invalid","analyzedSha":"e08e91734170b6bbd582cb578532c3948196124e","analyzedAt":"2026-08-17T01:36:49.014Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}