Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Fallback form creation to default form type if given purpose has no associated form type #15

Merged
merged 1 commit into from

3 participants

Gildas Quéméner Konstantin Kudryashov Florian Klein
Gildas Quéméner

No description provided.

Konstantin Kudryashov

Perfect!

Konstantin Kudryashov everzet merged commit a8e5c96 into from
Gildas Quéméner

I've thought again about the form type creator resolving rules and currently it follows this one:

  1. Does PurposeEntityType exist? use it.
  2. Does EntityType exist? use it.
  3. Does Entity have a parent? Does ParentEntityType exist? use it.
  4. Use none (meaning let the default creator handle this entity)

I think it'll be better to follow this one:

  1. Does PurposeEntityType exist? use it.
  2. Does Entity have a parent? Does PurposeParentEntityType exist? use it.
  3. Does EntityType exist? use it.
  4. Does Entity have a parent? Does ParentEntityType exist? use it.
  5. Use none.
Konstantin Kudryashov

@gquemener I think you should remove parent entities support altogether from rule or convention becomes too complex. Convention should be easy and simple to describe. And your "use parent type if no current type available" is easily achievable by creating simply empty type class, that extends parent.

Konstantin Kudryashov

We need to preserve balance between complexity of manual implementation and complexity of convention rules.

Konstantin Kudryashov

So, rule becomes simple:

  1. Does PurposeEntityType exist? use it.
  2. Does EntityType exist? use it.
  3. Pass resolving task to next creator
Florian Klein
Owner

see #16

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 8, 2012
  1. Fallback form creation to default form type if given purpose has no a…

    Gildas Quéméner authored
    …ssociated form type
This page is out of date. Refresh to see the latest.
31 spec/Knp/RadBundle/Form/FormTypeCreator.php
View
@@ -67,6 +67,37 @@ function it_should_return_form_type_with_purpose_if_there_is_one($object, $fetch
* @param stdClass $formType
* @param Symfony\Component\Form\Form $form
*/
+ function it_should_fallback_on_default_form_type_if_given_purpose_has_no_associated_form_type($object, $fetcher, $factory, $formType, $form)
+ {
+ $fetcher->getClass($object)->willReturn('App\Entity\Cheese');
+ $fetcher->classExists('App\Form\EditCheeseType')->willReturn(false);
+ $fetcher->classExists('App\Form\CheeseType')->willReturn(true);
+ $fetcher->newInstance('App\Form\CheeseType')->shouldBeCalled()->willReturn($formType);
+ $factory->create($formType, $object, array())->shouldBeCalled()->willReturn($form);
+
+ $this->create($object, 'edit')->shouldReturn($form);
+ }
+
+ /**
+ * @param stdClass $object
+ * @param stdClass $formType
+ * @param Symfony\Component\Form\Form $form
+ */
+ function it_should_return_null_if_given_purpose_has_no_associated_form_type_and_no_default_form_type($object, $fetcher, $factory, $formType, $form)
+ {
+ $fetcher->getClass($object)->willReturn('App\Entity\Cheese');
+ $fetcher->getParentClass('App\Entity\Cheese')->willReturn(null);
+ $fetcher->classExists('App\Form\CheeseType')->willReturn(false);
+ $fetcher->classExists('App\Form\EditCheeseType')->willReturn(false);
+
+ $this->create($object, 'edit')->shouldReturn(null);
+ }
+
+ /**
+ * @param stdClass $object
+ * @param stdClass $formType
+ * @param Symfony\Component\Form\Form $form
+ */
function it_should_return_parent_form_type_if_no_current_found($object, $fetcher, $factory, $formType, $form)
{
$fetcher->getClass($object)->willReturn('App\Entity\Roquefort');
10 src/Knp/RadBundle/Form/FormTypeCreator.php
View
@@ -34,10 +34,14 @@ private function getFormType($object, $purpose = null)
$formClass = sprintf('App\Form\%s%sType', ucfirst($purpose), end($arr));
if (!$this->fetcher->classExists($formClass)) {
- if ($parentClass = $this->fetcher->getParentClass($object)) {
- return $this->getFormType($parentClass, $purpose);
+ if (null === $purpose) {
+ if ($parentClass = $this->fetcher->getParentClass($object)) {
+ return $this->getFormType($parentClass, $purpose);
+ }
+ return;
+ } else {
+ return $this->getFormType($objectClass);
}
- return;
}
return $formClass;
Something went wrong with that request. Please try again.