Skip to content

Validate template option for grid fields - #364

Open
Rafikooo wants to merge 1 commit into
Sylius:1.13from
Rafikooo:1.13-validate-field-type
Open

Validate template option for grid fields#364
Rafikooo wants to merge 1 commit into
Sylius:1.13from
Rafikooo:1.13-validate-field-type

Conversation

@Rafikooo

Copy link
Copy Markdown
Contributor

Initial error:
image

The error message did not clearly indicate what was wrong.

The issue was related to grid configuration merging.

In Sylius, we have:

sylius_grid:
    grids:
        fields:
            email:
                type: twig
                label: sylius.ui.email
                sortable: ~
                options:
                    template: "@SyliusAdmin/shared/grid/field/name.html.twig"

and in some end app:

sylius_grid:
    grids:
        fields:
            email:
                type: string
                label: sylius.ui.email
                sortable: ~
                position: 4

After merging, the type was set to string, but the template option from the base configuration remained, causing the application to break.

This PR introduces validation to prevent similar issues in the future, saving users from hours of debugging.

Comment on lines +301 to +303
/**
* @test
*/

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/**
* @test
*/
/** @test */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also use the PHP Attribute :)

/**
* @test
*/
public function it_throws_exception_when_field_template_is_used_with_non_twig_type(): void

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function it_throws_exception_when_field_template_is_used_with_non_twig_type(): void
public function it_throws_an_exception_when_field_template_is_used_with_non_twig_type(): void

@GSadee GSadee added Enhancement Minor issues and PRs improving the current solutions (optimizations, typo fixes, etc.). DX Issues and PRs aimed at improving Developer eXperience. labels Feb 14, 2025
->end()
->end()
->validate()
->ifTrue(fn (array $config): bool => isset($config['options']['template']) && $config['type'] !== 'twig')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like an overreach that can plausibly cause conflicts with custom types.
Wouldn't it be better to reset the previous options once the type changes, we'd probably need to inject the logic somewhere inside the config merging mechanism though.

@loic425

loic425 commented Jul 9, 2026

Copy link
Copy Markdown
Member

I think we'll not have this issue anymore with PHP grids, we are able to remove the field and re-add it!

@loic425

loic425 commented Jul 9, 2026

Copy link
Copy Markdown
Member

@TheMilek That'd be cool to test it with mutators, could you try it?

@loic425

loic425 commented Jul 10, 2026

Copy link
Copy Markdown
Member

Ok, I've tried with the mutators, we still have the issue with YAML grids cause the config is still merged.
The work around:

public function __invoke(GridBuilderInterface $gridBuilder): void
{
    $gridBuilder->removeField('email')
        ->addField(StringField::create('email_string')
            ->setLabel('sylius.ui.email')
            ->setPath('email')
        )
    ;
}

With new Grids on Sylius 2.3 it works but we cannot use removeField + addField, cause the Removal is applied later with a service. But we'll be able to remove that service after removing YAML grids in Sylius GridBundle 2.0.

It works with this following code (without the removeField):

public function __invoke(GridBuilderInterface $gridBuilder): void
{
    $gridBuilder->addField(StringField::create('email'));
}

I'm also wondering if the removal service could be a default mutator, then we'll be able to handle the issue with the mutator priorities and placing a decorator after the removal.
Or maybe even simpler, we can add a flag to remove that removal service. Indeed, with new invokable grids, we inject the current grid builder into the mutators.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

DX Issues and PRs aimed at improving Developer eXperience. Enhancement Minor issues and PRs improving the current solutions (optimizations, typo fixes, etc.).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants