Symfony 3.4 形式:将变量从一种类型传递到另一种类型
Symfony 3.4 forms: Pass variables from one type to another
我有两种类型。一个是产品类型。第二个是 ProductFeaturesType。
ProductFeaturesType 是 ProductType 中的 CollectionType。
我想将我的可变产品从 ProductType 传递到 ProductFeaturesType。
EDITED
我的 objective 是根据我的一个 属性 产品在我的 ProductFeaturesType 中做一个条件。
我尝试了以下方法:
产品类型
$builder
->add('productFeatures', CollectionType::class, array(
'entry_type' => ProductFeaturesType::class,
'allow_add' => true,
'allow_delete' => true,
'product' => $product
))
;
没用。我收到以下错误消息:
The option "product" do not exist. Defined options are: "action", "allow_add", "allow_delete", "allow_extra_fields", "attr", "auto_initialize", "block_name", "by_reference", "compound", "constraints", "csrf_field_name", "csrf_message", "csrf_protection", "csrf_token_id", "csrf_token_manager", "data", "data_class", "delete_empty", "disabled", "documentation", "empty_data", "entry_options", "entry_type", "error_bubbling", "error_mapping", "extra_fields_message", "inherit_data", "invalid_message", "invalid_message_parameters", "label", "label_attr", "label_format", "mapped", "method", "post_max_size_message", "property_path", "prototype", "prototype_data", "prototype_name", "required", "translation_domain", "trim", "upload_max_size_message", "validation_groups".
你能告诉我哪里出了问题吗?
异常告诉您代码中有什么问题,选项产品不是表单构建器声明中定义的选项。如果您的意图是 save/update 具有产品功能的产品,那么您已经做到了,因为您拥有映射信息; MTO 或 MTM;在您的实体中,并在表单生成器中定义了 collectionType。当您提交表单时,symfony 将在每个功能实体中设置 "pass" 产品实体。
只需删除产品选项并尝试提交表单。
希望对您有所帮助!
EDITED
您只需在您的类型中添加一个form event listener
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$event->getData(); // here you have all the data to be set which match with the data_class option of your type
$event->getForm(); //here you have the form and you can manipulate it as you wish.
});
我有两种类型。一个是产品类型。第二个是 ProductFeaturesType。
ProductFeaturesType 是 ProductType 中的 CollectionType。
我想将我的可变产品从 ProductType 传递到 ProductFeaturesType。
EDITED
我的 objective 是根据我的一个 属性 产品在我的 ProductFeaturesType 中做一个条件。
我尝试了以下方法:
产品类型
$builder
->add('productFeatures', CollectionType::class, array(
'entry_type' => ProductFeaturesType::class,
'allow_add' => true,
'allow_delete' => true,
'product' => $product
))
;
没用。我收到以下错误消息:
The option "product" do not exist. Defined options are: "action", "allow_add", "allow_delete", "allow_extra_fields", "attr", "auto_initialize", "block_name", "by_reference", "compound", "constraints", "csrf_field_name", "csrf_message", "csrf_protection", "csrf_token_id", "csrf_token_manager", "data", "data_class", "delete_empty", "disabled", "documentation", "empty_data", "entry_options", "entry_type", "error_bubbling", "error_mapping", "extra_fields_message", "inherit_data", "invalid_message", "invalid_message_parameters", "label", "label_attr", "label_format", "mapped", "method", "post_max_size_message", "property_path", "prototype", "prototype_data", "prototype_name", "required", "translation_domain", "trim", "upload_max_size_message", "validation_groups".
你能告诉我哪里出了问题吗?
异常告诉您代码中有什么问题,选项产品不是表单构建器声明中定义的选项。如果您的意图是 save/update 具有产品功能的产品,那么您已经做到了,因为您拥有映射信息; MTO 或 MTM;在您的实体中,并在表单生成器中定义了 collectionType。当您提交表单时,symfony 将在每个功能实体中设置 "pass" 产品实体。 只需删除产品选项并尝试提交表单。
希望对您有所帮助!
EDITED
您只需在您的类型中添加一个form event listener
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$event->getData(); // here you have all the data to be set which match with the data_class option of your type
$event->getForm(); //here you have the form and you can manipulate it as you wish.
});