Propel and patForms integrated: powerful form generation

posted: May 3rd, 2005 · by: Sven

in: Programming · tagged as: , , , , ·  13 comments »

Propel is a php object persistence layer that enables you to:

  • generate an xml database scheme from an existing database,
  • generate ORM object and peer classes from this scheme and
  • then already use these classes as a (part of the) model of your application

Propel lets you describe and configure in detail how your classes are to be build. You can define validatiors for example, against which you can check your objects before you save them to the database. And you can define foreign key table relations and resolve them in a really simple manner.

The form generation toolkit patForms on the other side comes along with lots of capabilities to create flexible forms, nicely separated into layers by design and easily extensible. patForms itself provides ready-made form validation rules, tools to populate form elements like Select boxes from Datasources and much more.

Plugging these both tools together makes up a powerful combination.

5 steps to heaven

Let’s have a look at what these both tools can do for you when they join forces.

  1. You simply throw a Propel class(name) into a patForms_Definition_Propel. This will look up information that’s necessary to build a form and write it to a form definition xml file on the fly.
  2. Once created you can tweak the form definition xml file according to your needs. For example you might want to rename labels, descriptions or display different values from related tables (linked by foreign keys).
  3. But you don’t need to tweak anything. Yet, handing this definition over to a patForms_Creator_Definition will create a complete and working patForms instance – full fledged with validation rules (both client- and server-side) and Html select boxes populated from related tables (via foreign keys).
  4. This patForms instance is able to connect to a patForms_Storage_Propel, that will (at present) look at the $_POST and $_GET vars and automagically retrieve, validate and save objects for you, populating the form from the object or – vice versa – populating the object from the form values.
  5. You can now render the patForms instance by using one of the provided patForms_Renderers and display the form elements and data using a template engine of your choice.

You’re done.

New data has been validated before it has been saved to the database. If a validation error has occured, the error will be displayed by your form template and nothing has gone to the database. Existing objects have been fetched from the database, have been validated and updated.

Yes, life can be that easy :)

To say the least: we are really impressed about how seamlessly everything in Propel and patForms (both being great projects for themselves) fits together with only some minor changes to both of them. The good design pays out on both sides, keeping the libraries flexible and extensible.

7 lines of code

Wanna see some code? Ok, let’s do it together …

By now, it takes less than 10 lines of code to create a working “Add a new book” form for the bookstore example application shipped with Propel:

$definition = patForms_Definition_Propel::create(array(
   'name' => 'book', 'filename' => $filename));
$storage = patForms::createStorage('Propel');
$storage->setStorageLocation('BookPeer');
$form = &patForms::createCreator('Definition')->create($definition);
$form->setRenderer(patForms::createRenderer('Array'));
$form->setStorage($storage);

The result will look like this:

screenshot 1

As you can see, there are two related tables (foreign keys) defined for the Propel Book entity: Author and Publisher, both referenced by ID. For each of them a Select box is created and already populated with the IDs available from those tables.

screenshot 2

Some very little changes

Of cause, we’ll want to show something different than these IDs here. So, we’ll tweak the form definition xml file, that’s already been created on-the-fly.

For the AuthorId, we’ll change the lines

<members>
  <tag>Id</tag>
</members>
<mask>%s</mask>

to something like this:

<members>
  <tag>FirstName</tag>
  <tag>LastName</tag>
</members>
<mask>%s (%s)</mask>

And (after having additionally changed some labels and titles) we’ll get:

screenshot 3

Save new and edit existing

When we fill in some valid form data and click the “Save Form” button, the data gets already saved to the database table. (This will be painlessly done by a patForms_Storage_Propel driver by simply registering it to the form.)

By the way … want an “Edit an existing book” form instead?

Just add one single line before rendering the form to the template:

$form->setValues(array('Id' => 1)); // the primary key

This will display the Book with the ID 1 and we will already be able to edit existing data and save it back to the database:

screenshot 4

Even shorter?

It’s no big deal to further imagine a patForms_Factory::create() method, that could be called by something like this:

$form = patForms_Factory::create(array(
    'type' => 'Propel',
    'class' => 'Book',
    'renderer' => 'Array',
    'primary' => array('Id', 1)
));

One call for a complete and full-fledged, auto-validated form that’s defined by the database scheme and some very simple additional changes in an xml file.

Hey, don’t say that’s not really cool :)

Go and play with it …

You will need working installations of:

... as well as a build version of the Propel Bookstore project (tested with mysql). Have a look at the Propel docs to get started with the Bookstore build.

But don’t use it!

Ok. :)

This is a proof of concept. There are several flaws and missing stuff, things to be discussed and solved. And yes: patForms itself is considered to be in alpha …

But with some extra work here and there these both tools will make up a really great team that can help you to get started very fast with a working prototype for your data(base)-driven web application.

Feedback?

Do you like this? Then please digg it!

Leave a comment

13 Comments

  1. Jeremy Seitz said June 2nd, 2005 at 02:24 AM  

    I've been looking around a while for a good PHP framework (like DBObjects and HTMLQuickform), but this looks much more flexible. I will definitely try it out.

    Is this being used for any live web sites? How does it perform, in terms of time saved and flexibility? With some frameworks, it can be difficult to do fancy interface things, like if you want related drop-downs (like, city/state) with javascript onChange()... are there ways to override the default renderers?

  2. Sven said June 2nd, 2005 at 03:59 AM  

    Jeremy,

    this still is in pre-alpha state, really. So there's no performance optimization done yet (not even performance tests) and it's not used in any live pages.

    Propel is known to be not as fast as PEAR DB. On the other hand, you will get a rock solid, very straight-forward and objectoriented object mapping layer (which is the reason for me to choose Propel instead of PEAR DB or other solutions). Propel is matured and has brilliant support via the mailing lists - Hans Lellelid and others are doing a great job over there.

    PatForms also is in alpha state but under active development. It will already integrate nicely with patTemplate (also matured and used in production) and a currently boosted state-of-the art framework (still called "patPortal", the name will change in future).

    The php-tools (pat-stuff) actually is very flexible because everything is nicely separated by design. You might want to have a look at the patForms design. Builder, Renderer, the form object itself, Storage etc. are all separate and extensible classes.

    Drop me a mail if there's anything I can assist you with. Any reports on how this works for you also are very appreciated.

  3. Stephan Schmidt said June 8th, 2005 at 03:23 PM  

    Jeremy,

    patForms is extremely extendible and the related drop-down functionilty is already available as a seperate rule, which allows you to connect an input field/radion group/whatever with a dropdown. Rules are able to generate javascript so the validation and user-interaction can be done on the client and the server.

    Stephan

  4. Tom Balon said October 6th, 2005 at 12:02 AM  

    I've been using propel for a new project to define and collect pricing information on various products. I use hibernate on the java side, and I really appreciate propel.

    The combination of patForms and propel sounds great. I wish I had this a year or so ago when I was building lots of dynamic php/html forms that needed to be updated via the web.

    Tom

  5. Michel Feldheim said February 1st, 2006 at 01:13 PM  

    I am feeling like Alice in wonderland, a lot of new ways for developement, a lot of new innovative ideas.. nice!! Thanks to the pat team, thanks to the propel team.. i love you guys. Will enter active developement to help leaving alpha/beta states of many interesting packages

  6. Ed Finkler said August 23rd, 2006 at 05:42 AM  

    Looks cool. This is very similar to DBDataObjectFormBuilder, which basically combines DBDataObject and HTMLQuickForms.

  7. moepMan said April 1st, 2008 at 09:46 AM  

    as of 2008 the patform/propel team is not working together fine anymore

    generation of a simple form is working as usual, but foreign keys and pre-loaded values are not working.

    is there any development team left, the last release sems to be from 2005

  8. jack said January 24th, 2011 at 03:29 PM  

    UCVHOST has changed the face of web hosting industry in a major way, people were paying gold for peanuts (and it is still happening). cheap hosting has become synonym with UCVHOST, anybody and everybody who wants a reliable and affordable domain web hosting visits UCVHOST and gets either windows vps or Linux hosting from UCVHOST. UCVHOST sells cheap hosting WITHOUT hidden terms and conditions where as competition has huge MSA and SLA’s which are good enough to confuse a seasoned lawyer also. For clients by now Business with us for the value of windows vps became very critical piece of puzzle for their whole operation, uptime and performance became a huge concern.. However it came with a cost, dedicated servers proved to be at least 100 times expensive in comparison to any windows or Linux plans. Somewhere in the labs engineers were working on splicing raw power of a server into virtual instances, this technology was called as Virtualization also termed as or virtual private servers. Also UCVHOST comes handy when you are looking for remotely hosted and managed FOREX MetaTrader4 terminals. Our forex vps platform is all geared up in fight of pips, our platform support any number of expert advisory (EA) and along with an assure of 100% uptime. Our Virtual Forex Tradng Terminals are well equipped to help you in making money .

  9. QQQ said February 7th, 2011 at 06:34 PM  

    Finally we kissed and the passion scale went sky high and I knew I was onto a good thing - sex was a certainty free porn videos. She never hesitated when I began to fondle her breasts and she willingly exposed them for me mobile porn. They were firm and I suspected a breast enhancement but said nothing - they still felt good and I was enjoying them and gradually working my way further south free porn tube. She was a step ahead of me and before I could completely undress her she moved on me atk hairy and I was suddenly having my pants pulled down and I was enjoying one of he best cock sucking hairy pussy experiences I had ever had. ABB728019394

  10. chat said March 31st, 2011 at 08:22 PM  

    The following cleaned up the issue:

    Dependencies.loadoncepaths -= Dependencies.loadoncepaths.select{|path| \ path =~ %r(^#{File.dirname(FILE)}) }

  11. Okey oyunu said May 12th, 2011 at 04:30 PM  

    Thanks a lot. This is nice post. Tüm dünya artik okey oyunu oynuyor. Yillardir bir çok oyun programi olmasina ragmen, içlerinden en güzeli olarak nitelendirebilecegimiz tek bir site göze çarpmaktadir. Diger tüm okey oyunu programlarinin aksine ücretsiz olmasi ve 3 boyutlu olarak hizmet vermesi mükemmel bir gelismedir. Sizlerde www.okey-oyunu.com adresinden bu essiz okey oyununu indirebilirsiniz. Kullanimi çok basit ve Türkçe dil seçenegi ile kolaylikla oyuna baslayabilirsiniz. Ister kendi ülkenizden, isterseniz dünyanin tüm farkli bölgelerinden dilediginiz oyun odalarini seçerek, oyuna hemen baslayabilirsiniz. Okey oyunu oynamak için artik arkadas bile aramaniza gerek kalmadan, bilgisayarinizdan 100 binlerce üye ile online olarak okey oyununu oynamanin zevkine varabilirsiniz.

  12. porno said May 23rd, 2011 at 01:12 PM  

    I do agree with all of the ideas you have presented in your post. They’re really convincing and will definitely work. Still, the posts are too short for newbies. Could you please extend them a bit from next time? Thanks for the post.

  13. porno said May 23rd, 2011 at 01:12 PM  

    good comment. thanks you friends.

    I’ve surfed the net more than three hours today, however, I haven’t found such useful information. Thanks a lot, it is really useful to me

Sorry, comments are closed for this article.

artweb design
Sven Fuchs
Grünberger Str. 65
10245 Berlin, Germany


http://www.artweb-design.de

Fon +49 (30) 47 98 69 96
Fax +49 (30) 47 98 69 97