Form::removeGroup()
Upozornění: Tohle vlákno je hodně staré a informace nemusí být platné pro současné Nette.

- Jan Tvrdík
 - Nette guru | 2595
 
Pokus o opravu Form::removeGroup z revize 476. Ještě by to měl ale někdo překopat a hlavně celé pořádně překontrolovat.
	/**
	 * Remove fieldset group from form.
	 * @param  string|FormGroup
	 * @return void
	 */
	public function removeGroup($name)
	{
		if (is_string($name) && isset($this->groups[$name])) {
			$group = $this->groups[$name];
		} elseif ($name instanceof FormGroup) {
			$group = $name;
			$name = $group->getOption('caption');
			if (!isset($name)) {
				$name = array_search($group, $this->groups);
				if ($name === FALSE) {
					throw new InvalidArgumentException("Group not found in form '$this->name'");
				}
			}
			if (!isset($this->groups[$name])) {
				throw new InvalidArgumentException("Group '$name' not found in form '$this->name'");
			}
		} else {
			if (is_string($name))
				throw new InvalidArgumentException("Group '$name' not found in form '$this->name'");
			else
				throw new InvalidArgumentException("Group not found in form '$this->name'");
		}
		foreach ($group->getControls() as $control) {
			$this->removeComponent($control);
		}
		unset($this->groups[$name]);
	}
					Editoval Jan Tvrdík (6. 8. 2009 9:18)

- PetrP
 - Člen | 587
 
A co rovnou podobně rozšířit i
FormContainer::setCurrentGroup() aby se mu mohl taky dát
i název groupy
	/**
	 * @param  FormGroup|string
	 * @return void
	 */
	public function setCurrentGroup($group = NULL)
	{
		if ($group === NULL) {
			$this->currentGroup = NULL;
		} else if ($group instanceof FormGroup) {
			$this->currentGroup = $group;
		}	else if (is_string($group) AND $form = $this->getForm(FALSE)) {
			$formGroup = $form->getGroup($group);
			if (isset($formGroup)) {
				$this->currentGroup = $formGroup;
			} else {
				throw new InvalidArgumentException("Group '$group' not found in form '$form->name'");
			}
		} else {
			throw new InvalidArgumentException("Expected FromGroup, '".(is_object($group)?get_class($group):gettype($group))."' given.");
		}
	}
				
- romansklenar
 - Člen | 655
 
Jan Tvrdík: Zbytečně komplikované a taky jsou tam chyby.
Překvapilo mě, že taková metoda ve formulářích není. Pak mi ale došlo, proč nejspíše David tuto metodu neimplementoval a to jsou nepojmenované skupiny.
$form->addGroup();
$form->addGroup('group');
$form->addGroup();
Není je podle čeho odstranit. S tím se budem muset smířit. Celé by to mohlo vypadat takto:
	/**
	 * Remove fieldset group from form.
	 * @param  string|FormGroup
	 * @return void
	 */
	public function removeGroup($name)
	{
		if ($name instanceof FormGroup) {
			$name = $name->getOption('label');
		}
		if ($name === NULL) {
			throw new InvalidArgumentException("Name of group must be given, non-named groups cannot be removed.");
		}
		$group = $this->getGroup($name);
		if (!$group instanceof FormGroup) {
			throw new InvalidArgumentException("Group '$name' not found in form '$this->name'");
		}
		if ($this->currentGroup === $group) {
			$this->setCurrentGroup();
		}
		foreach ($group->getControls() as $control) {
			$this->removeComponent($control);
		}
		unset($this->groups[$name]);
	}