Skip to content

Commit

Permalink
bug #6028 Pass the original value to formatValue() method (javieregui…
Browse files Browse the repository at this point in the history
…luz)

This PR was squashed before being merged into the 4.x branch.

Discussion
----------

Pass the original value to formatValue() method

Fixes #5941.

The `formatValue()` callable should always receive the original field value, not the formatted one.

Commits
-------

ec051b0 Pass the original value to formatValue() method
  • Loading branch information
javiereguiluz committed Nov 19, 2023
2 parents 338f31a + ec051b0 commit 7003ee7
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/Context/AdminContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use EasyCorp\Bundle\EasyAdminBundle\Config\Option\EA;
use EasyCorp\Bundle\EasyAdminBundle\Config\UserMenu;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Controller\DashboardControllerInterface;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Factory\MenuFactoryInterface;
use EasyCorp\Bundle\EasyAdminBundle\Dto\AssetsDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\CrudDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\DashboardDto;
Expand All @@ -14,7 +15,6 @@
use EasyCorp\Bundle\EasyAdminBundle\Dto\MainMenuDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\SearchDto;
use EasyCorp\Bundle\EasyAdminBundle\Dto\UserMenuDto;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Factory\MenuFactoryInterface;
use EasyCorp\Bundle\EasyAdminBundle\Registry\CrudControllerRegistry;
use EasyCorp\Bundle\EasyAdminBundle\Registry\TemplateRegistry;
use Symfony\Component\HttpFoundation\Request;
Expand Down
2 changes: 1 addition & 1 deletion src/Factory/AdminContextFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ final class AdminContextFactory
{
private string $cacheDir;
private ?TokenStorageInterface $tokenStorage;
private MenuFactory $menuFactory;
private MenuFactoryInterface $menuFactory;
private CrudControllerRegistry $crudControllers;
private EntityFactory $entityFactory;

Expand Down
2 changes: 1 addition & 1 deletion src/Field/Configurator/CommonPostConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ private function buildFormattedValueOption($value, FieldDto $field, EntityDto $e
return $value;
}

$formatted = $callable($value, $entityDto->getInstance());
$formatted = $callable($field->getValue(), $entityDto->getInstance());

// if the callable returns a string, wrap it in a Twig Markup to render the
// HTML and CSS/JS elements that it might contain
Expand Down
40 changes: 40 additions & 0 deletions tests/Controller/FormFieldValueControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace EasyCorp\Bundle\EasyAdminBundle\Tests\Controller;

use Doctrine\ORM\EntityRepository;
use EasyCorp\Bundle\EasyAdminBundle\Test\AbstractCrudTestCase;
use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Controller\DashboardController;
use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Controller\FormFieldValueController;
use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Entity\BlogPost;

class FormFieldValueControllerTest extends AbstractCrudTestCase
{
protected EntityRepository $blogPosts;

protected function setUp(): void
{
parent::setUp();
$this->client->followRedirects();

$this->blogPosts = $this->entityManager->getRepository(BlogPost::class);
}

protected function getControllerFqcn(): string
{
return FormFieldValueController::class;
}

protected function getDashboardFqcn(): string
{
return DashboardController::class;
}

public function testFieldsFormatValue()
{
$this->client->request('GET', $this->generateIndexUrl());

static::assertSelectorTextSame('td[data-column="title"]', 'Blog Post 0');
static::assertSelectorTextSame('td[data-column="createdAt"]', '20201101090000');
}
}
28 changes: 28 additions & 0 deletions tests/TestApplication/src/Controller/FormFieldValueController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Controller;

use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Entity\BlogPost;

class FormFieldValueController extends AbstractCrudController
{
public static function getEntityFqcn(): string
{
return BlogPost::class;
}

public function configureFields(string $pageName): iterable
{
// these fields format the original value with some options (e.g. max length)
// and then use the formatValue() method to test that this method receives the
// original field value, not the one modified with the other options
return [
TextField::new('title')->setMaxLength(2)->formatValue(fn ($value, $entity) => $value),
DateTimeField::new('createdAt')->setFormat('long', 'long')
->formatValue(fn (/** @var \DateTimeInterface $value */ $value, $entity) => date('YmdHis', $value->getTimestamp())),
];
}
}

0 comments on commit 7003ee7

Please sign in to comment.