public function onBindNormData(FilterDataEvent $event)
 {
     $group = $event->getForm()->getData();
     $data = $event->getData();
     $group->replace($data);
     $event->setData($group);
 }
 public function onBindNormData(FilterDataEvent $event)
 {
     $collection = $event->getForm()->getData();
     $data = $event->getData();
     $rootEntity = $event->getForm()->getParent()->getData();
     if (!$collection) {
         $collection = $data;
         foreach ($data as $key => $value) {
             $value->setEntity($rootEntity);
         }
     } elseif (count($data) === 0) {
         $collection->clear();
     } else {
         // merge $data into $collection
         foreach ($collection as $key => $entity) {
             if (!$data->containsKey($key)) {
                 $collection->removeElement($entity);
             } else {
                 $collection->get($key)->setCategory($data->get($key)->getCategory());
                 $data->remove($key);
             }
         }
         foreach ($data as $key => $entity) {
             $collection->set($key, $entity);
             $entity->setEntity($rootEntity);
         }
     }
     $event->setData($collection);
 }
Esempio n. 3
0
 public function onBindClientData(FilterDataEvent $event)
 {
     $data = $event->getData();
     if (is_string($data)) {
         $event->setData(trim($data));
     }
 }
Esempio n. 4
0
 public function onSetData(FilterDataEvent $event)
 {
     $data = $event->getData();
     if (isset($this->mapping['onSetData'][$data])) {
         $event->setData($this->mapping['onSetData'][$data]);
     }
 }
 public function onBindNormData(FilterDataEvent $event)
 {
     $data = $event->getData();
     if ($this->defaultProtocol && $data && !preg_match('~^\\w+://~', $data)) {
         $event->setData($this->defaultProtocol . '://' . $data);
     }
 }
 /**
  * @param \Symfony\Component\Form\Event\FilterDataEvent $event
  */
 public function onBind(FilterDataEvent $event)
 {
     $collection = $event->getForm()->getData();
     $data = $event->getData();
     if (!$collection) {
         $collection = $data;
     } else {
         if (count($data) === 0) {
             $this->modelManager->collectionClear($collection);
         } else {
             // merge $data into $collection
             foreach ($collection as $entity) {
                 if (!$this->modelManager->collectionHasElement($data, $entity)) {
                     $this->modelManager->collectionRemoveElement($collection, $entity);
                 } else {
                     $this->modelManager->collectionRemoveElement($data, $entity);
                 }
             }
             foreach ($data as $entity) {
                 $this->modelManager->collectionAddElement($collection, $entity);
             }
         }
     }
     $event->setData($collection);
 }
 public function onBindNormData(FilterDataEvent $event)
 {
     $collection = $event->getForm()->getData();
     $data = $event->getData();
     if (!$collection) {
         $collection = $data;
     } else {
         if (count($data) === 0) {
             $collection->clear();
         } else {
             // merge $data into $collection
             foreach ($collection as $model) {
                 if ($data->search($model) === false) {
                     $collection->remove($model);
                 } else {
                     $data->remove($model);
                 }
             }
             foreach ($data as $model) {
                 $collection->append($model);
             }
         }
     }
     $event->setData($collection);
 }
 public function onBindNormData(FilterDataEvent $event)
 {
     $collection = $event->getForm()->getData();
     $data = $event->getData();
     if (!$collection) {
         $collection = $data;
     } else {
         if (count($data) === 0) {
             $collection->clear();
         } else {
             // merge $data into $collection
             foreach ($collection as $entity) {
                 if (!$data->contains($entity)) {
                     $collection->removeElement($entity);
                 } else {
                     $data->removeElement($entity);
                 }
             }
             foreach ($data as $entity) {
                 $collection->add($entity);
             }
         }
     }
     $event->setData($collection);
 }
Esempio n. 9
0
 public function onBindClientData(FilterDataEvent $event)
 {
     $form = $event->getForm();
     $data = $event->getData();
     if (null === $data) {
         $data = array();
     }
     if (!is_array($data)) {
         throw new UnexpectedTypeException($data, 'array');
     }
     $data = array_replace(array('file' => '', 'token' => '', 'name' => '', 'originalName' => ''), $data);
     // Newly uploaded file
     if ($data['file'] instanceof UploadedFile && $data['file']->isValid()) {
         $data['token'] = (string) rand(100000, 999999);
         $directory = $this->storage->getTempDir($data['token']);
         $data['file']->move($directory);
         $data['name'] = $data['file']->getName();
         $data['originalName'] = $data['file']->getOriginalName();
     }
     // Existing uploaded file
     if (!$data['file'] && $data['token'] && $data['name']) {
         $path = $this->storage->getTempDir($data['token']) . DIRECTORY_SEPARATOR . $data['name'];
         if (file_exists($path)) {
             $data['file'] = new UploadedFile($path, $data['originalName'], null, null, null, true);
         }
     }
     // Clear other fields if we still don't have a file, but keep
     // possible existing files of the field
     if (!$data['file']) {
         $data = $form->getNormData();
     }
     $event->setData($data);
 }
 public function onBindNormData(FilterDataEvent $event)
 {
     $dataToMergeInto = $event->getForm()->getNormData();
     $form = $event->getForm();
     $data = $event->getData();
     if (null === $data) {
         $data = array();
     }
     if (!is_array($data) && !($data instanceof \Traversable && $data instanceof \ArrayAccess)) {
         throw new UnexpectedTypeException($data, 'array or (\\Traversable and \\ArrayAccess)');
     }
     if (null !== $dataToMergeInto && !is_array($dataToMergeInto) && !($dataToMergeInto instanceof \Traversable && $dataToMergeInto instanceof \ArrayAccess)) {
         throw new UnexpectedTypeException($dataToMergeInto, 'array or (\\Traversable and \\ArrayAccess)');
     }
     // If we are not allowed to change anything, return immediately
     if (!$this->allowAdd && !$this->allowDelete || $data === $dataToMergeInto) {
         $event->setData($dataToMergeInto);
         return;
     }
     if (!$dataToMergeInto) {
         // No original data was set. Set it if allowed
         if ($this->allowAdd) {
             $dataToMergeInto = $data;
         }
     } else {
         // Calculate delta
         $itemsToAdd = is_object($data) ? clone $data : $data;
         $itemsToDelete = array();
         foreach ($dataToMergeInto as $beforeKey => $beforeItem) {
             foreach ($data as $afterKey => $afterItem) {
                 if ($afterItem === $beforeItem) {
                     // Item found, next original item
                     unset($itemsToAdd[$afterKey]);
                     continue 2;
                 }
             }
             // Item not found, remember for deletion
             $itemsToDelete[] = $beforeKey;
         }
         // Remove deleted items before adding to free keys that are to be
         // replaced
         if ($this->allowDelete) {
             foreach ($itemsToDelete as $key) {
                 unset($dataToMergeInto[$key]);
             }
         }
         // Add remaining items
         if ($this->allowAdd) {
             foreach ($itemsToAdd as $key => $item) {
                 if (!isset($dataToMergeInto[$key])) {
                     $dataToMergeInto[$key] = $item;
                 } else {
                     $dataToMergeInto[] = $item;
                 }
             }
         }
     }
     $event->setData($dataToMergeInto);
 }
Esempio n. 11
0
 public function preSetData(FilterDataEvent $event)
 {
     $data = $event->getData();
     if (null === $data) {
         return;
     }
     $data = $this->filterData($data);
     $event->setData($data);
 }
 public function onBindNormData(FilterDataEvent $event)
 {
     $collection = $event->getForm()->getData();
     $data = $event->getData();
     // If all items were removed, call clear which has a higher
     // performance on persistent collections
     if ($collection && count($data) === 0) {
         $collection->clear();
     }
 }
Esempio n. 13
0
 public function onBindClientData(FilterDataEvent $event)
 {
     $form = $event->getForm();
     $data = $event->getData();
     if ($form->isRoot() && $form->hasChildren() && isset($data[$this->fieldName])) {
         if (!$this->csrfProvider->isCsrfTokenValid($this->intention, $data[$this->fieldName])) {
             $form->addError(new FormError('The CSRF token is invalid. Please try to resubmit the form'));
         }
         unset($data[$this->fieldName]);
     }
     $event->setData($data);
 }
Esempio n. 14
0
 /**
  * Listens to the Post Bind event and triggers filtering if adequate.
  *
  * @param FilterDataEvent $event
  */
 public function onPostBind($event)
 {
     $form = $event->getForm();
     if (!$form->isRoot()) {
         return;
     }
     $clientData = $event->getForm()->getData();
     if (!is_object($clientData)) {
         return;
     }
     $this->filterService->filterEntity($clientData);
 }
Esempio n. 15
0
    public function onBindClientData(FilterDataEvent $event)
    {
        $form = $event->getForm();

        // TODO should be disableable

        // TESTME
        $data = array_merge(array(
            'file' => '',
            'token' => '',
            'name' => '',
        ), $event->getData());

        // Newly uploaded file
        if ($data['file'] instanceof UploadedFile && $data['file']->isValid()) {
            $data['token'] = (string)rand(100000, 999999);
            $directory = $this->storage->getTempDir($data['token']);
            $data['file']->move($directory);
            $data['name'] = $data['file']->getName();
        }

        // Existing uploaded file
        if (!$data['file'] && $data['token'] && $data['name']) {
            $path = $this->storage->getTempDir($data['token']) . DIRECTORY_SEPARATOR . $data ['name'];

            if (file_exists($path)) {
                $data['file'] = new File($path);
            }
        }

        // Clear other fields if we still don't have a file, but keep
        // possible existing files of the field
        if (!$data['file']) {
            $data = $form->getNormData();
        }

        $event->setData($data);
    }
Esempio n. 16
0
 public function onBindNormData(FilterDataEvent $event)
 {
     $form = $event->getForm();
     $data = $event->getData();
     if (null === $data) {
         $data = array();
     }
     if (!is_array($data) && !($data instanceof \Traversable && $data instanceof \ArrayAccess)) {
         throw new UnexpectedTypeException($data, 'array or (\\Traversable and \\ArrayAccess)');
     }
     if ($this->allowDelete) {
         foreach ($data as $name => $child) {
             if (!$form->has($name)) {
                 unset($data[$name]);
             }
         }
     }
     $event->setData($data);
 }
Esempio n. 17
0
 public function onBindClientData(FilterDataEvent $event)
 {
     $value = $event->getData();
     $index = current($this->choiceList->getIndicesForValues(array($value)));
     $event->setData(false !== $index ? array($index => $value) : array());
 }
Esempio n. 18
0
    public function onBindClientData(FilterDataEvent $event)
    {
        $data = $event->getData();

        $event->setData(count((array)$data) === 0 ? array() : array($data => true));
    }
Esempio n. 19
0
 public function onBindClientData(FilterDataEvent $event)
 {
     $data = $event->getData();
     $event->setData(empty($data) ? array() : array($data => true));
 }
Esempio n. 20
0
 public function onBindNormData(FilterDataEvent $event)
 {
     $originalData = $event->getForm()->getNormData();
     // If we are not allowed to change anything, return immediately
     if (!$this->allowAdd && !$this->allowDelete) {
         // Don't set to the snapshot as then we are switching from the
         // original object to its copy, which might break things
         $event->setData($originalData);
         return;
     }
     $form = $event->getForm();
     $data = $event->getData();
     $childPropertyPath = null;
     $parentData = null;
     $addMethod = null;
     $removeMethod = null;
     $propertyPath = null;
     $plural = null;
     if ($form->hasParent() && $form->getAttribute('property_path')) {
         $propertyPath = new PropertyPath($form->getAttribute('property_path'));
         $childPropertyPath = $propertyPath;
         $parentData = $form->getParent()->getClientData();
         $lastElement = $propertyPath->getElement($propertyPath->getLength() - 1);
         // If the property path contains more than one element, the parent
         // data is the object at the parent property path
         if ($propertyPath->getLength() > 1) {
             $parentData = $propertyPath->getParent()->getValue($parentData);
             // Property path relative to $parentData
             $childPropertyPath = new PropertyPath($lastElement);
         }
         // The plural form is the last element of the property path
         $plural = ucfirst($lastElement);
     }
     if (null === $data) {
         $data = array();
     }
     if (!is_array($data) && !($data instanceof \Traversable && $data instanceof \ArrayAccess)) {
         throw new UnexpectedTypeException($data, 'array or (\\Traversable and \\ArrayAccess)');
     }
     if (null !== $originalData && !is_array($originalData) && !($originalData instanceof \Traversable && $originalData instanceof \ArrayAccess)) {
         throw new UnexpectedTypeException($originalData, 'array or (\\Traversable and \\ArrayAccess)');
     }
     // Check if the parent has matching methods to add/remove items
     if ($this->mergeStrategy & self::MERGE_INTO_PARENT && is_object($parentData)) {
         $reflClass = new \ReflectionClass($parentData);
         $addMethodNeeded = $this->allowAdd && !$this->addMethod;
         $removeMethodNeeded = $this->allowDelete && !$this->removeMethod;
         // Any of the two methods is required, but not yet known
         if ($addMethodNeeded || $removeMethodNeeded) {
             $singulars = (array) FormUtil::singularify($plural);
             foreach ($singulars as $singular) {
                 // Try to find adder, but don't override preconfigured one
                 if ($addMethodNeeded) {
                     $addMethod = 'add' . $singular;
                     // False alert
                     if (!$this->isAccessible($reflClass, $addMethod, 1)) {
                         $addMethod = null;
                     }
                 }
                 // Try to find remover, but don't override preconfigured one
                 if ($removeMethodNeeded) {
                     $removeMethod = 'remove' . $singular;
                     // False alert
                     if (!$this->isAccessible($reflClass, $removeMethod, 1)) {
                         $removeMethod = null;
                     }
                 }
                 // Found all that we need. Abort search.
                 if ((!$addMethodNeeded || $addMethod) && (!$removeMethodNeeded || $removeMethod)) {
                     break;
                 }
                 // False alert
                 $addMethod = null;
                 $removeMethod = null;
             }
         }
         // Set preconfigured adder
         if ($this->allowAdd && $this->addMethod) {
             $addMethod = $this->addMethod;
             if (!$this->isAccessible($reflClass, $addMethod, 1)) {
                 throw new FormException(sprintf('The public method "%s" could not be found on class %s', $addMethod, $reflClass->getName()));
             }
         }
         // Set preconfigured remover
         if ($this->allowDelete && $this->removeMethod) {
             $removeMethod = $this->removeMethod;
             if (!$this->isAccessible($reflClass, $removeMethod, 1)) {
                 throw new FormException(sprintf('The public method "%s" could not be found on class %s', $removeMethod, $reflClass->getName()));
             }
         }
     }
     // Calculate delta between $data and the snapshot created in PRE_BIND
     $itemsToDelete = array();
     $itemsToAdd = is_object($data) ? clone $data : $data;
     if ($this->dataSnapshot) {
         foreach ($this->dataSnapshot as $originalItem) {
             foreach ($data as $key => $item) {
                 if ($item === $originalItem) {
                     // Item found, next original item
                     unset($itemsToAdd[$key]);
                     continue 2;
                 }
             }
             // Item not found, remember for deletion
             foreach ($originalData as $key => $item) {
                 if ($item === $originalItem) {
                     $itemsToDelete[$key] = $item;
                     continue 2;
                 }
             }
         }
     }
     if ($addMethod || $removeMethod) {
         // If methods to add and to remove exist, call them now, if allowed
         if ($removeMethod) {
             foreach ($itemsToDelete as $item) {
                 $parentData->{$removeMethod}($item);
             }
         }
         if ($addMethod) {
             foreach ($itemsToAdd as $item) {
                 $parentData->{$addMethod}($item);
             }
         }
         $event->setData($childPropertyPath->getValue($parentData));
     } elseif ($this->mergeStrategy & self::MERGE_NORMAL) {
         if (!$originalData) {
             // No original data was set. Set it if allowed
             if ($this->allowAdd) {
                 $originalData = $data;
             }
         } else {
             // Original data is an array-like structure
             // Add and remove items in the original variable
             if ($this->allowDelete) {
                 foreach ($itemsToDelete as $key => $item) {
                     unset($originalData[$key]);
                 }
             }
             if ($this->allowAdd) {
                 foreach ($itemsToAdd as $key => $item) {
                     if (!isset($originalData[$key])) {
                         $originalData[$key] = $item;
                     } else {
                         $originalData[] = $item;
                     }
                 }
             }
         }
         $event->setData($originalData);
     }
 }
Esempio n. 21
0
    public function onBindNormData(FilterDataEvent $event)
    {
        if (!$this->resizeOnBind) {
            return;
        }

        $form = $event->getForm();
        $data = $event->getData();

        if (null === $data) {
            $data = array();
        }

        if (!is_array($data) && !$data instanceof \Traversable) {
            throw new UnexpectedTypeException($data, 'array or \Traversable');
        }

        foreach ($data as $name => $child) {
            if (!$form->has($name)) {
                unset($data[$name]);
            }
        }

        $event->setData($data);
    }
Esempio n. 22
0
 public function onBindClientData(FilterDataEvent $event)
 {
     $values = (array) $event->getData();
     $indices = $this->choiceList->getIndicesForValues($values);
     $event->setData(count($indices) > 0 ? array_combine($indices, $values) : array());
 }