Monday, November 26, 2012

Zend Framework 1: Zend Form Display Groups

Greetings Programs,

Creating large forms with many inputs can be a challenge for your UI especially if you want to avoid scrolling the page. These days with tablets, smart tvs, and cell phones the need to create UIs that minimize scrolling is often desired. If you and your team are going to write an app for every platform out there then you probably have a pretty big team. If you don't,  don't despair, many of these platforms have a browser so if you're careful how you design your UI you might find that you can have a very nice interface across many platforms and not have to have a fleet of developers for every platform out there. 

Zend Forms and display groups are one potential way to solve this problem. In this Post we are going to create a shipping and billing address form that is split across the screen. Billing on one side and shipping on the other. We will be using form decorators to wrap display groups in a div that can be positioned using CSS. In the next post we will look at using sub forms to accomplish the same effect.

The code is available at https://github.com/jdellostritto/ZTMPL_formdisplaygroups.git


To start we need an idea of what we want this to look like...


The form above is separated in two halves based on our target design. A sample structure that will allow for what we want to accomplish can be seen below.

...
Full code for the Application_Address_Form Class can be found on github. In this form class we create functions for the inputs we need created. Because we have two nearly identical display groups we create a single function that creates our input text field based on a name we give it.  In the _render_fullname function we send an argument called $given_name. Calling $this->_render_fullname("bill_fullname") and $this->_render_fullname("ship_fullname") will result in two input elements on our form (we could further optimize our code by creating a single function that creates any text input - later). All we need to do after this is add them to two distinct display groups....

The init() function of our form helps us do just this. First we create the items we need in our form. Notice step 4 where we create duplicate inputs for both groups:
...
Second we create our groups and decorate the form. Step 7 in the code below is where we create our display groups giving the names of the elements created previously as an array, giving each group a distinct name, and finally decorating them in a structure that meets our design requirements.
...
Decorating our groups is done in the function below. Notice that we take the group name and a given_name as arguments. The group is the target display group and the given_name is a string we can use to differentiate our div id attribute:
...
The last item left on our list is to style our page to get the desired effect. This is accomplished by absolute positioning the bill group and ship group....


I have some other small tricks in this baseline that I have learned from many sources... too many to list.  There are many ways to accomplish what i have posted so please if you find the time to contribute to this code or make suggestions please do not hesitate. I have learned from many posts and other sources and I appreciate every one.  

...
Kevin Flynn: Like a man says, "There are no problems, only solutions."
...
Tron 1982

3 comments:

  1. Hi I'm new in Zend framework and I would like to create come complex form using Zend 2. What i try too get is form like that: http://goo.gl/D9ghh
    . I have N-photos (where N can be any number) and i would like to create this kind of form simply not to edit every single photo. I did it, using only PHP, but don't know even how to start this using Zend 2. Whould you be so nice and tell me how start it, how write correct my Model and Form files, to be able check any number of photos, and how to display form next to photo?

    ReplyDelete
    Replies
    1. I looked at your link. Are you trying to add text to a photo, then use the check box to mark the photo(s) to be saved when you hit the apply button on the upper left of you page?

      If you can give me a quick story board description step by step on what it is you want to accomplish from the view point of the user and what in turn you want to save I could probably get you moving in the right direction.

      Delete
    2. Thx Jim
      1.) Yes this is what I want.

      2.) I want to be able to add some description to photos, I want also add some check-box for additional options like i.e. make photo (in)visible and/or remove photo.
      I could do this just for every single photo by adding some edit link that will open edit page. In this case it will be a time killer to open every single photo and reedit.
      I also need this to create order photos page, were user could choose photo, print size and how no. of copies. At this case it will be a bit more complex cos we will not know exactly number of photos but also print size will not have fixed number. So it will be much easier for user to choose some photos, choose some size of prints and number of copies and just send it all as a one group.

      Delete