Forms API Example

Topics: Writing modules
Jan 19, 2012 at 9:16 PM

Does anyone have a good working example to use the Forms API to create forms for a public (non-administrative) view, store the data in the database, and get the data out using jQuery or something similar?

Jan 19, 2012 at 10:19 PM

Or is it only for the admin features? I was kind of hoping I can use it for public views (ie: Feedback form on a site or something).

Jan 19, 2012 at 11:00 PM

It's used primarily in the Rules and Projections UI, where it allows other modules to contribute bits of form to build a larger UI. Unfortunately the process of getting data back out of it seemed a bit obscure, at least that was my impression when I tried to use it for a simple admin form, and I got the feeling it was more geared towards those complex scenarios than creating simple individual forms. I ended up just using MVC. Still, if you follow the Rules and Projections examples you might get further and see something I missed.

Although, I don't know how you plan to use jQuery to get data out of a database?

Coordinator
Jan 20, 2012 at 12:49 AM

This should be pretty straightforward to get the XML representation of a filled form, and the opposite. Like 2 lines of code to get the values, and another 2 lines to bing the form. Then saving it to the db is one line, in a TEXT column.

Rules and Projections and using it extensively.

Jan 20, 2012 at 2:22 AM

In the Rules controller I see this:

 

            // build the form, and let external components alter it
            var form = _formManager.Build(action.Form);

            // generate an anti forgery token
            var viewContext = new ViewContext { HttpContext = HttpContext, Controller = this };
            var siteSalt = Services.WorkContext.CurrentSite.SiteSalt;
            var token = new HtmlHelper(viewContext, new ViewDataContainer()).AntiForgeryToken(siteSalt);

            // add a submit button to the form
            form
                ._Actions(Shape.Fieldset(
                    _RequestAntiForgeryToken: Shape.Markup(
                        Value: token.ToString()),
                    _Save: Shape.Submit(
                        Name: "op",
                        Value: T("Save"))
                    )
                );

 

...That's quite a lot of code to copy/paste just to display the form, maybe this could be boolean option on the form manager call?

Then I think I got slightly thrown by the fact that Forms and Projections are storing the whole dictionary in XML ... I just needed to get some values to perform some work there and then, a situation where I'd normally bind a model. It just seemed like the process was designed for more complicated cases than something that could be handled by simple MVC in this case.

Jan 20, 2012 at 4:12 AM
sebastienros wrote:

This should be pretty straightforward to get the XML representation of a filled form, and the opposite. Like 2 lines of code to get the values, and another 2 lines to bing the form. Then saving it to the db is one line, in a TEXT column.

Rules and Projections and using it extensively.

Do you mean of an admin form or a public form (such as a registration form). What I was hoping to do was create something such as a registration form and allow the admin to see the list of registered users.

randompete wrote:

It's used primarily in the Rules and Projections UI, where it allows other modules to contribute bits of form to build a larger UI. Unfortunately the process of getting data back out of it seemed a bit obscure, at least that was my impression when I tried to use it for a simple admin form, and I got the feeling it was more geared towards those complex scenarios than creating simple individual forms. I ended up just using MVC. Still, if you follow the Rules and Projections examples you might get further and see something I missed.

Although, I don't know how you plan to use jQuery to get data out of a database?

 I'd use a WCF service to return the data as a JSON dataset and then use jQuery within the page to show it.

Jan 20, 2012 at 4:32 AM

You generate the form in a controller action; it's the same whether it's back or front end, just MVC.

Why would you want to use WCF to display data in a page, can't you just pass a model into a view? Basically, how the client accesses the data is irrelevant: if you've got the data in a database and can query it, it's up to you how an action or other service emits it.

Jan 20, 2012 at 10:07 PM

So what I was thinking was that the Forms API had a certain table and format that it stored the data. If that's the case, I wasn't sure if there was a certain way to access the data and was assuming it would be accessible via a service.

Jan 20, 2012 at 10:26 PM

No, it expects you to manage storing and retrieving the data yourself, it just provides a system to build the form elements from shapes. Although it wouldn't be hard to make a generic system that just stored all the XML in text fields and expose a service to access it, but why need to use WCF, why not just emit a JSON array from an MVC action, it's much simpler? Anything that can consume WCF can probably consume JSON as well, and it's much more robust for a situation like this where you don't necessarily know the schema in advance...

Jan 21, 2012 at 1:29 AM

I guess I could do that. I really wasn't thinking about doing it that way. It would have been cool though if it allowed custom database table storage or on the fly table creation so that it's easier to query data. While you can query data in XML fields and/or can return all fields, build an XML set and query, it's not as easy and tends to be slower.


From: randompete [notifications@codeplex.com]
Sent: Friday, January 20, 2012 5:26 PM
To: Jason N. Gaylord
Subject: Re: Forms API Example [orchard:286732]

From: randompete

No, it expects you to manage storing and retrieving the data yourself, it just provides a system to build the form elements from shapes. Although it wouldn't be hard to make a generic system that just stored all the XML in text fields and expose a service to access it, but why need to use WCF, why not just emit a JSON array from an MVC action, it's much simpler? Anything that can consume WCF can probably consume JSON as well, and it's much more robust for a situation like this where you don't necessarily know the schema in advance...

Jan 21, 2012 at 2:39 AM

That would be too hard and too prone to error. Although I've had a little idea kicking around for a while that would do pretty much exactly what you're describing, it's extremely complex and not something I'm going to get far on any time soon ;)  Or you could look at doing some custom stuff with RavenDB which I think is much better suited to dynamically building tables than MS SQL.