Chapter 6. Edit and New Views Configuration
===========================================
This chapter explains how to customize the read-and-write views: ``edit`` and
``new``. You'll learn all their configuration options and how to override or
tweak their templates.
Edit, New and Form Views
------------------------
The **Edit View** is displayed when modifying the contents of any existing
entity. The **New View** is used when creating new items of the given entity.
The design of both views is almost identical:
.. image:: ../images/easyadmin-edit-view.png
:alt: Edit view interface
The Special ``form`` View
~~~~~~~~~~~~~~~~~~~~~~~~~
Most of the times you apply the same or very similar configuration to both the
``edit`` and ``new`` views. Instead of duplicating the configuration, you can
use the special ``form`` view:
.. code-block:: yaml
easy_admin:
entities:
Customer:
class: AppBundle\Entity\Customer
form: # <-- 'form' is applied to both 'new' and 'edit' views
fields:
- 'id'
- { property: 'email', type: 'email', label: 'Contact' }
# ...
# ...
Any option defined in the ``form`` view is copied into the ``new`` and ``edit``
views. However, any option defined in the ``edit`` and ``new`` view overrides
the corresponding ``form`` option. In other words, always use the ``form``
action to define the common configuration, and then use the ``new`` and ``edit``
views to define just the specific options you want to override:
.. code-block:: yaml
easy_admin:
entities:
Customer:
class: AppBundle\Entity\Customer
form:
title: 'Add customer'
form_options: { validation_groups: ['Default'] }
new:
form_options: { validation_groups: ['Default', 'Customer'] }
edit:
title: 'Edit customer'
# ...
The above configuration is equivalent to the following:
.. code-block:: yaml
easy_admin:
entities:
Customer:
class: AppBundle\Entity\Customer
new:
title: 'Add customer'
form_options: { validation_groups: ['Default', 'Customer'] }
edit:
title: 'Edit customer'
form_options: { validation_groups: ['Default'] }
# ...
The merging of the ``form`` fields configuration is done recursively, so you can
change or add any option to any property. In addition, the following processing
takes place:
1. All the fields defined in the ``form`` view are copied in the same order into
the ``edit`` and ``new`` views.
2. Any field defined in the ``edit`` or ``new`` view which is not present in the
``form`` view is added after the ``form`` fields.
3. The ``edit`` and ``new`` views can remove any field defined in the ``form`` view
just by prefixing the name of the removed field with a dash ``-`` (e.g. add
a property called ``-name`` to remove the ``name`` property defined in ``form``)
Consider the following complex form field configuration:
.. code-block:: yaml
easy_admin:
entities:
Customer:
class: AppBundle\Entity\Customer
form:
fields:
- id
- { property: 'name', icon: 'user' }
- { property: 'email', css_class: 'input-large' }
new:
fields:
- '-id'
- { property: 'email', type_options: { required: false } }
edit:
fields:
- { property: 'name', icon: 'customer' }
- { property: 'email', help: 'Phone number is preferred' }
- phone
# ...
The above configuration is equivalent to the following:
.. code-block:: yaml
easy_admin:
entities:
Customer:
class: AppBundle\Entity\Customer
new:
fields:
- { property: 'name', icon: 'user' }
- { property: 'email', css_class: 'input-large', type_options: { required: false } }
edit:
fields:
- id
- { property: 'name', icon: 'customer' }
- { property: 'email', css_class: 'input-large', help: 'Phone number is preferred' }
- phone
# ...
General Configuration
---------------------
In order to make examples more concise, this section only shows the
configuration for the special ``form`` view, but you can apply the same options
to the other ``edit`` and ``new`` views.
Customize the Title of the Page
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This option refers both to the value of the ``
`` element and to the visible
title displayed at the top of the page. By default the title is just the name of
the entity. Define the ``title`` option to set a custom page title:
.. code-block:: yaml
# app/config/config.yml
easy_admin:
entities:
Customer:
class: AppBundle\Entity\Customer
label: 'Customers'
form:
title: "Add/edit customers"
# ...
The ``title`` option can include the following special variables:
* ``%entity_label%``, resolves to the value defined in the ``label`` option of
the entity. If you haven't defined it, this value will be equal to the
entity name. In the example above, this value would be ``Customers``.
* ``%entity_name%``, resolves to the entity name, which is the YAML key used
to configure the entity in the backend configuration file. In the example
above, this value would be ``Customer``.
* ``%entity_id%``, it's only available for the ``edit`` view and it resolves to
the value of the primary key of the entity being edited. Even if the option
is called ``entity_id``, it also works for primary keys with names different
from ``id``.
.. caution::
In Symfony applications, YAML values enclosed with ``%`` and ``%`` have a
special meaning (they are considered container parameters). Escape these
values doubling the ``%`` characters:
.. code-block:: yaml
easy_admin:
entities:
Customer:
class: AppBundle\Entity\Customer
label: 'Customers'
form:
title: '%%entity_label%% listing'
# ...
If several entities use the same custom title, you can define the default title
for all entities in the global ``edit.title`` and ``new.title`` options (these
global titles are always overridden by the title defined by each entity):
.. code-block:: yaml
# app/config/config.yml
easy_admin:
edit:
title: '%%entity_label%%_edit'
new:
title: 'New %%entity_label%%'
Display a Help Message in the Page
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Entities can define a global help message that is displayed below the title of
the page. This is useful to add instructions or warning messages for the end
users (e.g. "The upload process can take a lot of time (don't close the browser
window)").
The help message is defined with the ``help`` configuration option, which can be
added to the entity (all views display the same message) and to each of the
entity views:
.. code-block:: yaml
# app/config/config.yml
easy_admin:
entities:
Customer:
class: AppBundle\Entity\Customer
help: 'Global message displayed in all views'
# ...
form:
help: 'The form view overrides the global help message'
# ...
edit:
# 'help' is not defined, so the global help message is displayed
# ...
new:
# use the null value to not display the inherited global help message
help: null
# ...
# ...
Customize the Properties Displayed
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
By default, the ``edit`` and ``new`` views display all the entity properties.
Use the ``fields`` option to explicitly set the properties to display in each
view:
.. code-block:: yaml
# app/config/config.yml
easy_admin:
entities:
Customer:
class: AppBundle\Entity\Customer
form:
fields: ['firstName', 'lastName', 'phone', 'email']
# ...
This option is also useful to reorder the form fields, because by default they
are displayed in the same order as defined in the related Doctrine entity.
.. note::
Fields that represent an association with another entity are displayed as
``