-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] set unique block prefix with any namespace #17874
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
Conversation
👍 I've just noticed similar error in my own application. The problem seems to occur when a prefix is repeated in the block_prefixes array. The exception is thrown too early - in the example from @andrey1s the block form_row certainly exists even though the exception says otherwise. |
Hi, thanks for reporting this problem :) How can it happen that a block name is repeated in the chain? Can you provide code that reproduces your problem? |
for example //src/AppBundle/Controller/IndexController.php
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Form\Type\Other\SimpleType;
class IndexController extends Controller
{
/**
* @Route("/", name="homepage")
*/
public function homepageAction(Request $request)
{
$form = $this
->createFormBuilder()
->add('simple', SimpleType::class)
->getForm()
;
return $this->render('@App/Index/homepage.html.twig', ['form' => $form->createView()]);
}
} forms <?php
//src/AppBundle/Form/Type/Other/SimpleType.php
namespace AppBundle\Form\Type\Other;
use Symfony\Component\Form\AbstractType;
use AppBundle\Form\Type\SimpleType as BaseType;
class SimpleType extends AbstractType
{
public function getParent()
{
return BaseType::class;
}
} <?php
//src/AppBundle/Form/Type/SimpleType.php
namespace AppBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
class SimpleType extends AbstractType
{
public function getParent()
{
return TextType::class;
}
} when there are two blocks with the same name is an error. <?php
//src/AppBundle/Form/Type/Other/SimpleType.php
namespace AppBundle\Form\Type\Other;
use Symfony\Component\Form\AbstractType;
use AppBundle\Form\Type\SimpleType as BaseType;
class SimpleType extends AbstractType
{
public function getParent()
{
return BaseType::class;
}
public function getBlockPrefix()
{
return 'text';
}
} |
@@ -171,7 +171,7 @@ private function loadResourceForBlockNameHierarchy($cacheKey, FormView $view, ar | ||
// The next two if statements contain slightly duplicated code. This is by intention | ||
// and tries to avoid execution of unnecessary checks in order to increase performance. | ||
|
||
if (isset($this->resources[$cacheKey][$parentBlockName])) { | ||
if (isset($this->resources[$cacheKey][$parentBlockName]) && $this->resources[$cacheKey][$parentBlockName] !== false) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change is missing a corresponding test.
Also you are reducing the impact of a performance optimization here. Could you benchmark the result for big forms?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i can't find any test for the Symfony\Component\Form\AbstractRendererEngine
or Symfony\Component\Form\Extension\Templating\TemplatingRendererEngine
, could you show where are they located?
could you show the current benchmark?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be the yoda style like:
false !== $this->resources[$cacheKey][$parentBlockName]
Thanks for the example, I understand your use case now. This conveys a bigger problem IMO. In a template, a block (e.g.
We should consider to recommend returning unique values from |
@webmozart In that case we should modify the exception - if there is a duplicate in the array throw an exception about that instead of what is thrown currently. Probably only when a problem like this occurs to avoid unnecessary checking for non-unique prefixes for every field. |
@webmozart if we need to use a unique template, then Yes, otherwise it's not necessary. or |
IIUC, this change should not be merged but a new exception should be added, is that correct? How do we moved forward? |
@enumag Can you update this PR with the commit you mentioned above? Tests can be added later or by someone else if needed. |
@fabpot I'm not the author of this PR so I'll have to create a new one instead. Wait a moment... |
Does that mean we should close here in favour of #19127? |
Closing in favor of #19127 |
…k names (enumag) This PR was merged into the 2.7 branch. Discussion ---------- [Form] Add exception to FormRenderer about non-unique block names | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #17874 | License | MIT | Doc PR | Commits ------- c6db6f3 [Form] Add exception to FormRenderer about non-unique block names
if there is no block
hidden_locale_row
when error