Skip to content

Commit

Permalink
Added options to admin_type to allow to hide delete
Browse files Browse the repository at this point in the history
  • Loading branch information
Bladrak committed Aug 28, 2014
1 parent 5c01801 commit 480ae90
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
14 changes: 13 additions & 1 deletion Form/Type/AdminType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -65,6 +70,13 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
'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',
Expand Down
21 changes: 16 additions & 5 deletions Resources/doc/reference/form_types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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',
Expand All @@ -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)
Expand Down

0 comments on commit 480ae90

Please sign in to comment.