Browse Source

added model transformer boolean to sonata boolean

Dominik Spengler 12 years ago
parent
commit
e08b05ac86

+ 35 - 0
Form/DataTransformer/BooleanToSonataBooleanTransformer.php

@@ -0,0 +1,35 @@
+<?php
+
+namespace Sonata\AdminBundle\Form\DataTransformer;
+
+use Symfony\Component\Form\DataTransformerInterface;
+use Sonata\AdminBundle\Form\Type\BooleanType;
+
+class BooleanToSonataBooleanTransformer implements DataTransformerInterface
+{
+
+    /**
+     * {@inheritdoc}
+     */
+    public function transform($value)
+    {
+        if ($value === true) {
+            return BooleanType::TYPE_YES;
+        }
+        
+        return BooleanType::TYPE_NO;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function reverseTransform($value)
+    {
+        if ($value === BooleanType::TYPE_YES) {
+            return true;
+        }
+        
+        return false;
+    }
+
+}

+ 7 - 0
Form/Type/BooleanType.php

@@ -12,6 +12,8 @@
 namespace Sonata\AdminBundle\Form\Type;
 
 use Symfony\Component\Form\AbstractType;
+use Symfony\Component\Form\FormBuilderInterface;
+use Sonata\AdminBundle\Form\DataTransformer\BooleanToSonataBooleanTransformer;
 
 use Symfony\Component\OptionsResolver\OptionsResolverInterface;
 
@@ -21,6 +23,11 @@ class BooleanType extends AbstractType
 
     const TYPE_NO = 2;
 
+    public function buildForm(FormBuilderInterface $builder, array $options)
+    {
+        $builder->addModelTransformer(new BooleanToSonataBooleanTransformer());
+    }
+    
     /**
      * {@inheritDoc}
      */