Przeglądaj źródła

Merge pull request #1962 from Bladrak/craueCode

Added documentation & unit tests to PR #174
Thomas 11 lat temu
rodzic
commit
6243ed9ee8

+ 8 - 2
Admin/BaseFieldDescription.php

@@ -325,14 +325,20 @@ abstract class BaseFieldDescription implements FieldDescriptionInterface
      */
      */
     public function getFieldValue($object, $fieldName)
     public function getFieldValue($object, $fieldName)
     {
     {
+        $code = $this->getOption('code');
+
+        if (is_callable($code)) {
+            return call_user_func($code, $object);
+        }
+
         $camelizedFieldName = self::camelize($fieldName);
         $camelizedFieldName = self::camelize($fieldName);
 
 
         $getters = array();
         $getters = array();
         $parameters = array();
         $parameters = array();
         
         
         // prefer method name given in the code option
         // prefer method name given in the code option
-        if ($this->getOption('code')) {
-            $getters[] = $this->getOption('code');
+        if ($code) {
+            $getters[] = $code;
         }
         }
         // parameters for the method given in the code option
         // parameters for the method given in the code option
         if($this->getOption('parameters')){
         if($this->getOption('parameters')){

+ 9 - 0
Resources/doc/reference/form_types.rst

@@ -287,6 +287,15 @@ General
         <?php
         <?php
         $form->add('status', null, array('label' => false);
         $form->add('status', null, array('label' => false);
 
 
+- ``code``: You can set the ``code`` option to a closure if you want to customize the value returned (will take the object as a parameter).
+
+.. code-block:: php
+
+        <?php
+        $form->add('status', null, array('code' => function ($objectWithStatus) {
+            return sprintf("Status: %s", $objectWithStatus->getStatus());
+        });
+
 .. _`Symfony field types`: http://symfony.com/doc/current/book/forms.html#built-in-field-types
 .. _`Symfony field types`: http://symfony.com/doc/current/book/forms.html#built-in-field-types
 .. _`Symfony choice Field Type docs`: http://symfony.com/doc/current/reference/forms/types/choice.html
 .. _`Symfony choice Field Type docs`: http://symfony.com/doc/current/reference/forms/types/choice.html
 .. _`Symfony PropertyPath`: http://api.symfony.com/2.0/Symfony/Component/Form/Util/PropertyPath.html
 .. _`Symfony PropertyPath`: http://api.symfony.com/2.0/Symfony/Component/Form/Util/PropertyPath.html

+ 10 - 0
Tests/Admin/BaseFieldDescriptionTest.php

@@ -95,6 +95,16 @@ class BaseFieldDescriptionTest extends \PHPUnit_Framework_TestCase
     }
     }
 
 
     public function testGetValue()
     public function testGetValue()
+    {
+        $description = new FieldDescription();
+
+        $mock = $this->getMock('stdClass', array('getFake'));
+        $mock->expects($this->once())->method('getFake')->will($this->returnValue(42));
+
+        $this->assertEquals(42, $description->getFieldValue($mock, 'fake'));
+    }
+
+    public function testGetValueCode()
     {
     {
         $description = new FieldDescription();
         $description = new FieldDescription();
         $description->setOption('code', 'getFoo');
         $description->setOption('code', 'getFoo');