Commit 81b384b0 authored by David Surville's avatar David Surville
Browse files

[Fix] Remplacement du validator UniqueObject qui pose problème sur postgreSQL

parent f6b063c8
Loading
Loading
Loading
Loading
+40 −20
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ namespace UnicaenUtilisateur\Form\Role;

use Application\Entity\Db\UtilisateurRole;
use DoctrineModule\Form\Element\ObjectSelect;
use DoctrineModule\Validator\NoObjectExists;
use DoctrineModule\Validator\UniqueObject;
use UnicaenApp\Service\EntityManagerAwareTrait;
use UnicaenUtilisateur\Entity\Db\UserInterface;
@@ -17,6 +18,7 @@ use Zend\Form\Form;
use Zend\InputFilter\Factory;
use Zend\Validator\Callback;
use Zend\Validator\NotEmpty;
use Zend\Validator\Regex;

class RoleForm extends Form
{
@@ -27,12 +29,6 @@ class RoleForm extends Form
    {
        $this->setAttribute('id', 'form-role');

        /** nécessaire pour le validator @see \DoctrineModule\Validator\UniqueObject */
        $this->add([
            'name' => 'id',
            'type' => Hidden::class,
        ]);

        $this->add([
            'type' => Text::class,
            'name' => 'libelle',
@@ -157,17 +153,24 @@ class RoleForm extends Form
                        ],
                    ],
                    [
                        'name' => UniqueObject::class,
                        'name' => Callback::class,
                        'options' => [
                            'object_manager' => $this->getEntityManager(),
                            'object_repository' => $this->roleService->getRepo(),
                            'use_context' => true,
                            'fields'   => ['libelle'],
                            'messages' => [
                                UniqueObject::ERROR_OBJECT_NOT_UNIQUE => "Ce libellé est déjà utilisé dans l'application.",
                                Callback::INVALID_VALUE => "Ce libellé est déjà utilisé pour un autre rôle.",
                            ],
                            'callback' => function ($value, $context = []) {
                                $role = $this->roleService->findByLibelle($value);
                                if(!$role) {
                                    return true;
                                }
                                elseif($this->getObject()->getId() != null) { // modification
                                    return strtoupper($this->getObject()->getLibelle()) === strtoupper($value);
                                }
                                return false;
                            },
                            'break_chain_on_failure' => true,
                        ],
                    ],
                    ]
                ],
            ],

@@ -184,17 +187,34 @@ class RoleForm extends Form
                        ],
                    ],
                    [
                        'name' => UniqueObject::class,
                        'name' => Regex::class,
                        'options' => [
                            'pattern' => '/^[a-z0-9_-]+$/',
                            'messages' => [
                                Regex::NOT_MATCH => "Seuls les caractères suivants sont autorisés : [a-z, 0-9, _, -].",
                            ],
                            'break_chain_on_failure' => true,
                        ],
                    ],
                    [
                        'name' => Callback::class,
                        'options' => [
                            'object_manager' => $this->getEntityManager(),
                            'object_repository' => $this->roleService->getRepo(),
                            'use_context' => true,
                            'fields'   => ['roleId'],
                            'messages' => [
                                UniqueObject::ERROR_OBJECT_NOT_UNIQUE => "Cet identifiant est déjà utilisé dans l'application.",
                                Callback::INVALID_VALUE => "Cet identifiant est déjà utilisé pour un autre rôle.",
                            ],
                            'callback' => function ($value, $context = []) {
                                $role = $this->roleService->findByRoleId($value);
                                if(!$role) {
                                    return true;
                                }
                                elseif($this->getObject()->getId() != null) { // modification
                                    return strtoupper($this->getObject()->getRoleId()) === strtoupper($value);
                                }
                                return false;
                            },
                            'break_chain_on_failure' => true,
                        ],
                    ],
                    ]
                ],
            ],