-1

How to implement the form, that accepts the following JSON-Object? I have defined no models, because I don't need them. The data are sent

{
  "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"
    }
  ]
}

I already have begun to build the form:

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,
            ])
        ;
    }

And I have defined RequestsFormType like this. What is here wrong? It looks, as if RequestsFormType would not be accepted.

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 '';
    }
}
olek07
  • 513
  • 2
  • 7
  • 21

1 Answers1

1

Maybe try with using a CollectionType.

See here : https://symfony.com/doc/current/reference/forms/types/collection.html

$builder->add('requests', CollectionType:class, [
    'entry_type'   => YourCustomType::class,
]);

YourCustomType :

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,
        ]);
    }
}
Alexis
  • 347
  • 5
  • 16
  • and how to define url, method and timestamp? – olek07 May 09 '19 at 09:30
  • In Collectiontype give the argument : `'entry_type' => YourCustomType::class,` Create a custom form and in the builder add url, method and timestamp – Alexis May 09 '19 at 09:31
  • should I define an extra class? Is there no another solution? and how to create a custom form in the buildForm-method? – olek07 May 09 '19 at 09:34
  • Yes, you should create a custom type, like RequestsType and in Form builder, add your parameters : url, method and timestamp. I don't know if there is another solution. This one should fits your need – Alexis May 09 '19 at 09:37
  • Ok, it looks like a form in another form. – olek07 May 09 '19 at 09:39
  • How to create YourCustomType in the same php-file? I don't want to create an extra file with the class YourCustomType – olek07 May 09 '19 at 09:42
  • The best practice is to create a separate file for your Forms. Have a look at the documentation : https://symfony.com/doc/current/forms.html#form-creating-form-classes Create your separate file and then include it in your controller : `$form = $this->createForm(XXXType::class, $xxxx);` – Alexis May 09 '19 at 09:45
  • Yes, but I can not say entry_type' => $form. It doesn't work – olek07 May 09 '19 at 09:47
  • You should have two Form files : XXXXType.php (type_of_error, severity, etc..) and RequestsType.php (url, timestamp etc..). In XXXXType you are building your form. So add the CollectionType and in parameter `entry_type` give your second form `RequestsType:class`. And in your controller create the $form like I said before – Alexis May 09 '19 at 09:52
  • It doesn't work. I receive an error: The form may not have additional fields (Dieses Formular sollte keine zusätzlichen Felder enthalten.) – olek07 May 09 '19 at 10:04
  • Could you update your post to add what you did please ? – Alexis May 09 '19 at 10:11
  • `'allow_add'` => true was missing. It works now, Thanks! – olek07 May 09 '19 at 11:54