2 回答

TA貢獻(xiàn)1795條經(jīng)驗(yàn) 獲得超7個(gè)贊
以下內(nèi)容適用于 Symfony 5.0.x(盡管 3.4 不需要進(jìn)行重大更改,可能只需要一些額外的配置):
要抽象主題處理,請(qǐng)定義您自己的復(fù)選框類型,如下(例如):
<?php
declare(strict_types=1);
namespace App\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
class ThemedCheckboxType extends AbstractType
{
? ? /**
? ? ?* @param OptionsResolver $resolver
? ? ?*/
? ? public function configureOptions(OptionsResolver $resolver): void
? ? {
? ? ? ? $resolver
? ? ? ? ? ? ->setDefaults([
? ? ? ? ? ? ? ? 'theme' => 'default',
? ? ? ? ? ? ? ? 'block_prefix' => static function (Options $options): ?string {
? ? ? ? ? ? ? ? ? ? return [
? ? ? ? ? ? ? ? ? ? ? ? 'default' => null,
? ? ? ? ? ? ? ? ? ? ? ? 'theme-a' => 'theme_a_checkbox',
? ? ? ? ? ? ? ? ? ? ? ? 'theme-b' => 'theme_b_checkbox',
? ? ? ? ? ? ? ? ? ? ][$options['theme']];
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ])
? ? ? ? ? ? ->setAllowedValues('theme', ['default', 'theme-a', 'theme-b']);
? ? }
? ? /**
? ? ?* @inheritDoc
? ? ?*/
? ? public function getParent(): string
? ? {
? ? ? ? return CheckboxType::class;
? ? }
}
然后在需要的地方簡單地使用它,例如:
$builder->add('test_a1', ThemedCheckboxType::class, [
? ? 'theme' => 'theme-a', // or others
]);
這樣,您可以注入自己的表單主題,在需要時(shí)直接操作復(fù)選框,例如
{% block theme_a_checkbox_widget %}
? ? <em>Theme-A</em>
? ? {{ block('checkbox_widget') }}
{% endblock %}
{% block theme_b_checkbox_widget %}
? ? <em>Theme-B</em>
? ? {{ block('checkbox_widget') }}
{% endblock %}
如果您不想使用額外的表單類型,可以使用自定義表單擴(kuò)展來為默認(rèn)復(fù)選框類型添加選項(xiàng)來實(shí)現(xiàn)同樣的效果。請(qǐng)參閱: https: //symfony.com/doc/current/form/create_form_type_extension.html
例如:
<?php
declare(strict_types=1);
namespace App\Form\Extension;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
class CheckboxTypeExtension extends AbstractTypeExtension
{
? ? /**
? ? ?* @inheritDoc
? ? ?*/
? ? public static function getExtendedTypes(): iterable
? ? {
? ? ? ? return [CheckboxType::class];
? ? }
? ? /**
? ? ?* @inheritDoc
? ? ?*/
? ? public function configureOptions(OptionsResolver $resolver): void
? ? {
? ? ? ? $resolver
? ? ? ? ? ? ->setDefaults([
? ? ? ? ? ? ? ? 'theme' => 'default',
? ? ? ? ? ? ? ? 'block_prefix' => static function (Options $options): ?string {
? ? ? ? ? ? ? ? ? ? return [
? ? ? ? ? ? ? ? ? ? ? ? 'default' => null,
? ? ? ? ? ? ? ? ? ? ? ? 'theme-a' => 'theme_a_checkbox',
? ? ? ? ? ? ? ? ? ? ? ? 'theme-b' => 'theme_b_checkbox',
? ? ? ? ? ? ? ? ? ? ][$options['theme']];
? ? ? ? ? ? ? ? },
? ? ? ? ? ? ])
? ? ? ? ? ? ->setAllowedValues('theme', ['default', 'theme-a', 'theme-b']);
? ? }
}
這允許:
$builder->add('test_a1', CheckboxType::class, [
? ? 'theme' => 'theme-a',
]);

TA貢獻(xiàn)1853條經(jīng)驗(yàn) 獲得超9個(gè)贊
您可以將主題應(yīng)用于單個(gè)復(fù)選框:
{%?form_theme?form.my_special_checkbox?'Form/Theme/special_checkbox.html.twig'?%}
或者,如果您不需要單獨(dú)的主題(單個(gè)用例),您可以在當(dāng)前 Twig 文件中編寫塊:
{%?block?_my_checkbox_widget_block?%} ????{#?the?theme?#} ????{%?endblock?%} {%?form_theme?form.my_special_checkbox?_self?%}
我會(huì)使用自定義塊名稱創(chuàng)建自定義復(fù)選框組件并將其放入全局表單主題中,以便我可以在整個(gè)應(yīng)用程序中重用它。
- 2 回答
- 0 關(guān)注
- 151 瀏覽
添加回答
舉報(bào)