Add support for nested form types

This commit is contained in:
William DURAND 2012-07-24 18:42:05 +02:00
parent 15a4558277
commit 83c91a3d9d

View File

@ -55,12 +55,22 @@ class FormTypeParser
if (is_string($type) && class_exists($type)) { if (is_string($type) && class_exists($type)) {
$type = new $type(); $type = new $type();
} }
$form = $this->formFactory->create($type); $form = $this->formFactory->create($type);
return $this->parseForm($form);
}
private function parseForm($form, $prefix = null)
{
$parameters = array(); $parameters = array();
foreach ($form as $name => $child) { foreach ($form as $name => $child) {
$config = $child->getConfig(); $config = $child->getConfig();
if ($prefix) {
$name = sprintf('%s[%s]', $prefix, $name);
}
$bestType = ''; $bestType = '';
for ($type = $config->getType(); null !== $type; $type = $type->getParent()) { for ($type = $config->getType(); null !== $type; $type = $type->getParent()) {
if (isset($this->mapTypes[$type->getName()])) { if (isset($this->mapTypes[$type->getName()])) {
@ -68,6 +78,17 @@ class FormTypeParser
} }
} }
if ('' === $bestType) {
if ($type = $config->getType()) {
if ($type = $type->getInnerType()) {
$subForm = $this->formFactory->create($type);
$parameters = array_merge($parameters, $this->parseForm($subForm, $name));
continue;
}
}
}
$parameters[$name] = array( $parameters[$name] = array(
'dataType' => $bestType, 'dataType' => $bestType,
'required' => $config->getRequired(), 'required' => $config->getRequired(),