S
S
sl02021-06-08 12:49:48
symfony
sl0, 2021-06-08 12:49:48

Is it possible to programmatically create a DTO and validate it?

The bottom line is this: there is a service for surveys. The admin composes a survey by choosing the type of question, answer options, etc. For these questions, a form is generated that the user fills out. Questions can be of different types (regular text, with a choice of checkboxes / radio buttons, files, etc.
Everything works on the site side, but you need to make an api for this service. And here's the problem. Usually I make dto for such purposes, which then I'm validating. But in this case I can't create it, because I don't know in advance how many questions there will be and what type they will be. I need to somehow dynamically create a dto based on the existing questions and then validate the user's answers. Or maybe there is some other Is there an easier way

?

"questions": [
            {
                "id"         : 1,
                "title"      : "question 1",
                "constraints": {
                    "NotBlank": true
                },
                "type"       : "TextType"
            },
            {
                "id"         : 2,
                "title"      : "question 2",
                "constraints": {
                    "NotBlank": true
                },
                "type"       : "TextAreaType"
            },
            {
                "id"         : 3,
                "title"      : "question 3 (select)",
                "constraints": {
                    "NotBlank": true
                },
                "choices"    : {
                    "new0": {
                        "value": "first option"
                    },
                    "new1": {
                        "value": "second option"
                    },
                    "new3": {
                        "value": "third option"
                    }
                },
                "type"       : "ChoiceType"
            },
            {
                "id"         : 6,
                "title"      : "question 3 (checkboxes)",
                "constraints": {
                    "NotBlank": true
                },
                "choices"    : {
                    "new0": {
                        "value": "first option"
                    },
                    "new1": {
                        "value": "second option"
                    },
                    "new3": {
                        "value": "third option"
                    }
                },
                "type"       : "CheckboxChoiceType"
            },
            {
                "id"         : 12,
                "title"      : "Image",
                "constraints": {
                    "NotNull": true
                },
                "choices"    : null,
                "type"       : "ImageType"
            }
        ]


Answer example:
"answers"       : {
    "1": "text",
    "2": "a lot of text",
    "3": "first option",
    "6" : [
      {"0" : "first option"},
      {"1" : "third option"}
    ],
    "12":  "data:@file/jpeg;base64,..."
  }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Derepko, 2021-06-08
@uDenX

For each request/response, it's better to create a DTO
https://symfony.com/doc/current/components/seriali...

M
Maxim Fedorov, 2021-06-08
@Maksclub

There are various kinds of tasks when DSLs are built and you have to dynamically create rules and objects.
It all depends on the situation and the chosen solution in response to it.
Validation of answers in this case will depend on what type is chosen / constructed when creating a question. It is under these rules that you need to do validation, it seems that there will be no final ATTs here, only intermediate

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question