我想从类“ AffaireType”向我的类“ PointageType”传递一些自定义选项,例如当前用户的ID,以在类“ PointageType”中创建查询。
我试图在AffaireType的buildForm()函数的'entry_options'中传递用户的ID,但这显示了一个错误:
选项“ id”不存在。定义的选项为:“操作”,“ 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_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” ”,“ property_path”,“必填”,“ translation_domain”,“修剪”,“ upload_max_size_message”,“ validation_groups”。
我的AffaireType类:
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,
]);
}
}
我的PointageType类:
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做到这一点?
答案 0 :(得分:0)
要将选项添加到表单类型,请使用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