Created
March 19, 2011 00:32
-
-
Save juzna/877080 to your computer and use it in GitHub Desktop.
Nette\Forms\FormContainer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class FormContainer { | |
/** | |
* Fill-in with values. | |
* @param array|Traversable values used to fill the form | |
* @param bool erase other controls? | |
* @return FormContainer provides a fluent interface | |
*/ | |
public function setValues($values, $erase = FALSE) | |
{ | |
if ($values instanceof \Traversable) { | |
$values = iterator_to_array($values); | |
} elseif (!is_array($values)) { | |
throw new \InvalidArgumentException("First parameter must be an array, " . gettype($values) ." given."); | |
} | |
$iterator = $this->getComponents(FALSE); | |
foreach ($iterator as $name => $control) { | |
if ($control instanceof IFormControl) { | |
if (array_key_exists($name, $values)) { | |
$control->setValue($values[$name]); | |
} elseif ($erase) { | |
$control->setValue(NULL); | |
} | |
} | |
if ($control instanceof FormContainer) { | |
if (isset($values[$name])) { | |
$control->setValues($values[$name], $erase); | |
} elseif ($erase) { | |
$control->setValues(array(), $erase); | |
} | |
} | |
} | |
return $this; | |
} | |
/** | |
* Returns the values submitted by the form. | |
* @return Nette\ArrayHash | |
*/ | |
public function getValues() | |
{ | |
$values = new Nette\ArrayHash; | |
$iterator = $this->getComponents(FALSE); | |
foreach ($iterator as $name => $control) { | |
if ($control instanceof IFormControl && !$control->isDisabled() && !$control instanceof ISubmitterControl) { | |
$values->$name = $control->getValue(); | |
} | |
if ($control instanceof FormContainer) { | |
$values->$name = $control->getValues(); | |
} | |
} | |
return $values; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment