fishbone
fishbone

Reputation: 3249

Parse nested data from HTTP requests without form classes with Symfony

Our Symfny application allows to print labels on a sheet with 6 labels. The user can specify an additional text for each label and whether to print it or not.

At the moment HTTP GET parameters contain the configuration:

labels[0][enabled]=true
labels[0][text]=foo
labels[1][enabled]=false
labels[1][text]=bar
...

In our controller the enabled field would be interpreted as string, but I'd like to to pass booleans to the view template.

$request->query->get('labels')[0]['enabled']
// 'true'

There is $request->query->getBoolean('...'), but I cannot use it here, because the booleans are nested in the array.

I could convert the entries manually in a foreach loop, but I'm looking for a better solution.

I thought that Symfony Forms would be useful here, but I don't want to create form type classes for such cases. Fortunately forms can be created inline like this:

$this->createFormBuilder($data)
    ->add(...)
    ->getForm()

In my case however I'd need to use CollectionType. The inner form type (with enabled and text fields) has to be defined as class name / string.

So I wonder if there is way to create an inline form as above with nested data?

If not, is there a another way to parse such request data? We have other similar cases in our application where other developers just manually parsed data from GET parameters which is always problematic with non-strings types like booleans or dates.

Upvotes: 0

Views: 158

Answers (0)

Related Questions