{"record":{"id":"298e217baa553b2e","repo":"passbolt/passbolt_api","slug":"could-not-validate-group-data","errorCode":null,"errorMessage":"Could not validate group data.","messagePattern":"Could not validate group data\\.","errorType":"validation","errorClass":"App\\Error\\Exception\\ValidationException","httpStatus":400,"severity":"error","filePath":"src/Model/Table/GroupsTable.php","lineNumber":253,"sourceCode":"     * @param \\App\\Utility\\UserAccessControl $control access control details\n     * @return \\App\\Model\\Entity\\Group\n     * @throws \\App\\Error\\Exception\\ValidationException\n     * @throws \\Cake\\Http\\Exception\\InternalErrorException\n     */\n    public function create(array $data, UserAccessControl $control): Group\n    {\n        // Manage defaults.\n        $defaults = [\n            'created_by' => $control->getId(),\n            'modified_by' => $control->getId(),\n            'deleted' => false,\n        ];\n        $data = array_merge($defaults, $data);\n\n        // Check validation rules.\n        $group = $this->buildEntity($data);\n        if ($group->getErrors()) {\n            throw new ValidationException(__('Could not validate group data.'), $group, $this);\n        }\n\n        $groupSaved = $this->save($group);\n\n        // Check for validation errors. (associated models too).\n        if ($group->getErrors()) {\n            throw new ValidationException(__('Could not validate group data.'), $group, $this);\n        }\n\n        // Check for errors while saving.\n        if (!$groupSaved) {\n            throw new InternalErrorException('Could not save the group, try again later.');\n        }\n\n        // Dispatch event.\n        $eventData = ['group' => $groupSaved, 'requester' => $control];\n        $event = new Event(static::GROUP_CREATE_SUCCESS_EVENT_NAME, $this, $eventData);\n        $this->getEventManager()->dispatch($event);","sourceCodeStart":235,"sourceCodeEnd":271,"githubUrl":"https://github.com/passbolt/passbolt_api/blob/31c1bbc10f32808a607fa9bd81891e898779c0bc/src/Model/Table/GroupsTable.php#L235-L271","documentation":"GroupsTable::create() merges default data, builds the group entity via buildEntity(), and throws ValidationException('Could not validate group data.') if the entity already carries errors before saving. The entity is attached so callers can read the detailed errors.","triggerScenarios":"Calling GroupsTable::create($data) where the built group entity fails validation — e.g. missing/invalid group name, invalid users data structure, or rule violations applied in buildEntity (beforeSave rules).","commonSituations":"API payloads omitting the name field, duplicate group names if uniqueness rules apply, malformed 'users' association arrays, or passing SecretsGroupsChanges data in the wrong shape.","solutions":["Catch ValidationException and inspect $e->getEntity()->getErrors() for the exact field failures.","Ensure the payload includes a valid name and correctly shaped users data.","Validate against the groups validation rules before calling create().","Use the API schema documentation for the required group creation fields."],"exampleFix":"// before\n$this->Groups->create($data, ['userId' => $uId]); // throws on bad name\n// after\n$data['name'] = trim($data['name'] ?? '');\nif ($data['name'] === '') { throw new BadRequestException('A group name is required.'); }\n$this->Groups->create($data, ['userId' => $uId]);","handlingStrategy":"validation","validationCode":"$data['name'] = trim($data['name'] ?? '');\nif ($data['name'] === '') { throw new BadRequestException('Group name is required.'); }\nif (!isset($data['users']) || !is_array($data['users'])) { throw new BadRequestException('users must be an array.'); }","typeGuard":"function isValidGroupPayload(array $data): bool {\n  return isset($data['name']) && is_string($data['name']) && trim($data['name']) !== ''\n    && (!isset($data['users']) || is_array($data['users']));\n}","tryCatchPattern":"try { $group = $this->Groups->create($data, ['userId' => $uId]); }\ncatch (ValidationException $e) {\n  $fieldErrors = $e->getEntity()->getErrors();\n  throw new BadRequestException(json_encode($fieldErrors));\n}","preventionTips":["Validate the request DTO against the group schema before calling create().","Inspect getErrors() on the exception's entity for exact field failures.","Reject empty/whitespace names at the API layer.","Cover group creation validation with unit tests."],"tags":["validation","groups","cakephp","entity"],"backgroundTag":"schema-validation-failed","analyzedSha":"31c1bbc10f32808a607fa9bd81891e898779c0bc","analyzedAt":"2026-09-17T00:04:38.960Z","contentChangedAt":"2026-09-17T00:04:38.960Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}