浏览代码

Added options to admin_type to allow to hide delete

Hugo Briand 10 年之前
父节点
当前提交
480ae9030c
共有 2 个文件被更改,包括 29 次插入6 次删除
  1. 13 1
      Form/Type/AdminType.php
  2. 16 5
      Resources/doc/reference/form_types.rst

+ 13 - 1
Form/Type/AdminType.php

@@ -30,12 +30,17 @@ class AdminType extends AbstractType
     public function buildForm(FormBuilderInterface $builder, array $options)
     {
         $admin = clone $this->getAdmin($options);
+
         if ($admin->hasParentFieldDescription()) {
             $admin->getParentFieldDescription()->setAssociationAdmin($admin);
         }
 
         if ($options['delete'] && $admin->isGranted('DELETE')) {
-            $builder->add('_delete', 'checkbox', array('required' => false, 'mapped' => false, 'translation_domain' => $admin->getTranslationDomain()));
+            if (!array_key_exists('translation_domain', $options['delete_options']['type_options'])) {
+                $options['delete_options']['type_options']['translation_domain'] = $admin->getTranslationDomain();
+            }
+
+            $builder->add('_delete', $options['delete_options']['type'], $options['delete_options']['type_options']);
         }
 
         $admin->setSubject($builder->getData());
@@ -65,6 +70,13 @@ class AdminType extends AbstractType
             'delete'          => function (Options $options) {
                 return ($options['btn_delete'] !== false);
             },
+            'delete_options'  => array(
+                'type'         => 'checkbox',
+                'type_options' => array(
+                    'required' => false,
+                    'mapped'   => false,
+                ),
+            ),
             'auto_initialize' => false,
             'btn_add'         => 'link_add',
             'btn_list'        => 'link_list',

+ 16 - 5
Resources/doc/reference/form_types.rst

@@ -176,9 +176,9 @@ model_manager
 
 callback
   defaults to null. Callable function that can be used to modify the query which is used to retrieve autocomplete items.
-  The callback should receive three parameters - the Admin instance, the property (or properties) defined as searchable and the 
+  The callback should receive three parameters - the Admin instance, the property (or properties) defined as searchable and the
   search value entered by the user.
-  
+
   From the ``$admin`` parameter it is possible to get the ``Datagrid`` and the ``Request``:
 
 .. code-block:: php
@@ -332,8 +332,19 @@ to the underlying forms.
         {
             $formMapper
                 ->add('sales', 'sonata_type_collection', array(
-                    // Prevents the "Delete" option from being displayed
-                    'type_options' => array('delete' => false)
+                    'type_options' => array(
+                        // Prevents the "Delete" option from being displayed
+                        'delete' => false,
+                        'delete_options' => array(
+                            // You may otherwise choose to put the field but hide it
+                            'type'         => 'hidden',
+                            // In that case, you need to fill in the options as well
+                            'type_options' => array(
+                                'mapped'   => false,
+                                'required' => false,
+                            )
+                        )
+                    )
                 ), array(
                     'edit' => 'inline',
                     'inline' => 'table',
@@ -354,7 +365,7 @@ btn_add and btn_catalogue:
 **TIP**: A jQuery event is fired after a row has been added (``sonata-admin-append-form-element``).
 You can listen to this event to trigger custom javascript (eg: add a calendar widget to a newly added date field)
 
-**TIP**: Setting the 'required' option to true does not cause a requirement of 'at least one' child entity. 
+**TIP**: Setting the 'required' option to true does not cause a requirement of 'at least one' child entity.
 Setting the 'required' option to false causes all nested form fields to become not required as well.
 
 sonata_type_native_collection (previously collection)