Przeglądaj źródła

Revert "[Form] changed the way default type names are created to avoid collisions"

This reverts commit d044498cdedea20c9e9a2e36a84e99577b2a26b3.

The reason for reverting is that the name is actually used to customize
the template on a per field basis:

{% block _post_excerpt_widget %}
    ***{{ block('text_widget') }}***
{% endblock %}

Here, post is the name of the Type.
Fabien Potencier 14 lat temu
rodzic
commit
41b7190efc

+ 4 - 2
src/Symfony/Component/Form/AbstractType.php

@@ -128,13 +128,15 @@ abstract class AbstractType implements FormTypeInterface
     /**
      * Returns the name of this type.
      *
-     * The default name type is the class name, where \ are replaced by _.
+     * The default name type is the class name without the Form nor Type suffix
      *
      * @return string The name of this type
      */
     public function getName()
     {
-        return strtolower(str_replace('\\', '_', get_class($this)));
+        preg_match('/\\\\(\w+?)(Form)?(Type)?$/i', get_class($this), $matches);
+
+        return strtolower($matches[1]);
     }
 
     /**

+ 4 - 4
tests/Symfony/Tests/Component/Form/AbstractTypeTest.php

@@ -19,28 +19,28 @@ class AbstractTypeTest extends \PHPUnit_Framework_TestCase
     {
         $type = new MyTest();
 
-        $this->assertEquals('symfony_tests_component_form_mytest', $type->getName());
+        $this->assertEquals('mytest', $type->getName());
     }
 
     public function testGetNameWithTypeSuffix()
     {
         $type = new MyTestType();
 
-        $this->assertEquals('symfony_tests_component_form_mytesttype', $type->getName());
+        $this->assertEquals('mytest', $type->getName());
     }
 
     public function testGetNameWithFormSuffix()
     {
         $type = new MyTestForm();
 
-        $this->assertEquals('symfony_tests_component_form_mytestform', $type->getName());
+        $this->assertEquals('mytest', $type->getName());
     }
 
     public function testGetNameWithFormTypeSuffix()
     {
         $type = new MyTestFormType();
 
-        $this->assertEquals('symfony_tests_component_form_mytestformtype', $type->getName());
+        $this->assertEquals('mytest', $type->getName());
     }
 }