交响乐。如何建立表格?
Symfony. How to build the form?
如何实现接受以下 JSON-Object 的表单?
我没有定义模型,因为我不需要它们。数据已发送
{
"type_of_error": "logic error",
"severity": "normal",
"what_did_you_do": "something",
"what_happened": "blue screen",
"which_result": "full satisfaction",
"requests": [
{
"url": "/api/v1/agent/statistic"
}
]
}
我已经开始建表了:
class ErrorReportFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('type_of_error', TextType::class, [
'empty_data' => '',
'constraints' => [
new NotBlank()
]
])
->add('severity', TextType::class, [
'empty_data' => '',
'constraints' => [
new NotBlank()
]
])
->add('what_did_you_do', TextType::class, [
'empty_data' => '',
'constraints' => [
new NotBlank()
]
])
->add('what_happened', TextType::class, [
'empty_data' => '',
'constraints' => [
new NotBlank(),
]
])
->add('which_result', TextType::class, [
'empty_data' => '',
'constraints' => [
new NotBlank(),
]
])
->add('requests', CollectionType::class, [
'entry_type' => RequestsFormType::class,
])
;
}
并且我已经这样定义了 RequestsFormType。这里有什么问题?看起来好像 RequestsFormType 不会被接受。
class RequestsFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('url', TextType::class, [
'empty_data' => '',
'constraints' => [
new NotBlank()
]
]);
}
public function getBlockPrefix()
{
return '';
}
}
也许尝试使用 CollectionType。
看这里:https://symfony.com/doc/current/reference/forms/types/collection.html
$builder->add('requests', CollectionType:class, [
'entry_type' => YourCustomType::class,
]);
您的自定义类型:
class YourCustomType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('url', TextType::class, []);
$builder->add('method', TextType::class, []);
$builder->add('timestamp', DateTimeType::class, []);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Requests::class,
]);
}
}
如何实现接受以下 JSON-Object 的表单? 我没有定义模型,因为我不需要它们。数据已发送
{
"type_of_error": "logic error",
"severity": "normal",
"what_did_you_do": "something",
"what_happened": "blue screen",
"which_result": "full satisfaction",
"requests": [
{
"url": "/api/v1/agent/statistic"
}
]
}
我已经开始建表了:
class ErrorReportFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('type_of_error', TextType::class, [
'empty_data' => '',
'constraints' => [
new NotBlank()
]
])
->add('severity', TextType::class, [
'empty_data' => '',
'constraints' => [
new NotBlank()
]
])
->add('what_did_you_do', TextType::class, [
'empty_data' => '',
'constraints' => [
new NotBlank()
]
])
->add('what_happened', TextType::class, [
'empty_data' => '',
'constraints' => [
new NotBlank(),
]
])
->add('which_result', TextType::class, [
'empty_data' => '',
'constraints' => [
new NotBlank(),
]
])
->add('requests', CollectionType::class, [
'entry_type' => RequestsFormType::class,
])
;
}
并且我已经这样定义了 RequestsFormType。这里有什么问题?看起来好像 RequestsFormType 不会被接受。
class RequestsFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('url', TextType::class, [
'empty_data' => '',
'constraints' => [
new NotBlank()
]
]);
}
public function getBlockPrefix()
{
return '';
}
}
也许尝试使用 CollectionType。
看这里:https://symfony.com/doc/current/reference/forms/types/collection.html
$builder->add('requests', CollectionType:class, [
'entry_type' => YourCustomType::class,
]);
您的自定义类型:
class YourCustomType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('url', TextType::class, []);
$builder->add('method', TextType::class, []);
$builder->add('timestamp', DateTimeType::class, []);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Requests::class,
]);
}
}