表单 - 如何将自定义选项传递给 Symfony 4 中的 CollectionType 子项
Forms - How to pass custom options to CollectionType child in Symfony 4
我想从 class "AffaireType" 向我的 class "PointageType" 传递一些自定义选项,例如当前用户的 ID 以创建查询我的 class "PointageType"。
我试图在 AffaireType 的 buildForm() 函数的 'entry_options' 中传递我的用户 ID,但它向我显示错误:
The option "id" does not exist. Defined options are: "action", "allow_extra_fields", "allow_file_upload", "attr", "attr_translation_parameters", "auto_initialize", "block_name", "block_prefix", "by_reference", "compound", "constraints", "csrf_field_name", "csrf_message", "csrf_protection", "csrf_token_id", "csrf_token_manager", "data", "data_class", "disabled", "empty_data", "error_bubbling", "error_mapping", "extra_fields_message", "help", "help_attr", "help_html", "help_translation_parameters", "inherit_data", "invalid_message", "invalid_message_parameters", "label", "label_attr", "label_format", "label_translation_parameters", "mapped", "method", "post_max_size_message", "property_path", "required", "translation_domain", "trim", "upload_max_size_message", "validation_groups".
我的外遇类型class:
class AffaireType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('numAffaire');
$builder->add('descAffaire');
$builder->add('pointages', CollectionType::class, [
'entry_type' => PointageType::class,
'entry_options' => array(
'id' => 8,
),
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Affaires::class,
]);
}
}
我的点数类型class:
class PointageType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('heurePointage');
//I want to do the query here
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Pointage::class,
]);
}
}
我认为此代码在 Symfony 2/3 中有效,但我如何在 Symfony 4.3 中执行此操作?
要向表单类型添加选项,请使用 configureOptions
中的 OptionsResolver:
$resolver->setRequired(['id']); // for required options, with no default
// OR
$resolver->setDefaults(['id' => null]); // for optional options, that can be set
在您的情况下,必须在 PointageType::configureOptions
中设置
我想从 class "AffaireType" 向我的 class "PointageType" 传递一些自定义选项,例如当前用户的 ID 以创建查询我的 class "PointageType"。
我试图在 AffaireType 的 buildForm() 函数的 'entry_options' 中传递我的用户 ID,但它向我显示错误:
The option "id" does not exist. Defined options are: "action", "allow_extra_fields", "allow_file_upload", "attr", "attr_translation_parameters", "auto_initialize", "block_name", "block_prefix", "by_reference", "compound", "constraints", "csrf_field_name", "csrf_message", "csrf_protection", "csrf_token_id", "csrf_token_manager", "data", "data_class", "disabled", "empty_data", "error_bubbling", "error_mapping", "extra_fields_message", "help", "help_attr", "help_html", "help_translation_parameters", "inherit_data", "invalid_message", "invalid_message_parameters", "label", "label_attr", "label_format", "label_translation_parameters", "mapped", "method", "post_max_size_message", "property_path", "required", "translation_domain", "trim", "upload_max_size_message", "validation_groups".
我的外遇类型class:
class AffaireType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('numAffaire');
$builder->add('descAffaire');
$builder->add('pointages', CollectionType::class, [
'entry_type' => PointageType::class,
'entry_options' => array(
'id' => 8,
),
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Affaires::class,
]);
}
}
我的点数类型class:
class PointageType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('heurePointage');
//I want to do the query here
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Pointage::class,
]);
}
}
我认为此代码在 Symfony 2/3 中有效,但我如何在 Symfony 4.3 中执行此操作?
要向表单类型添加选项,请使用 configureOptions
中的 OptionsResolver:
$resolver->setRequired(['id']); // for required options, with no default
// OR
$resolver->setDefaults(['id' => null]); // for optional options, that can be set
在您的情况下,必须在 PointageType::configureOptions