• Subscribe to the RSS feed!
  • Subscribe by Email
  • home
  • blog
  • dev
  • Recent Posts

    • Ze Balkanic Tweetup
      • on May 31, 2009
    • Moblin, Linux for netbooks
      • on May 21, 2009
    • Back
      • on
    • Happy birthday, dear magician…
      • on May 10, 2009
    • Wordpress as CMS tutorial
      • on March 14, 2009
    • New blog - Try Open Source
      • on March 11, 2009
    • Online resources for Zend Framework
      • on March 3, 2009
    • pywst - setting up web projects quickly
      • on February 22, 2009
    • Full Circle Magazine
      • on February 8, 2009
    • Trac on Ubuntu
      • on January 27, 2009
  • Recent Comments

    • Jason Gilmore
      • on June 23rd @ 5:04 am
    • slawek
      • on June 11th @ 1:29 am
    • igor
      • on June 7th @ 9:56 pm
    • Swizec
      • on June 1st @ 11:18 pm
    • Robert
      • on June 1st @ 8:12 pm
    • Eniac
      • on June 1st @ 2:17 pm
    • -1-
      • on May 31st @ 11:04 pm
    • Robert
      • on May 31st @ 10:54 pm
    • Swizec
      • on May 31st @ 10:27 pm
    • blackshtef
      • on May 31st @ 8:14 pm
  • Find me on

    • DZone
    • Google Code
    • Google Reader
    • Last.fm
    • StumbleUpon
    • Twitter
    • Vimeo
  • Friends and Blogs

    • Andrew Taylor
    • Andy Sowards
    • Bojan Pejić
    • Eran Galperin
    • Graham Smith
    • Jani Hartikainen
    • Jasper Tandy
    • Matthew Turland
    • Matthew Weier O’Phinney
    • Miff
    • Miloš Ćuković
    • Nebojša Radović
    • Nemanja Avramović
    • Nemanja Tobić
    • Nikola Krajačić
    • Nikola Plejić
    • Pádraic Brady
    • Rob Allen
    • Swizec Teller
    • Vladimir Stanković
    • WeAreJustCreative
    • Željko Stevanović
  • I use

    • 960 Grid System
    • jQuery
    • Notepad++
    • Subversion
    • Trac
    • Vim
    • Zend Framework
  • Tags

    • about
    • php
    • random
    • example
    • framework
    • zend
    • ubuntu
    • site
    • blog
    • introduction
    • wordpress
    • linux
    • apache
    • setup
    • lamp
    • svn
    • open source
    • registration
    • facebook
    • comic
  • Categories

    • Blablabla
    • Development
    • Free time
    • Places on the web
    • Programming
    • Software
  • Photos

    IMG_0463.JPG
    IMG_0467.JPG
    IMG_0473.JPG
    IMG_0472.JPG

  • Archives

    • May 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • September 2008

Styling the default Zend_Form layout

by Robert Basic on December 23rd, 2008

Here’s an example for styling Zend_Form’s default layout. The default layout is using definition lists. While there’s an option for changing the default layout, the wrapper tags and stuff, I see no reason for it. Create the form, add some CSS and your good to go :)

Note: Be sure to provide a Document Type in your view scripts like this:

<?= $this->doctype('XHTML1_STRICT') ?>

because when the form is generated, ZF is looking at the doctype to see how to create the form elements. Forgetting the doctype will probably generate invalid markup. I learned the hard way. Don’t do the same mistake, k? :)

The generated markup

So, here’s what Zend_Form makes for us (this markup is after submitting the form, but whit generated error, to show the error markup, too):

<form enctype="application/x-www-form-urlencoded" method="post" action="">
<dl class="zend_form">
    <dt>
        <label for="input1" class="required">Input field #1:</label>
    </dt>
    <dd>
        <input type="text" name="input1" id="input1" value="" />
        <ul class="errors">
            <li>Value is empty, but a non-empty value is required</li>
        </ul>
        <p class="description">Description? Yes, please.</p>
    </dd>
    <dt>
         
    </dt>
    <dd>
        <input type="submit" name="submit" id="submit" value="Submit form" />
    </dd>
</dl>
</form>

The PHP code which generates this form (without the error, of course) goes like this:

$input1 = new Zend_Form_Element_Text('input1');
$input1->setLabel('Input field #1:')
          ->setDescription('Description? Yes, please.')
          ->setRequired(true);

$submit = new Zend_Form_Element_Submit('submit');
$submit->setLabel('Submit form')

$form = new Zend_Form();
$form->setMethod('post')
       ->addElement($input1)
       ->addElement($submit);
Default Zend_Form layout with no CSS

Default Zend_Form layout with no CSS

Now, the generated form looks kinda good with no styling (which is good, if some maniac comes to visit with CSS support disabled).

OK, I lie: there’s a minimum of CSS for setting the background to white and the width to 460 pixels.

As you can see I’ve shortened the HTML and the PHP in the example codes…

The styling

I like my forms a bit different: form elements and their labels side by side with element descriptions and eventual errors showing up under the element. Here’s the CSS to achieve this:

.zend_form{
background:#fff;
width:460px;
margin:5px auto;
padding:0;
overflow:auto;
}

.zend_form dt{
padding:0;
clear:both;
width:30%;
float:left;
text-align:right;
margin:5px 5px 5px 0;
}

.zend_form dd{
padding:0;
float:left;
width:68%;
margin:5px 2px 5px 0;
}

.zend_form p{
padding:0;
margin:0;
}

.zend_form input, .zend_form textarea{
margin:0 0 2px 0;
padding:0;
}

.submit{
float:right;
}

.required:before{content:'* '}

.optional:before{content:'+ '}
Default Zend_Form layout with CSS

Default Zend_Form layout with CSS

Of course, this CSS takes care only of the layout; things like font types and sizes, colors, borders, backgrounds, etc. are not essential for this.

So, with this CSS applied to the generated Zend_Form, you can see on the image what will come up. And you know what’s the best part? It’s good for Firefox, Internet Explorer 6, Chrome and Opera, both under Windows and GNU/Linux (sorry, not tested for Internet Explorer 7 and Safari, but they should play along as well).

I almost forgot: I added a class=”submit” to the submit button, to be able to float it right. I first tried to do that with input[type=submit], but IE doesn’t know that, and as I wanted to make a styling that looks (almost) the same in all browsers with no hacks, I decided to add the class attribute.

So there, this little CSS code snippet should get you started with styling your Zend Form’s.

Cheers!

Share this post:
  • Digg
  • description
  • del.icio.us
  • StumbleUpon
  • Facebook
  • Reddit
  • TwitThis
  • Google
  • E-mail this story to a friend!
Tags: css, example, form, framework, layout, style, styling, zend.
Categories: Development, Programming.
Subscribe to the feed.

Comments: 5

Grab the comments feed

  • Josh

  • December 26th, 2008

short-tags are the devil :P

  • Robert Basic’s Blog: Styling the default Zend_Form layout : WebNetiques

  • December 26th, 2008

[...] Basic has a new post showing how to style the default output of the Zend_Form component of the Zend [...]

  • Robert Basic’s Blog: Styling the default Zend_Form layout : Dragonfly Networks

  • December 26th, 2008

[...] Basic has a new post showing how to style the default output of the Zend_Form component of the Zend [...]

  • Fernando Bittencourt

  • December 26th, 2008

Hey, Robert,

Internet Explorer don’t handle pseudo-elements like :before properly, so I suggest you to use the ‘requiredSuffix’ in the element’s label decorators. To use HTML markup, don’t forget to set ‘escape’ option to false.

Hope this is useful :) Cheers.

  • abtris

  • December 27th, 2008

Fix your version GSH. In php highlight you have twice word empty. You have to delete empty from reserved words in GSH.
Good post.

Leave a Reply

 

Robert Basic © 2008 — 2009
Design & graphics by: Livia Radvanski
Coded by: Robert Basic
Home page last updated on January 3rd, 2009.
Frameworks used: Zend Framework, jQuery, 960 Grid System
Blog is powered by Wordpress
Subscribe: Entries — RSS & Comments — RSS