Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error during translation extraction: "Form choice is not an array" and "Choice label is not a scalar string" #179

Open
ovsep404 opened this issue Feb 6, 2025 · 0 comments

Comments

@ovsep404
Copy link

ovsep404 commented Feb 6, 2025

Description

When running the translation extractor (php bin/console translation:extract), I encounter the following errors:

  • Form choice is not an array
  • Choice label is not a scalar string

Image

These errors occur in my form types where dynamic choices are generated via function calls. For example, in my ProductType form I use:

->add('vendor', EntityType::class, [
    'class' => Vendor::class,
    'choices' => $this->vendorRepository->findBy($vendorFilters),
    'required' => false,
])

And in another form, I have:

$builder->add('state', ChoiceType::class, [
    'choices' => $this->enumChoicesWithAll(EquipmentState::class),
    'data' => AbstractRepository::FILTER_ALL_ID,
]);

Problem

The issue arises because:

  • findBy() returns an array of entities (T[]), but the translation extractor expects a static array literal (an instance of Node\Expr\Array_).
  • I think the extractor can only analyze static arrays defined directly in the code. It cannot process arrays created dynamically at runtime via method calls like findBy() or helper functions like enumChoicesWithAll().
  • When the extractor encounters these dynamic calls, it sees a Node\Expr\MethodCall or Node\Expr\FuncCall instead of a static array, which triggers the errors:
    • Form choice is not an array: The extractor cannot process the dynamically generated choices.
    • Choice label is not a scalar string: In similar dynamic cases, the extractor is unable to determine the label because it isn’t provided as a static scalar string.

The error is triggered in the following block of code in the translation extractor: FormTypeChoices.php:

} elseif (!$choices instanceof Node\Expr\Array_) {
    $this->addError($choices, 'Form choice is not an array');
    continue;
}

Here, the result of findBy() is treated as a method call (Node\Expr\MethodCall), not as a static array node, which leads to the error.

  • Form choice is not an array: The extractor cannot process the dynamically generated choices.
  • Choice label is not a scalar string: In similar dynamic cases, the extractor is unable to determine the label because it isn’t provided as a static scalar string.

Question

I am wondering if there is an alternative approach that would allow the translation extractor to handle dynamic choices without requiring the use of multiple @Ignore annotations. Has anyone encountered a similar issue or found a solution that would let the extractor process dynamically generated arrays (or provide a configurable fallback during extraction)?

Any guidance, suggestions, or workarounds would be greatly appreciated.

Thank you for your time and support!


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant