Dynamic Form Fields Based On Db Rows

I have a table "notification_types" (id, type, name).

The table consists of the next rows:

comment_photo

comment_comment

vote_photo

vote_comment

comment_article

I want to create a form based on this rows. I want to let users to choose notifications (via checkboxes) they want to receive.

In perfect in my model I would create a method named something like "setNotificationFields", which would get data from a database and dynamically create properties of the object with validators and labels without need to manually declare public properties of each type of notification. Is it possible?

Thank you.

Why don’t you maintain all those rows in the database as boolean and use checkboxes to set/reset? And if you need to work on, work with only the set boxes. Would this be your solution?

At first that was my solution, but I thought that it would be better if I will not depend on DB schema. Now I’m starting to think about database storing this values as columns again, otherwise it seems to require hacks.

I think you need two table, one for notification types and another to store each user answers for example

notification_type

(nt_id ,type ,name)

notification_result

(nt_id,user_id,result)

then you can retrieve notification types and create your form dynamically and collecting results as Tabular Input

Reza m, your example is exactly how I was thinking. But I can’t find a way to create form dinamically. Having table “notification_type” I will need to build rows of checkboxes with attributes name and label retrieved from database. I could create all needed fields-properties right in form, but I think it’s hacky.

by looking at Collecting Tabular Input article your path is clear, for example you can create an array of notification results models in controller and send them to view then create your form and collect results from user