Ibexa DXP Discussions

Community discussion forum for developers working with Ibexa DXP

Content fields did not validate

Hi, I am trying to create new user using API services, but i am getting an error…

My syntax :
$parentGroups = (object)[‘id’ => 63];
$user = $this->container->get(‘ezpublish.api.service.user’)
->newUserCreateStruct(’’, ‘’, ‘’, ‘’);
$user->login = ‘testuser’;
$user->email = ‘test@user.com’;
$user->password = ‘User124578User’;
$user->mainLanguageCode = ‘eng-GB’;
return $this->container->get(‘ezpublish.api.service.user’)->createUser($user, array($parentGroups))->id;

ERROR:
eZ\Publish\Core\Base\Exceptions\ContentFieldValidationException: Content fields did not validate

The error comes during validation in ContentService.php, createContent(). However, If I remove this condition “if (!empty($allFieldErrors)) { throw new ContentFieldValidationException($allFieldErrors); }” (ContentService.php, Line Number: 660) user creation is done succesfully.

I don’t think this hacky way is a good solution, can anyone help me? Thank you

Hello.

The error you’re describing is a generic one, you need to dig into its structure to see what actually had happened.

The ContentFieldValidationException has getFieldErrors method which contains multidimensional array with error messages. You need to investigate that to tell us what the actual error is.

At the first glance, the $parentGroups argument is incorrect. Please see the doc of the createUser method:

the groups of type \eZ\Publish\API\Repository\Values\User\UserGroup which are assigned to the user after creation.

You need to load the well-defined group from the Repository first:

$parentGroup = $userService->loadUserGroup(63);

I also have a feeling user password might be too weak, but that’s to be determined from the validation error.

Last but not least, I see yet another, unrelated issue in your code. You’re relying on the Service Container to get your services. This behavior has been deprecated and won’t work in the future versions of eZ Platform. Use Dependency Injection instead.

So in your class constructor inject User Service:

/** @var \eZ\Publish\API\Repository\UserService */
private $userService;

public function __construct(UserService $userService)
{
    $this->userService = $userService
}

and then in your method use $this->userService instead of $this->container->get('ezpublish.api.service.user').

If you’re using eZ Platform 2.5, then auto-wiring this class should work out of the box.


Kind Regards,
Andrew Longosz