Skip to content

Commit

Permalink
Initial form refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Rabaix committed Aug 3, 2011
1 parent 11b32ce commit 0dc07ae
Show file tree
Hide file tree
Showing 33 changed files with 978 additions and 798 deletions.
16 changes: 5 additions & 11 deletions Admin/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -983,20 +983,23 @@ public function getFormBuilder($object = null, $options = array())
$object = $this->getNewInstance();
}

$this->setSubject($object);

// add the custom inline validation option
$metadata = $this->validator->getMetadataFactory()->getClassMetadata($this->class);
$metadata->addConstraint(new \Sonata\AdminBundle\Validator\Constraints\InlineConstraint(array(
'service' => $this,
'method' => 'validate'
)));

$options['data'] = $object;
$options['data_class'] = $this->getClass();

$formBuilder = $this->getFormContractor()->getFormBuilder(
$this->getUniqid(),
array_merge($this->formOptions, $options)
);

$formBuilder->setData($object);

$this->defineFormBuilder($formBuilder);

return $formBuilder;
Expand All @@ -1013,15 +1016,6 @@ public function defineFormBuilder(FormBuilder $formBuilder)
$this->buildFormFieldDescriptions();

$this->configureFormFields($mapper);

foreach ($this->getFormFieldDescriptions() as $fieldDescription) {
// do not add field already set in the configureFormField method
if ($mapper->has($fieldDescription->getFieldName())) {
continue;
}

$mapper->add($fieldDescription);
}
}

/**
Expand Down
19 changes: 19 additions & 0 deletions Admin/AdminHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
namespace Sonata\AdminBundle\Admin;

use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormView;
use Sonata\AdminBundle\Util\FormViewIterator;
use Sonata\AdminBundle\Util\FormBuilderIterator;

class AdminHelper
Expand Down Expand Up @@ -42,6 +44,23 @@ public function getChildFormBuilder(FormBuilder $formBuilder, $elementId)
return null;
}

/**
* @throws \RuntimeException
* @param \Symfony\Component\Form\FormView $formBuilder
* @param $elementId
* @return \Symfony\Component\Form\FormView
*/
public function getChildFormView(FormView $formView, $elementId)
{
foreach (new \RecursiveIteratorIterator(new FormViewIterator($formView), \RecursiveIteratorIterator::SELF_FIRST) as $name => $formView) {
if ($name == $elementId) {
return $formView;
}
}

return null;
}

/**
* @param string $code
* @return \Sonata\AdminBundle\Admin\AdminInterface
Expand Down
3 changes: 2 additions & 1 deletion Admin/BaseFieldDescription.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,13 +353,14 @@ public function getValue($object)
$getters[] = 'get'.$camelizedFieldName;
$getters[] = 'is'.$camelizedFieldName;


foreach ($getters as $getter) {
if (method_exists($object, $getter)) {
return call_user_func(array($object, $getter));
}
}

throw new NoValueException();
throw new NoValueException(sprintf('Unable to retrieve the value of `%s`', $this->getName()));
}

/**
Expand Down
8 changes: 0 additions & 8 deletions Builder/FormContractorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,6 @@ interface FormContractorInterface
*/
function __construct(FormFactoryInterface $formFactory);

/**
* @abstract
* @param \Symfony\Component\Form\FormBuilder $formBuilder
* @param \Sonata\AdminBundle\Admin\FieldDescriptionInterface $fieldDescription
* @return void
*/
function addField(FormBuilder $formBuilder, FieldDescriptionInterface $fieldDescription);

/**
* @abstract
* @param \Sonata\AdminBundle\Admin\AdminInterface $admin
Expand Down
Loading

0 comments on commit 0dc07ae

Please sign in to comment.