Skip to content

Commit

Permalink
fixes #1963
Browse files Browse the repository at this point in the history
  • Loading branch information
Elorfin committed Feb 9, 2022
1 parent f5600ff commit 34c825e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/main/core/API/Validator/UserValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Claroline\CoreBundle\Entity\Role;
use Claroline\CoreBundle\Entity\User;
use Claroline\CoreBundle\Library\Configuration\PlatformConfigurationHandler;
use Claroline\CoreBundle\Manager\FacetManager;
use Claroline\CoreBundle\Manager\Organization\OrganizationManager;
use Claroline\CoreBundle\Manager\UserManager;
use Claroline\CoreBundle\Manager\Workspace\WorkspaceManager;
Expand All @@ -36,6 +37,8 @@ class UserValidator implements ValidatorInterface
private $workspaceManager;
/** @var OrganizationManager */
private $organizationManager;
/** @var FacetManager */
private $facetManager;
/** @var ProfileSerializer */
private $profileSerializer;

Expand All @@ -49,6 +52,7 @@ public function __construct(
UserManager $manager,
WorkspaceManager $workspaceManager,
OrganizationManager $organizationManager,
FacetManager $facetManager,
ProfileSerializer $profileSerializer
) {
$this->authorization = $authorization;
Expand All @@ -58,6 +62,7 @@ public function __construct(
$this->manager = $manager;
$this->workspaceManager = $workspaceManager;
$this->organizationManager = $organizationManager;
$this->facetManager = $facetManager;
$this->profileSerializer = $profileSerializer;

$this->roleRepo = $om->getRepository(Role::class);
Expand Down Expand Up @@ -135,11 +140,13 @@ public function validate($data, $mode, array $options = [])
// todo validate Facet values
if (in_array(Options::VALIDATE_FACET, $options)) {
$facets = $this->profileSerializer->serialize([Options::REGISTRATION]);
$allFields = [];
$required = [];

foreach ($facets as $facet) {
foreach ($facet['sections'] as $section) {
foreach ($section['fields'] as $field) {
$allFields[] = $field;
if ($field['required']) {
$required[] = $field;
}
Expand All @@ -148,7 +155,7 @@ public function validate($data, $mode, array $options = [])
}

foreach ($required as $field) {
if (!ArrayUtils::has($data, 'profile.'.$field['id'])) {
if ($this->facetManager->isFieldDisplayed($field, $allFields, $data) && !ArrayUtils::has($data, 'profile.'.$field['id'])) {
$errors[] = [
'path' => 'profile/'.$field['id'],
'message' => 'The field '.$field['label'].' is required',
Expand Down
41 changes: 41 additions & 0 deletions src/main/core/Manager/FacetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Claroline\CoreBundle\Manager;

use Claroline\AppBundle\API\Utils\ArrayUtils;
use Claroline\CoreBundle\Event\CatalogEvents\FacetEvents;
use Claroline\CoreBundle\Event\Facet\GetFacetValueEvent;
use Claroline\CoreBundle\Event\Facet\SetFacetValueEvent;
Expand Down Expand Up @@ -42,4 +43,44 @@ public function deserializeFieldValue($object, $type, $value = null)

return $event->getFormattedValue();
}

public function isFieldDisplayed(array $fieldDef, array $allFields, array $data): bool
{
$condition = $fieldDef['display']['condition'];

if (!empty($condition)) {
$parentField = null;

foreach ($allFields as $searchedField) {
if ($searchedField['id'] === $condition['field']) {
$parentField = $searchedField;
}
}

if ($parentField) {
$parentValue = ArrayUtils::get($data, 'profile.'.$parentField['id']);

$displayed = false;

switch ($condition['comparator']) {
case 'equal':
$displayed = $parentValue === $condition['value'];
break;
case 'different':
$displayed = $parentValue !== $condition['value'];
break;
case 'empty':
$displayed = empty($parentValue);
break;
case 'not_empty':
$displayed = !empty($parentValue);
break;
}

return $displayed;
}
}

return true;
}
}
1 change: 1 addition & 0 deletions src/main/core/Resources/config/services/validator.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ services:
- '@Claroline\CoreBundle\Manager\UserManager'
- '@Claroline\CoreBundle\Manager\Workspace\WorkspaceManager'
- '@Claroline\CoreBundle\Manager\Organization\OrganizationManager'
- '@Claroline\CoreBundle\Manager\FacetManager'
- '@Claroline\CoreBundle\API\Serializer\User\ProfileSerializer'

Claroline\CoreBundle\API\Validator\GroupValidator:
Expand Down

0 comments on commit 34c825e

Please sign in to comment.