Validate template option for grid fields - #364
Conversation
| /** | ||
| * @test | ||
| */ |
There was a problem hiding this comment.
| /** | |
| * @test | |
| */ | |
| /** @test */ |
There was a problem hiding this comment.
You can also use the PHP Attribute :)
| /** | ||
| * @test | ||
| */ | ||
| public function it_throws_exception_when_field_template_is_used_with_non_twig_type(): void |
There was a problem hiding this comment.
| 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 |
| ->end() | ||
| ->end() | ||
| ->validate() | ||
| ->ifTrue(fn (array $config): bool => isset($config['options']['template']) && $config['type'] !== 'twig') |
There was a problem hiding this comment.
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.
|
I think we'll not have this issue anymore with PHP grids, we are able to remove the field and re-add it! |
|
@TheMilek That'd be cool to test it with mutators, could you try it? |
|
Ok, I've tried with the mutators, we still have the issue with YAML grids cause the config is still merged. 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. |
Initial error:

The error message did not clearly indicate what was wrong.
The issue was related to grid configuration merging.
In Sylius, we have:
and in some end app:
After merging, the type was set to
string, but thetemplateoption 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.