Exemplo n.º 1
0
 /**
  * Map Entity to form
  *
  * @param object $entity
  * @param Container $container
  */
 public function setEntityToContainer($entity, Container $container)
 {
     // go throw all components and try find values for it
     /** @var BaseControl $component */
     foreach ($container->getComponents() as $component) {
         $this->mapValueToComponent($entity, $component);
     }
 }
 public function getComponents($deep = FALSE, $filterType = NULL)
 {
     $form = $this->getForm(false);
     if ($form && $form->isAnchored()) {
         $this->initialize();
     }
     return parent::getComponents($deep, $filterType);
 }
Exemplo n.º 3
0
 public function entityToForm($entity, Nette\Forms\Container $form)
 {
     foreach ($form->getComponents() as $name => $control) {
         if (isset($entity->{$name})) {
             $form[$name]->setValue($entity->{$name});
         }
     }
     return $form;
 }
Exemplo n.º 4
0
 /**
  * @param BaseControl|Container $formElement
  * @return array|\ArrayIterator
  * @throws \Kdyby\DoctrineForms\InvalidArgumentException
  */
 private static function iterate($formElement)
 {
     if ($formElement instanceof Container) {
         return $formElement->getComponents();
     } elseif ($formElement instanceof IControl) {
         return array($formElement);
     } else {
         throw new Kdyby\DoctrineForms\InvalidArgumentException('Expected Nette\\Forms\\Container or Nette\\Forms\\IControl, but ' . get_class($formElement) . ' given');
     }
 }
Exemplo n.º 5
0
 /**
  * @param  Nette\Forms\Container  $container
  * @param  array|\Iterator  $values
  * @return void
  */
 public function setFilterContainerDefaults(Nette\Forms\Container $container, $values)
 {
     foreach ($container->getComponents() as $key => $control) {
         if (!isset($values[$key])) {
             continue;
         }
         if ($control instanceof Nette\Forms\Container) {
             $this->setFilterContainerDefaults($control, $values[$key]);
             continue;
         }
         $value = $values[$key];
         if ($value instanceof \DateTime && ($filter = $this->getFilter($key)) instanceof IFilterDate) {
             $value = $value->format($filter->getPhpFormat());
         }
         try {
             $control->setValue($value);
         } catch (Nette\InvalidArgumentException $e) {
             if ($this->strict_session_filter_values) {
                 throw $e;
             }
         }
     }
 }
Exemplo n.º 6
0
 /**
  * @param \Nette\Forms\Container $container
  * @param boolean $cleanUpGroups
  *
  * @throws \Nette\InvalidArgumentException
  * @return void
  */
 public function remove(Nette\Forms\Container $container, $cleanUpGroups = FALSE)
 {
     if ($container->parent !== $this) {
         throw new Nette\InvalidArgumentException('Given component ' . $container->name . ' is not children of ' . $this->name . '.');
     }
     if ($this->forceDefault && count($this->created) <= $this->createDefault) {
         return;
     }
     // to check if form was submitted by this one
     foreach ($container->getComponents(TRUE, 'Nette\\Forms\\ISubmitterControl') as $button) {
         /** @var \Nette\Forms\Controls\SubmitButton $button */
         if ($button->isSubmittedBy()) {
             $this->submittedBy = TRUE;
             break;
         }
     }
     /** @var \Nette\Forms\Controls\BaseControl[] $components */
     $components = $container->getComponents(TRUE);
     $this->removeComponent($container);
     unset($this->created[$container->name]);
     // reflection is required to hack form groups
     $groupRefl = Nette\Reflection\ClassType::from('Nette\\Forms\\ControlGroup');
     $controlsProperty = $groupRefl->getProperty('controls');
     $controlsProperty->setAccessible(TRUE);
     // walk groups and clean then from removed components
     $affected = array();
     foreach ($this->getForm()->getGroups() as $group) {
         /** @var \SplObjectStorage $groupControls */
         $groupControls = $controlsProperty->getValue($group);
         foreach ($components as $control) {
             if ($groupControls->contains($control)) {
                 $groupControls->detach($control);
                 if (!in_array($group, $affected, TRUE)) {
                     $affected[] = $group;
                 }
             }
         }
     }
     // remove affected & empty groups
     if ($cleanUpGroups && $affected) {
         foreach ($this->getForm()->getComponents(FALSE, 'Nette\\Forms\\Container') as $container) {
             if ($index = array_search($container->currentGroup, $affected, TRUE)) {
                 unset($affected[$index]);
             }
         }
         /** @var \Nette\Forms\ControlGroup[] $affected */
         foreach ($affected as $group) {
             if (!$group->getControls() && in_array($group, $this->getForm()->getGroups(), TRUE)) {
                 $this->getForm()->removeGroup($group);
             }
         }
     }
 }
Exemplo n.º 7
0
 public function setFilterContainerDefaults(Nette\Forms\Container $container, array $values)
 {
     foreach ($container->getComponents() as $name => $control) {
         if ($control instanceof Nette\Forms\IControl) {
             if (array_key_exists($name, $values)) {
                 try {
                     $control->setValue($values[$name]);
                 } catch (Nette\InvalidArgumentException $e) {
                     if ($this->strict_session_filter_values) {
                         throw $e;
                     }
                 }
             }
         } elseif ($control instanceof Nette\Forms\Container) {
             if (array_key_exists($name, $values)) {
                 try {
                     $control->setValues($values[$name]);
                 } catch (Nette\InvalidArgumentException $e) {
                     if ($this->strict_session_filter_values) {
                         throw $e;
                     }
                 }
             }
         }
     }
 }