gpt4 book ai didi

forms - 表单提交后,子表单值在接收端显示为空

转载 作者:行者123 更新时间:2023-12-04 15:50:11 26 4
gpt4 key购买 nike

我已经构建了一个提交正常的表单,但是当我在 Controller 中查看它们时,子表单值在接收端最终都为空。

这是我的 UserProfileType表格,基于 User类(class)。具体来说,我们正在查看的子表单是 subscriptionTier1 , subscriptionTier1 , 和 subscriptionTier1 :

class UserProfileType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('firstName', TextType::class)
->add('lastName', TextType::class)
->add('email', EmailType::class)

// etc... I'll keep out the unimportant fields

// here are the subforms whose values show up as null on the back end
->add('subscriptionTier1', UserSubscriptionTierType::class, [
'required' => false,
'mapped' => false
])
->add('subscriptionTier2', UserSubscriptionTierType::class, [
'required' => false,
'mapped' => false
])
->add('subscriptionTier3', UserSubscriptionTierType::class, [
'required' => false,
'mapped' => false
])
->add('save', SubmitType::class, [
'attr' => ['class' => 'save'],
])
;

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => User::class,
'mode' => null
)
);
}

}

这是我的 UserSubscriptionTierType表单类型类看起来像:
class UserSubscriptionTierType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class, [
'attr' => [
'maxlength' => 25
]
])
->add('price', ChoiceType::class, [
'choices' => [
'$10 per month' => 10,
'$20 per month' => 20,
'$30 per month' => 30
]
])
->add('description', TextareaType::class)
->add('messaging', CheckboxType::class, [
'required' => false
])
;
}

public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => UserSubscriptionTier::class,
));
}
}

如下图所示,子表单subscriptionTier1的name、price、description值都是在前端设置的:

enter image description here

这是 subscriptionTier1 的 Twig 代码的样子子表格:
    <div class="row justify-content-center w-100 ml-0 mr-0 mt-3 tier tier-1" >
<div class="col-lg-6 mt-1">
<div class="form-group form-control-lg w-100">
<label for="firstname" class="mb-2">Tier 1 Name</label>
{{ form_widget(form.subscriptionTier1.name, { 'attr': {'class': 'form-control'}}) }}
</div>
</div>
<div class="col-lg-6 mt-1">
<div class="form-group form-control-lg w-100">
<label for="tier1price" class="mb-2">Tier 1 Price</label>
{{ form_widget(form.subscriptionTier1.price, { 'attr': {'class': 'form-control'}}) }}
</div>
</div>
<div class="col-lg-12 mt-3">
<div class="form-group form-control-lg w-100">
<label for="bio" class="mb-2">Tier 1 Description
<sup class="text-danger">*</sup>
</label>
{{ form_widget(form.subscriptionTier1.description, { 'attr': {'class': 'form-control border-box', 'rows':'8'}}) }}
</div>
</div>
<div class="col-lg-12">
<div class="form-group form-control-lg w-100">
<div class="form-check">
<label class="form-check-label">
{{ form_widget(form.subscriptionTier1.messaging, { 'attr': {'class': 'form-control form-check-input'}}) }}
<span class="form-check-sign"></span>
Enable Messaging
</label>
</div>
</div>
</div>
</div>

这是接收端的代码, Controller :
public function saveProfileAction(Request $request)
{
$user = $this->getUser();

$form = $this->createForm(UserProfileType::class, $user);
$form->handleRequest($request);

dump($form->get('subscriptionTier1')->getData());

所以在调试中,如果我只转储第一个表单 subscriptionTier1 ,可以看到值都是 null .
ProfileController.php on line 269:
Form {#2235 ▼
-config: FormBuilder {#2236 ▶}
-parent: Form {#2112 ▶}
-children: OrderedHashMap {#2237 ▶}
-errors: []
-submitted: true
-clickedButton: null
-modelData: UserSubscriptionTier {#2280 ▼
-id: null
-posts: ArrayCollection {#2323 ▶}
-subscriptions: null
-name: null // Don't understand why this is null
-price: null // Don't understand why this is null
-description: null // Don't understand why this is null
-tierNumber: null
-versionNumber: null
-messaging: false
-user: null
+"subsciptions": ArrayCollection {#2089 ▶}
}
-normData: UserSubscriptionTier {#2280 ▶}
-viewData: UserSubscriptionTier {#2280 ▶}
-extraData: []
-transformationFailure: null
-defaultDataSet: true
-lockSetData: false
}

有谁知道为什么这些值没有传递到后端(或被清零)?

最佳答案

data_class UserProfileType 的选项表单类型设置为 User类,模型数据将是 User 的一个实例类和自 User类没有像 subscriptionTier1 这样的字段等,这些不会出现在您的模型数据中。

相反,您可以像这样在 Controller 中的表单中访问未映射的字段:

$subscriptionTier1 = $form->get('subscriptionTier1')->getData();

文档 here

编辑:您只能访问subscriptionTier1等的值,只有在表单被处理后并且只有表单被提交时,否则它将为空:
public function saveProfileAction(Request $request)
{
$user = $this->getUser();

$form = $this->createForm(UserProfileType::class, $user);
$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
$subscriptionTier1 = $form->get('subscriptionTier1')->getData();
dump($subscriptionTier1);
}

关于forms - 表单提交后,子表单值在接收端显示为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54623492/

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