Commit 51908204 authored by David Surville's avatar David Surville
Browse files

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

parent 574a6927
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -43,6 +43,7 @@ class PrivilegeController extends AbstractActionController
        $privilege->setCategorie($categorie);
        $form = $this->privilegeForm;
        $form->setAttribute('action', $this->url()->fromRoute('unicaen-privilege/ajouter', ['categorie' => $categorie->getId()], [], true));
        $form->get('categorieId')->setValue($categorie->getId());
        $form->bind($privilege);

        if ($data = $this->params()->fromPost()) {
@@ -72,6 +73,7 @@ class PrivilegeController extends AbstractActionController
        $privilege = $this->privilegeService->getRequestedPrivilege($this);
        $form = $this->privilegeForm;
        $form->setAttribute('action', $this->url()->fromRoute('unicaen-privilege/modifier', ['privilege' => $privilege->getId()], [], true));
        $form->get('categorieId')->setValue($privilege->getCategorie()->getId());
        $form->bind($privilege);

        if ($data = $this->params()->fromPost()) {
+39 −20
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@ use Zend\Form\Form;
use Zend\InputFilter\Factory;
use Zend\Validator\Callback;
use Zend\Validator\NotEmpty;
use Zend\Validator\Regex;

class CategorieForm extends Form
{
@@ -23,12 +24,6 @@ class CategorieForm extends Form
    {
        $this->setAttribute('id', 'form-categorie');

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

        $this->add([
            'type' => Text::class,
            'name' => 'libelle',
@@ -108,17 +103,24 @@ class CategorieForm extends Form
                        ],
                    ],
                    [
                        'name' => UniqueObject::class,
                        'name' => Callback::class,
                        'options' => [
                            'object_manager' => $this->getEntityManager(),
                            'object_repository' => $this->categorieService->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 une autre catégorie.",
                            ],
                            'callback' => function ($value, $context = []) {
                                $categorie = $this->categorieService->findByLibelle($value);
                                if(!$categorie) {
                                    return true;
                                }
                                elseif($this->getObject()->getId() != null) { // modification
                                    return strtoupper($this->getObject()->getLibelle()) === strtoupper($value);
                                }
                                return false;
                            },
                            'break_chain_on_failure' => true,
                        ],
                    ],
                    ]
                ],
            ],

@@ -135,17 +137,34 @@ class CategorieForm 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->categorieService->getRepo(),
                            'use_context' => true,
                            'fields'   => ['code'],
                            'messages' => [
                                UniqueObject::ERROR_OBJECT_NOT_UNIQUE => "Ce code est déjà utilisé dans l'application.",
                                Callback::INVALID_VALUE => "Ce code est déjà utilisé pour une autre catégorie.",
                            ],
                            'callback' => function ($value, $context = []) {
                                $categorie = $this->categorieService->findByCode($value);
                                if(!$categorie) {
                                    return true;
                                }
                                elseif($this->getObject()->getId() != null) { // modification
                                    return strtoupper($this->getObject()->getCode()) === strtoupper($value);
                                }
                                return false;
                            },
                            'break_chain_on_failure' => true,
                        ],
                    ],
                    ]
                ],
            ],

+43 −16
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ use Zend\Form\Form;
use Zend\InputFilter\Factory;
use Zend\Validator\Callback;
use Zend\Validator\NotEmpty;
use Zend\Validator\Regex;

class PrivilegeForm extends Form
{
@@ -29,10 +30,9 @@ class PrivilegeForm extends Form
    {
        $this->setAttribute('id', 'form-privilege');

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

        $this->add([
@@ -54,6 +54,7 @@ class PrivilegeForm extends Form
            ],
            'attributes' => [
                'id' => 'categorie',
                'readonly' => true
            ],
        ]);

@@ -126,17 +127,25 @@ class PrivilegeForm extends Form
                        ],
                    ],
                    [
                        'name' => UniqueObject::class,
                        'name' => Callback::class,
                        'options' => [
                            'object_manager' => $this->getEntityManager(),
                            'object_repository' => $this->privilegeService->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 privilège de cette catégorie.",
                            ],
                            'callback' => function ($value, $context = []) {
                                $categorie = $this->categorieService->find($context['categorieId']);
                                $privilege = $this->privilegeService->findByLibelle($value, $categorie);
                                if(!$privilege) {
                                    return true;
                                }
                                elseif($this->getObject()->getId() != null) { // modification
                                    return strtoupper($this->getObject()->getLibelle()) === strtoupper($value);
                                }
                                return false;
                            },
                            'break_chain_on_failure' => true,
                        ],
                    ],
                    ]
                ],
            ],

@@ -153,17 +162,35 @@ class PrivilegeForm extends Form
                        ],
                    ],
                    [
                        'name' => UniqueObject::class,
                        'name' => Regex::class,
                        'options' => [
                            'object_manager' => $this->getEntityManager(),
                            'object_repository' => $this->privilegeService->getRepo(),
                            'use_context' => true,
                            'fields'   => ['code'],
                            'pattern' => '/^[a-z0-9_-]+$/',
                            'messages' => [
                                UniqueObject::ERROR_OBJECT_NOT_UNIQUE => "Ce code est déjà utilisé dans l'application.",
                                Regex::NOT_MATCH => "Seuls les caractères suivants sont autorisés : [a-z, 0-9, _, -].",
                            ],
                            'break_chain_on_failure' => true,
                        ],
                    ],
                    [
                        'name' => Callback::class,
                        'options' => [
                            'messages' => [
                                Callback::INVALID_VALUE => "Ce code est déjà utilisé pour un autre privilège de cette catégorie.",
                            ],
                            'callback' => function ($value, $context = []) {
                                $categorie = $this->categorieService->find($context['categorieId']);
                                $privilege = $this->privilegeService->findByCode($value, $categorie);
                                if(!$privilege) {
                                    return true;
                                }
                                elseif($this->getObject()->getId() != null) { // modification
                                    return strtoupper($this->getObject()->getCode()) === strtoupper($value);
                                }
                                return false;
                            },
                            'break_chain_on_failure' => true,
                        ],
                    ],
                    ]
                ],
            ],

+1 −1
Original line number Diff line number Diff line
@@ -113,7 +113,7 @@ $canProvider = $this->isAllowed(PrivilegePrivileges::getResourceId(PrivilegeP
                </a>
            </td>
            <td>
                <code><?php echo $categorie->getNamespace(); ?></code>
                <?php echo $categorie->getNamespace() ? sprintf('<code>%s</code>', $categorie->getNamespace()) : ''; ?>
            </td>
            <td><?php echo $categorie->getOrdre(); ?></td>
            <td>