# Dynamic forms

**URL:** <https://www.ruby-forum.com/t/dynamic-forms/103437>\
**Category:** Rails\
**Created:** [May 6, 2007, 1:42am UTC](https://www.ruby-forum.com/t/dynamic-forms/103437 "2007-05-06T01:42:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![chapaev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7cd45c/32.png) [@chapaev](https://www.ruby-forum.com/u/chapaev)\
**Post date:** [May 6, 2007, 1:42am UTC](https://www.ruby-forum.com/t/dynamic-forms/103437/1 "2007-05-06T01:42:55Z")

</div>

Hi everyone!

I’m new to Rails and i am interesting in methods for developing dynamic  
forms. I’ll try to explain what i wanted to say. I’m making a  
web-application for Human Resource Management and there must be  
opportunity for users to visually create forms: user selects the type of  
input, variants, validation options will be also awesome. And after that  
CRUD interface with search options must be created. So it’s a  
model-editor like stuff.

Now I’m reading about meta-programming and checking  
[http://activeform.rubyforge.org/](http://activeform.rubyforge.org/) as a compontent for creating interface.

What I’m thinking about:

1. Create form → Create Rails Model + yml-file for storing form  
structure.
2. Edit form → Add Migrations(add/delete columns) + edit Model  
file(add/delete validations) + edit yml-file.
3. Generate Interface → Something like scaffold generator.

Is it ok to let application creating/editing model/migrations?

Any ideas?

---

<div class="post-metadata">

**Author:** ![chapaev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7cd45c/32.png) [@chapaev](https://www.ruby-forum.com/u/chapaev)\
**Post date:** [May 6, 2007, 2:25am UTC](https://www.ruby-forum.com/t/dynamic-forms/103437/2 "2007-05-06T02:25:47Z")

</div>

There are two types of approaches. Generating things like migrations  
and model classes for data storage is generally not “dynamic” but can  
be generated by code. It is more common to use a more generic schema  
for data storage and dynamically create the presentation. This would  
be more “dynamic” as the behavior is changing at run-time rather than  
design-time. I am not sure which approach you were asking about.

Who are your users? Are they HR people that are not programmers? If  
this is the case you definitely do not want to be changing the  
database schema at run-time unless you really know what you are  
doing. There are a lot of issues with modifying a production database  
schema that end-users will not be able to cope with. If you are  
targeting programmers then are you looking to help them during  
development or after deployment?

Michael

On May 5, 4:42 pm, Alexey L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![chapaev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7cd45c/32.png) [@chapaev](https://www.ruby-forum.com/u/chapaev)\
**Post date:** [May 6, 2007, 2:44am UTC](https://www.ruby-forum.com/t/dynamic-forms/103437/3 "2007-05-06T02:44:34Z")

</div>

MichaelLatta wrote:

> There are two types of approaches. Generating things like migrations  
> and model classes for data storage is generally not “dynamic” but can  
> be generated by code. It is more common to use a more generic schema  
> for data storage and dynamically create the presentation. This would  
> be more “dynamic” as the behavior is changing at run-time rather than  
> design-time. I am not sure which approach you were asking about.

I was meaning the first one, but really didn’t like thr mixing the  
elements of run-time & design-time.

> Who are your users? Are they HR people that are not programmers? If  
> this is the case you definitely do not want to be changing the  
> database schema at run-time unless you really know what you are  
> doing. There are a lot of issues with modifying a production database  
> schema that end-users will not be able to cope with. If you are  
> targeting programmers then are you looking to help them during  
> development or after deployment?

Not programmers. Those items(creating model & adding migrations) were  
meant to be done by application(code generation).

I thought about generic schema, but had no idea how to implement it. Are  
there any examples?

---

<div class="post-metadata">

**Author:** ![chapaev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7cd45c/32.png) [@chapaev](https://www.ruby-forum.com/u/chapaev)\
**Post date:** [May 6, 2007, 5:02am UTC](https://www.ruby-forum.com/t/dynamic-forms/103437/4 "2007-05-06T05:02:55Z")

</div>

I’ve done something similar to this and got it working however I’m  
unsure if my solution was truly the best way as I realized quickly it  
was over kill.

But basically I did it like this

Data Table that stored hashes of various datatypes as well as a field  
that told me what type of data was being stored.

Then I had a Forms Table and a Forms Attributes table

Forms can have many attributes and attributes.

Then I could get to it like

Forms.attributes.collect { |a| a.datum.data }

look into the serialize method of rails for storing multiple datatypes  
for the form attributes.

Of course you can add new fields and such by adding them to a “form  
instance” etc.

This is all just REALLY general information, but it could help you  
along, since I had written quite a bit of code before I abandoned it  
for a much more “hard coded” approach and the project was scaled down  
and dynamic forms were removed for the sake of me wanting to have a  
project finished before the end of the year.

Good luck to you,  
Jim

On May 5, 7:44 pm, Alexey L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![chapaev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7cd45c/32.png) [@chapaev](https://www.ruby-forum.com/u/chapaev)\
**Post date:** [June 5, 2007, 2:40pm UTC](https://www.ruby-forum.com/t/dynamic-forms/103437/5 "2007-06-05T14:40:45Z")

</div>

p3rlhax wrote:

> Hi Alexey
> 
> Did you figure out how to do this? I have the exact same requirement  
> and am looking for a way to create forms dynamically as well.  
> In my case I know the fields and their datatypes. For example  
> Name - text  
> Address - text  
> Phone number - numeric  
> dateofbirth - date
> 
> I need to create a form programatically so that I can accept this data  
> and store it in the database.
> 
> Any help would be appreciated.
> 
> Thanks

I decided not to use them and to make application hard-coded. I have  
made a presentation for the client where I described pros & consof  
dynamic forms) My arguments were accepted.

But I also thought about dynamic implementation and here are some notes:

"My thougts about dynamic forms.

The only way to make it done is to use a generic schema for data storage  
and dynamically create the presentation. For example I will need three  
tables:

- Forms  
id | name

- Forms Attributes  
id | form\_id | name | type | values | validators | searchable

The type may be: text input, select input, radio button, checkbox,  
textarea. If we want define data to be selected(like sex: male/female)  
we create a hash-map of variants and save it serialized to values field:  
[1 =\> “male”, 2 =\> “female”]. Some basic validators(is\_data,  
is\_numeric, is\_not\_null…) maybe with  
parameters will be saved like: [“is\_not\_null” =\> “true”, “is\_numeric” =\>  
“true”, “min\_length” =\> “15”]Searchable may be a boolean value(if it’s  
true – we add it to the search form).

- Data  
id | form\_id | attribute\_id | data"

Maybe that will help you.

---

<div class="post-metadata">

**Author:** ![chapaev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7cd45c/32.png) [@chapaev](https://www.ruby-forum.com/u/chapaev)\
**Post date:** [June 6, 2007, 6:39am UTC](https://www.ruby-forum.com/t/dynamic-forms/103437/6 "2007-06-06T06:39:10Z")

</div>

Hi Alexey

I agree that dynamically changing the schema is not a good idea. Even  
with a generic schema, I still need to generate the presentation  
dynamically.  
The only way I know to do that would be to put logic into the view to  
check the datatype (stored in the database ) and then use the  
corresponding helper.

For instance, using my previous example  
\<%= if Field.type = text %\>  
\<%= text\_field …\>  
\<%= if Field.type = text-multiline%\>  
\<%= text\_area …\>

I was wondering if there is a better way to do this.

Thanks  
p3rlhax

Alexey L. wrote:

> p3rlhax wrote:
> 
> > Hi Alexey
> > 
> > Did you figure out how to do this? I have the exact same requirement  
> > and am looking for a way to create forms dynamically as well.  
> > In my case I know the fields and their datatypes. For example  
> > Name - text  
> > Address - text  
> > Phone number - numeric  
> > dateofbirth - date
> > 
> > I need to create a form programatically so that I can accept this data  
> > and store it in the database.
> > 
> > Any help would be appreciated.
> > 
> > Thanks
> 
> I decided not to use them and to make application hard-coded. I have  
> made a presentation for the client where I described pros & consof  
> dynamic forms) My arguments were accepted.
> 
> But I also thought about dynamic implementation and here are some notes:
> 
> "My thougts about dynamic forms.
> 
> The only way to make it done is to use a generic schema for data storage  
> and dynamically create the presentation. For example I will need three  
> tables:
> 
> - Forms  
> id | name
> 
> - Forms Attributes  
> id | form\_id | name | type | values | validators | searchable
> 
> The type may be: text input, select input, radio button, checkbox,  
> textarea. If we want define data to be selected(like sex: male/female)  
> we create a hash-map of variants and save it serialized to values field:  
> [1 =\> “male”, 2 =\> “female”]. Some basic validators(is\_data,  
> is\_numeric, is\_not\_null…) maybe with  
> parameters will be saved like: [“is\_not\_null” =\> “true”, “is\_numeric” =\>  
> “true”, “min\_length” =\> “15”]Searchable may be a boolean value(if it’s  
> true – we add it to the search form).
> 
> - Data  
> id | form\_id | attribute\_id | data"
> 
> Maybe that will help you.

---

<div class="post-metadata">

**Author:** ![chapaev](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/7cd45c/32.png) [@chapaev](https://www.ruby-forum.com/u/chapaev)\
**Post date:** [June 5, 2007, 1:58pm UTC](https://www.ruby-forum.com/t/dynamic-forms/103437/7 "2007-06-05T13:58:35Z")

</div>

Hi Alexey

Did you figure out how to do this? I have the exact same requirement  
and am looking for a way to create forms dynamically as well.  
In my case I know the fields and their datatypes. For example  
Name - text  
Address - text  
Phone number - numeric  
dateofbirth - date

I need to create a form programatically so that I can accept this data  
and store it in the database.

Any help would be appreciated.

Thanks

Alexey L. wrote:

> Hi everyone!
> 
> I’m new to Rails and i am interesting in methods for developing dynamic  
> forms. I’ll try to explain what i wanted to say. I’m making a  
> web-application for Human Resource Management and there must be  
> opportunity for users to visually create forms: user selects the type of  
> input, variants, validation options will be also awesome. And after that  
> CRUD interface with search options must be created. So it’s a  
> model-editor like stuff.
> 
> Now I’m reading about meta-programming and checking  
> [http://activeform.rubyforge.org/](http://activeform.rubyforge.org/) as a compontent for creating interface.
> 
> What I’m thinking about:
> 
> 1. Create form → Create Rails Model + yml-file for storing form  
> structure.
> 2. Edit form → Add Migrations(add/delete columns) + edit Model  
> file(add/delete validations) + edit yml-file.
> 3. Generate Interface → Something like scaffold generator.
> 
> Is it ok to let application creating/editing model/migrations?
> 
> Any ideas?
