Dynamic forms

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/ 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?

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. [email protected]

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?

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. [email protected]

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.

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.

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/ 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?