Created
February 3, 2011 16:42
-
-
Save rande/809747 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* This method can be overwritten to tweak to form construction, by default the form | |
* is built by reading the FieldDescription | |
* | |
* @return void | |
*/ | |
protected function configureFormFields(Form $form) | |
{ | |
foreach ($this->getFormFields() as $fieldDescription) { | |
if (!$fieldDescription->getType()) { | |
continue; | |
} | |
$this->addFormField($form, $fieldDescription); | |
} | |
} | |
protected function addFormField($form, $name, array $options = array()) | |
{ | |
$field = false; | |
$object = $form->getData(); | |
if ($name instanceof FieldDescription) { | |
$fieldDescription = $name; | |
$name = $fieldDescription->getName(); | |
} else if ($name instanceof FormInterface) { | |
$field = $name; | |
$name = $field->getKey(); | |
$fieldDescription = new FieldDescription; | |
$fieldDescription->setFieldName($name); | |
$this->fixFormFieldDescription($fieldDescription); | |
$this->formFields[$name] = $fieldDescription; | |
} else if (is_string($name) && !array_key_exists($name, $this->formFields)) { | |
// the provided $name | |
$fieldDescription = new FieldDescription; | |
$fieldDescription->setFieldName($name); | |
$this->fixFormFieldDescription($fieldDescription); | |
$this->formFields[$name] = $fieldDescription; | |
} else if (is_string($name) && array_key_exists($name, $this->formFields)) { | |
$fieldDescription = $this->formFields[$name]; | |
} | |
if(!$field) { | |
switch ($fieldDescription->getType()) { | |
case ClassMetadataInfo::ONE_TO_MANY: | |
$field = $this->getOneToManyField($object, $fieldDescription); | |
break; | |
case ClassMetadataInfo::MANY_TO_MANY: | |
$field = $this->getManyToManyField($object, $fieldDescription); | |
break; | |
case ClassMetadataInfo::MANY_TO_ONE: | |
$field = $this->getManyToOneField($object, $fieldDescription); | |
break; | |
case ClassMetadataInfo::ONE_TO_ONE: | |
$field = $this->getOneToOneField($object, $fieldDescription); | |
break; | |
default: | |
$class = $this->getFormFieldClass($fieldDescription); | |
$options = $fieldDescription->getOption('form_field_options', array()); | |
// there is no way to use a custom widget with the FieldFactory | |
if($class) { | |
$field = new $class($fieldDescription->getFieldName(), $options); | |
} else { | |
$field = $this->container->get('form.field_factory')->getInstance($this->getClass(), $fieldDescription->getFieldName(), $options); | |
} | |
} | |
} | |
$form->add($field); | |
} | |
/** | |
* return a form depend on the given $object and FieldDescription $fields array | |
* | |
* @throws RuntimeException | |
* @param $object | |
* @return Symfony\Component\Form\Form | |
*/ | |
public function getForm($object) | |
{ | |
$form = $this->getBaseForm($object); | |
$this->configureFormFields($form); | |
return $form; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment