Zendcasts Forum

A community of developers who work with the Zend Framework and other enterprise PHP technologies

You are not logged in.

#1 2010-02-25 13:01:34

harri
Member
From: Kristiansand, Norway
Registered: 2009-12-02
Posts: 27
Website

Dynamic Forms?

Hi there.

I'm working on a custom registration/poll system which basicly stores forms and filled out forms i a database and shows this in structured order.

I wonder if it is possible to use Zend Forms to make the forms from the information in the database (fieldname, type, required etc.) or do I need to make something custom?

I can explain more if needed. smile

Offline

#2 2010-02-25 21:17:16

iznogood
Member
From: Montreal
Registered: 2009-12-17
Posts: 20
Website

Re: Dynamic Forms?

If you mean create a form automagically from database info we just had a discussion about this here and as far as I know nothing like that is implemented.
If you are wondering how to populate a zend form with stuff from a database look into the Zend_Form->populate() function.

Offline

#3 2010-03-01 01:59:57

harri
Member
From: Kristiansand, Norway
Registered: 2009-12-02
Posts: 27
Website

Re: Dynamic Forms?

I mean the first you suggested. What did the discussion end with? Do you think it is possible to do?

The last suggestion is something I'm going to need, but haven't really thought about it before so I'll definitely be looking into that as well.

Offline

#4 2010-03-03 05:09:24

iznogood
Member
From: Montreal
Registered: 2009-12-17
Posts: 20
Website

Re: Dynamic Forms?

harri wrote:

I mean the first you suggested. What did the discussion end with? Do you think it is possible to do?

The last suggestion is something I'm going to need, but haven't really thought about it before so I'll definitely be looking into that as well.

I wasn't personally involved in it but you could find it her in the forums its fairly recent. If I recall correctly its all a question of separating the database stuff from the business logic. Having a auto-form from db class would defeat this patern.
And it could get complicated. What about fields you do not want in the form? Like datestamps for creation, modification, last user and stuff like that.

Offline

#5 2010-04-21 17:39:31

harri
Member
From: Kristiansand, Norway
Registered: 2009-12-02
Posts: 27
Website

Re: Dynamic Forms?

Yeah, that is a good question. I have not thought about those things. I am not that far in my program yet, but eventually I need to find a way to do it.

Offline

#6 2010-04-21 19:31:17

iznogood
Member
From: Montreal
Registered: 2009-12-17
Posts: 20
Website

Re: Dynamic Forms?

harri wrote:

Yeah, that is a good question. I have not thought about those things. I am not that far in my program yet, but eventually I need to find a way to do it.

All that being said it should'nt be very difficult to make a class that extends Zend_Form and creates fields from some array (for you data from a query).

Offline

#7 2010-06-05 00:09:25

harri
Member
From: Kristiansand, Norway
Registered: 2009-12-02
Posts: 27
Website

Re: Dynamic Forms?

So now I'm as far as figuring out how to do this. I'm gonna think out loud and try to explain the situation.

Short info: I use Doctrine, if that would be relevant

The core of the application is to make registration forms or polls, which needs to have the ability to make custom fields/choose the fields needed. Without this capability it would be only one possible hardcoded  (or several different hardcoded) form(s). So the point is to be able to define the fields needed.

I have a model called Form which has a set of attributes (or the table have fields). This is a no-brainer to do with Zend_Form, but a form in my application is not complete without one or more fields (Field model) related to it (the actual fields a user would fill inn to be registered or vote for inn a poll).
A picture to explain the database
Word definitions:
form = form in the application (Form + Field)
fields = fields in a html form (inputs, textarea, etc)
Form = Doctrine based model for meta data about a registration/poll (name,time frame, description, etc)
Field = Doctrine based model that stores info about the fields in the (required, name,  small description, type, etc)


I have wondered (for when an admin create a new form with the application) if it's possible to just make a Zend_Form, that you normally would to pass on data to be persisted, for Form but just leave out the submit element and sneak inn the extra fields (Field) in the view and then send them in the same post as the Zend_Form. And then filter out the fields belonging to the Form model, persist them, thus leaving the Field data left to be persisted.
Would this work? Do you have any other solutions?

Then, for making the form that the user are going to fill out, I could try to do what iznogood suggested in the post above.

What do you guys (or girls?) suggest? I kinda rely on you to help me out here.:)

Just let me know if I need to explain more.:) I'm not sure if explained it clearly enough.

Last edited by harri (2010-06-05 00:11:39)

Offline

#8 2010-12-15 11:08:46

Jacks-hon
New member
From: asia
Registered: 2010-12-13
Posts: 2
Website

Re: Dynamic Forms?

Here's what I'm trying to do: I have a php page I created on one domain, that stores information in a database. Next, I have another web page (This page is html NOT php) on a Different domain that I call this php page using Javascript, and would like to return a variable from the php page. How is this possible?

Offline

#9 2010-12-16 11:14:09

samuelherzog
Member
From: Eugendorf, Austria
Registered: 2009-08-18
Posts: 27

Re: Dynamic Forms?

Okay, i somehow never saw this discussion but I guess to maybe have some ideas for you. i don't know if you can need them anymore, but I hope to find out big_smile

@harri
Zend_Form implements the ability to generate from config, so if you offer some interface to generate a config, you could save this one in your db and populate Zend_Form with it, generating a user-generated form.
alternativly you maybe like to iterate over your db data and then pass it on to $form->addElement($type, $name, $otherConf);

@Jacks-hon
I guess you're calling via XHTTPRequest aka AJAX - so the most simplest thing to do would be in your phpfile:
[code=php]
header('Content-type: application/json');
echo json_encode($yourDataArray);
# or the Zend way
echo Zend_Json::encode($yourDataArray);
[/code]
Zend_Json has the advantage that you don't have to care if the php extension for json_encode is activated, it will fall back to a (slower) php implementation.

this way you generate a javascript object for your html page to fetch.
after that theres only javascript magic left to do smile

aye, i just realized that with your HTML being on another domain you have to AJAX a php-proxy bcs of the same-origin-policy.
if you can't use a php-proxy maybe generating an iframe and reacting on its onLoad event helps here.

hope that helps,
Samuel

Offline

#10 2010-12-17 21:53:27

harri
Member
From: Kristiansand, Norway
Registered: 2009-12-02
Posts: 27
Website

Re: Dynamic Forms?

So I have been working with my little registrations system for a while on the side. I think I have worked it out relatively well. you can check it out here: http://sourceforge.net/p/zendreg/home/

Offline

Board footer

Powered by FluxBB