Skip to content

Commit

Permalink
Add preValidate method.
Browse files Browse the repository at this point in the history
  • Loading branch information
Koc committed Dec 15, 2015
1 parent f9d4621 commit 1e6bc7d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 5 deletions.
7 changes: 7 additions & 0 deletions Admin/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,13 @@ public function delete($object)
}
}

/**
* {@inheritdoc}
*/
public function preValidate($object)
{
}

/**
* {@inheritdoc}
*/
Expand Down
8 changes: 8 additions & 0 deletions Admin/AdminInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,14 @@ public function create($object);
*/
public function delete($object);

//TODO: uncomment this method for 3.0
// /**
// * @param mixed $object
// *
// * @return mixed
// */
// public function preValidate($object);

/**
* @param mixed $object
*
Expand Down
8 changes: 8 additions & 0 deletions Controller/CRUDController.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,10 @@ public function editAction($id = null, Request $request = null)
$form->handleRequest($request);

if ($form->isSubmitted()) {
//TODO: remove this check for 3.0
if (method_exists($this->admin, 'preValidate')) {
$this->admin->preValidate($object);
}
$isFormValid = $form->isValid();

// persist if the form was valid and if in preview mode the preview was approved
Expand Down Expand Up @@ -686,6 +690,10 @@ public function createAction(Request $request = null)
$form->handleRequest($request);

if ($form->isSubmitted()) {
//TODO: remove this check for 3.0
if (method_exists($this->admin, 'preValidate')) {
$this->admin->preValidate($object);
}
$isFormValid = $form->isValid();

// persist if the form was valid and if in preview mode the preview was approved
Expand Down
11 changes: 6 additions & 5 deletions Resources/doc/reference/saving_hooks.rst
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
Saving hooks
============

When a SonataAdmin is submitted for processing, two events are always called. One
is before any persistence layer interaction and the other is afterwards, the
When a SonataAdmin is submitted for processing, there are some events called. One
is before any persistence layer interaction and the other is afterward. Also between submitting
and validating for edit and create actions ``preValidate`` event called. The
events are named as follows:

- new object : ``prePersist($object)`` / ``postPersist($object)``
- edited object : ``preUpdate($object)`` / ``postUpdate($object)``
- new object : ``preValidate($object)`` / ``prePersist($object)`` / ``postPersist($object)``
- edited object : ``preValidate($object)`` / ``preUpdate($object)`` / ``postUpdate($object)``
- deleted object : ``preRemove($object)`` / ``postRemove($object)``

It is worth noting that the update events are called whenever the Admin is successfully
submitted, regardless of whether there are any actual persistence layer events. This
differs from the use of preUpdate and postUpdate events in DoctrineORM and perhaps some
differs from the use of ``preUpdate`` and ``postUpdate`` events in DoctrineORM and perhaps some
other persistence layers.

For example: if you submit an edit form without changing any of the values on the form
Expand Down

0 comments on commit 1e6bc7d

Please sign in to comment.