Commit 06e0e6f3 authored by Laurent Lecluse's avatar Laurent Lecluse
Browse files

Meilleure gestion des champs désactivés & protection contre la saisie malicieuse

parent c74a4071
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -65,6 +65,20 @@ abstract class AbstractForm extends Form implements InputFilterProviderInterfac
                $request->getFiles()->toArray()
            );

            // Protection : Pour les éléments en lecture seule, on utilise les données extraites de l'entité et non celles en entrée
            $roData = [];
            foreach( $this->getElements() as $element){
                if (true === $element->getAttribute('readonly') || true === $element->getAttribute('disabled')){
                    if (empty($roData)){
                        $roData = $this->extract();
                    }
                    $elementName = $element->getName();
                    if (isset($roData[$elementName])) {
                        $data[$elementName] = $roData[$elementName];
                    }
                }
            }

            $this->setData($data);
            if ($this->isValid()) {
                try {
+12 −14
Original line number Diff line number Diff line
@@ -4,17 +4,11 @@ namespace Application\Traits;

use Application\Constants;
use Application\Filter\FloatFromString;
use Application\Form\AbstractForm;
use Application\Hydrator\GenericHydrator;
use Application\Interfaces\ParametreEntityInterface;
use Doctrine\ORM\EntityManager;
use Laminas\Form\Element\Checkbox;
use Laminas\Form\Element\Csrf;
use Laminas\Form\Element\Number;
use Laminas\Form\Element\Select;
use Laminas\Form\Element\Submit;
use Laminas\Form\Element\Text;
use Laminas\Form\Form;
use Laminas\Mvc\Plugin\FlashMessenger\FlashMessenger;
use Laminas\Stdlib\ArrayUtils;
use UnicaenApp\Entity\HistoriqueAwareInterface;
@@ -59,11 +53,11 @@ trait FormFieldsetTrait
     *
     * @param string            $name               Name of the route
     * @param array             $params             Parameters for the link
     * @param array|Traversable $options            Options for the route
     * @param array|\Traversable $options            Options for the route
     * @param bool              $reuseMatchedParams Whether to reuse matched parameters
     *
     * @return string                         For the link href attribute
     * @see    \Laminas\Mvc\Router\RouteInterface::assemble()
     * @see    \Laminas\Mvc\I18n\Router\RouteInterface::assemble()
     *
     */
    protected function getUrl($name = null, $params = [], $options = [], $reuseMatchedParams = false): string
@@ -86,19 +80,23 @@ trait FormFieldsetTrait



    public function readOnly(bool $readOnly = true)
    public function readOnly(bool $readOnly = true, array $elements = [])
    {
        if (empty($elements)){
            $elements = $this->getElements();
        }

        /** @var $element \Laminas\Form\Element */
        foreach ($this->getElements() as $element) {
        foreach ($elements as $elementName) {
            if ($element = $this->get($elementName)) {
                switch (get_class($element)) {
                case Number::class:
                case Text::class:
                    $element->setAttribute('readonly', $readOnly);
                break;
                    case Select::class:
                    case Checkbox::class:
                        $element->setAttribute('disabled', $readOnly);
                        break;
                    default:
                        $element->setAttribute('readonly', $readOnly);
                }
            }
        }
    }