vukojevicf
vukojevicf

Reputation: 827

Symfony 6 and Easy Admin: add fields to form that are not part of the entity

Quick question, hopefully easily solvable, I'm just stuck atm.

Let's say I have a User table that has a string column address. Using EasyAdmin bundle I can very easily do this:

yield TextField::new('address', 'Address')->onlyOnIndex();

This is brilliant for my table view, but what if I wanted to split address into street, houseNumber and zipCode in my creation flow? My address has this form: street houseNumber, zipCode and it is defined in this class:

<?php

declare(strict_types=1);

namespace App\Domain\UserManagement\VO;

use Assert\Assertion;
use Assert\AssertionFailedException;

final class Address
{
    private string $address;

    public function __construct(private string $street, private string $houseNumber, private string $zipcode)
    {
        $this->validateStreet($this->street);
        $this->validateZipcode($this->zipcode);

        $this->createAddress();
    }

    public function value(): string
    {
        return $this->address;
    }

    private function createAddress(): void
    {
        $this->address = $this->street . ' ' . $this->houseNumber . ', ' . $this->zipcode;
    }

    private function validateStreet(string $street): void
    {
        try {
            Assertion::notEmpty($street, 'Street cannot be empty');
            Assertion::string($street, 'Street must be a valid string');
            Assertion::minLength($street, 1, 'Street cannot be blank');
        } catch (AssertionFailedException $e) {
            throw new \InvalidArgumentException($e->getMessage());
        }
    }

    private function validateZipcode(string $zipcode): void
    {
        try {
            Assertion::numeric($zipcode, 'Zipcode must contain only numbers');
        } catch (AssertionFailedException $e) {
            throw new \InvalidArgumentException($e->getMessage());
        }
    }
}

Now I need to find a way to split address into 3 form fields on edit and create, and merge them back together once the form is submitted.

Upvotes: 0

Views: 309

Answers (1)

Anton
Anton

Reputation: 161

You can create unmapped fields in your property, that means you can create getter and setter methods which are not "used" by doctrine.

For example, if you want to show not first_name and last_name in your backend, you can add a getFullName() getter in the entity:

use Doctrine\ORM\Mapping as ORM;

/** @ORM\Entity */
class Customer
{
    // ...

    public function getFullName()
    {
        return $this->getFirstName().' '.$this->getLastName();
    }
}

and in the EasyAdmin-Controller you can ad the field fullName:

public function configureFields(string $pageName): iterable
{
    return [
        TextField::new('fullName'),
        // ...
    ];
}

The getter are standard PHP methods, so it's also possible to manipulate a string like

use Doctrine\ORM\Mapping as ORM;

/** @ORM\Entity */
class Customer
{
    // ...

    public function getStreetFromAddress()
    {
        // Content of $this->address:
        // ($this->address may mapped as text or string)
        // 
        // 132, My Street,
        // Kingston, New York 12401
        // United States

        $addressParts = explode(PHP_EOL, $this->address);

        return $addressParts[0];
    }
}

See also this part of the documentation of EasyAdmin: https://symfony.com/bundles/EasyAdminBundle/current/fields.html#unmapped-fields

Upvotes: 0

Related Questions