在我從我的 ChoiceType 中選擇一個值后,我正在嘗試向我的表單添加一個新的 TextType 欄位。我不知道該怎么做。我嘗試了一點,但它無法按我想要的方式作業。
拋出的例外是:
型別屬性 App\Model\Software::$productType 在初始化之前不能被訪問
final class ProductType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('productType', ChoiceType::class, [
'choices' => [
'Software' => 'software',
'Television' => 'television',
'Giftcard' => 'giftcard',
'Bitte w?hlen' => '',
],
])
->add('productNumber', TextType::class)
->add('title', TextType::class)
->add('submit', SubmitType::class);
$builder->addEventListener(
FormEvents::POST_SET_DATA,
function (FormEvent $event)
{
$form = $event->getForm();
$data = $event->getData();
if ($data->productType === 'giftcard') {
$form->add('value', TextType::class);
}
}
);
}
}
我已經用不同的 FormEvents 試過了。
uj5u.com熱心網友回復:
您有“型別化屬性 App\Model\Software::$productType 不能在初始化之前訪問”的錯誤是因為在您的物體 'App\Model\Software' 中,屬性 'productType' 的型別為“string”或其他型別未初始化為 null 或空字串或 ...,并且在您的偵聽器中您正試圖訪問該屬性 ($data->productType)
嘗試將您的屬性初始化為 null 或空字串
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/331945.html
