gpt4 book ai didi

php - 如何在 Symfony 表单字段上使用 setAttribute

转载 作者:可可西里 更新时间:2023-11-01 13:52:26 25 4
gpt4 key购买 nike

我正在尝试将 stripe-data 属性添加到使用 createBuilder 生成的字段。

这是我正在使用的代码:

$form = $this->formFactory->createBuilder(FormType::class, [
'action' => $this->router->generate('storeSubscription', ['id' => $store->getId()]),
'method' => 'POST',
])
->add('plan', PremiumType::class, [
'data_class' => PremiumFeaturesEmbeddable::class,
'data' => $store->getPremium(),
]);

if (null === $store->getBranchOf()->getStripeCustomer() || null === $store->getBranchOf()->getStripeCustomer()->getDefaultSource()) {
$form->add('credit_card', CreditCardStripeTokenType::class)
->add('company_data', CompanyType::class, [
'data_class' => Company::class,
'data' => $store->getBranchOf()
]);

// Remove unused data
$form->get('company_data')
->remove('brand')
->remove('primaryEmail')
->remove('description')
->remove('phoneNumber')
->remove('faxNumber')
->remove('save');

// Set data-stripe
$form->get('company_data')
->get('legalName')->setAttributes(['attr' => ['data-stripe', 'name']]);
}

如您所见,此代码的最后一行首先获取 copmany_data 表单类型,然后从中获取字段 legalName:在此我想设置属性 stripe-data="name"

但是这段代码不起作用。

要添加属性,我必须在 Twig 中使用 form_widget:

<div class="form-group">
{{ form_errors(form.company_data.legalName) }}
{{ form_label(form.company_data.legalName) }}
{{ form_widget(form.company_data.legalName, {'attr': {'class': 'form-control input-sm', 'data-stripe': 'name'}}) }}
</div>

这样就可以正常工作并正确添加属性。当然,我可能会继续在 Twig 中设置这些属性,但我想在 PHP 中添加它们,但不明白为什么它不起作用。有人能解释我如何解决这个问题吗?谢谢!

我尝试过的

测试 1

// Set data-stripe
$form->get('company_data')
->get('legalName')->setAttributes(['data-stripe', 'name']);

测试 2

// Set data-stripe
$form->get('company_data')
->get('legalName')->setAttribute('data-stripe', 'name');

测试 3

// Set data-stripe
$form->get('company_data')
->get('legalName')->setAttributes(['attr' => ['data-stripe', 'name']]);

这些都行不通。我不知道该多尝试什么。

注意:我打开了一个 issue on GitHub也是。

最佳答案

如果您需要添加/修改任何现有字段中的任何选项,只需执行以下操作:

// to keep the current options
$options = $form->get('company_data')->get('legalName')->getConfig()->getOptions();

// add/change the options here
$options['attr']['data-stripe'] = 'name';

// re-define the field options
$form->get('company_data')->add('legalName', TextType::class, $options);

这不会改变字段的顺序,只会改变它们的选项。对于 buildForm() 和监听器/订阅者事件的条件选项很有用。


但是,如果您改变方法,则可以通过其他方式实现。

首先,向CreditCardStripeTokenType 表单类型添加一个新的默认选项'add_stripe_name' => null,并将此值传递给'legalName' 字段选项定义:

// into CreditCardStripeTokenType::buildForm():

$legalNameOptions = array();

if ($options['add_stripe_name']) {
$legalNameOptions['attr']['data-stripe'] = $options['add_stripe_name'];
}

$builder->add('legalName', TextType::class, $legalNameOptions)

因此 CreditCardStripeTokenType'add_stripe_name' 选项将是将属性添加到 legalName 字段的标志:

$builder->add('credit_card', CreditCardStripeTokenType::class, [
'add_stripe_name' => 'name', // <---- \o/
]);

关于php - 如何在 Symfony 表单字段上使用 setAttribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40267844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com