- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试将 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/
我尝试通过正则表达式将文本中的单引号更改为双引号。 (单字)示例:我走了。 You gona fly to planet 'Ziqtos' => 我需要在 I'm 中保留单引号,并在 You gona
我正在构建一个 API,其中大部分将包含 JSON 和 HTML 内容。但是一些非常具体的端点只呈现 true 或 false,并且还在 POST 中接受 true 或 false。这是请求或响应的整
我是一名优秀的程序员,十分优秀!