-
Notifications
You must be signed in to change notification settings - Fork 15
Using Data Transfer Objects
Iltar van der Berg edited this page Apr 6, 2017
·
1 revision
In case you are using data transfer objects instead of binding your entities directly, you'll have to add all required information to your data object. In case of a EditUserType
, this could be a EditUserData
. In order to get the Account
entity into the handler, you can create the data object with an Account
as required value. The data object would probably look similar to this:
<?php
namespace App\FormData;
use App\Entity\Account;
final class EditUserData
{
private $account;
private $first_name;
// ...
// other field for the form
public function __construct(Account $account)
{
$this->account = $account;
// default form values
$this->first_name = $account->getFirstName();
// ...
}
public function getAccount(): Account
{
return $this->account;
}
// ...
// other getters and setters for the form
}
The controller needs a small change for this. Instead of setting the Account
as data object, you have to create the EditUserData
.
<?php
namespace App\Controller;
use App\Entity\Account;
use App\FormData\EditUserData;
use App\FormHandler\EditUserFormHandler;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
class EditUserController extends Controller
{
/**
* @Route("/edit-user/{id}/", name="app.edit-user")
*/
public function editUserAction(Request $request, Account $account)
{
$handler = $this->get('hostnet.form_handler.factory')->create(EditUserFormHandler::class);
$data = new EditUserData($account);
if (($response = $handler->handle($request, $data)) instanceof RedirectResponse) {
return $response;
}
return $this->renderView('edit_user.html.twig', ['form' => $handler->getForm()->createView());
}
}
When handling this data in your onSuccess
callback, you can now transfer the data from your data transfer object, into the entity.