talking about...

PHP, Zend Framework, jQuery, Javascript, MySQL and other web development topics.

Zend Framework Tutorial: A Fully Customized Form Using Zend_Form and Decorators (UPDATED now using Tables)

Filed Under (General) by Wenbert on 29-10-2008

Tagged Under : , ,

This tutorial is derived from this post. So I suggest that you read weirophinney’s post first before this.

This method of rendering a form can be more verbose, but it also allows you to tweak the form at almost infinitely whole still gaining the advantages of error reporting, labeling, etc that decorators provide (by using the decorators associated with the elements).

I would like to stress out the advantages:

  • Error Reporting
  • And Labelling

You get these by doing only this in your view script (.phtml file):

<?=$this->form->firstname ?>
<?=$this->form->lastname ?>

Looking good? Well, here is the rest of it.

First, create this file: application/forms/sample/sampleform.php

Inside sampleform.php, you should have something like this:

 class forms_sample_sample2form extends Zend_Form
{
    public $checkboxDecorator = array(
                                    'ViewHelper',
                                    'Errors',
                                    'Description',
                                    array('HtmlTag',array('tag' => 'td')),
                                    array('Label',array('tag' => 'td','class' =>'element')),
                                    array(array('row' => 'HtmlTag'), array('tag' => 'tr')));
    public $elementDecorators = array(
                                    'ViewHelper',
                                    'Errors',
                                    'Description',
                                    array('HtmlTag',array('tag' => 'td')),
                                    array('Label',array('tag' => 'td','class' =>'element')),
                                    array(array('row' => 'HtmlTag'), array('tag' => 'tr')));
    public $buttonDecorators = array(
                                    'ViewHelper',
                                    array('HtmlTag',array('tag' => 'td')),
                                    //array('Label',array('tag' => 'td')), NO LABELS FOR BUTTONS
                                    array(array('row' => 'HtmlTag'), array('tag' => 'tr')));
    public function init()
    {
        $this->setAction('');
        $this->setMethod('post');
 
        $this->addElement('text', 'email', array(
            'decorators' => $this->elementDecorators,
            'label'       => 'Email:',
            'required'   => true,
            'validators'  => array(
                            'EmailAddress',
                            ),
            'attribs' =>   array(
                                'id'=>'email_id',
                                'class'=>'email_class'
                            ),
        ));
 
        $this->addElement('text', 'age', array(
            'decorators' => $this->elementDecorators,
            'label'      => 'Age:',
            'required'   => true,
        ));
 
        $this->addElement('select', 'country', array(
            'decorators' => $this->elementDecorators,
            'label'      => 'Country:',
            'required'   => true,
            'attribs' =>   array(
                                'id'=>'country_id',
                            ),
            'multioptions'   => array(
                            'ph' => 'Philippines',
                            'us' => 'USA',
                            ),
        ));
 
        $this->addElement('text', 'username', array(
            'decorators' => $this->elementDecorators,
            'label'       => 'Username:',
            'validators'  => array(
                                array('stringLength', 1, 255)
                            ),
            'required'   => true,
        ));
 
        $this->addElement('text', 'firstname', array(
            'decorators' => $this->elementDecorators,
            'label'      => 'First Name:',
            'required'   => true,
        ));
 
        $this->addElement('text', 'lastname', array(
            'decorators' => $this->elementDecorators,
            'label'       => 'Last Name:',
            'required'   => true,
        ));
 
        $this->addElement('radio', 'gender', array(
            'decorators' => $this->elementDecorators,
            'label'      => 'Gender:',
            'required'   => true,
            'attribs' =>   array(
                                'id'=>'country_id',
                            ),
            'multioptions'   => array(
                            'male' => 'Male',
                            'female' => 'Female',
                            ),
        ));
 
        $checkboxDecorator = array(
                                'ViewHelper',
                                'Errors',
                                array(array('data' => 'HtmlTag'), array('tag' => 'span', 'class' => 'element')),
                                array('Label', array('tag' => 'dt'),
                                array(array('row' => 'HtmlTag'), array('tag' => 'span')),
                            ));
        $this->addElement('checkbox', 'agreement', array(
            'decorators' => $checkboxDecorator,
            'label'       => 'Agreement:',
            'required'   => true,
        ));
 
        $this->addElement('submit', 'save', array(
            'decorators' => $this->buttonDecorators,
            'label'       => 'Save',
        ));
 
    }
    public function loadDefaultDecorators()
    {
        $this->setDecorators(array(
            'FormElements',
            array('HtmlTag', array('tag' => 'table')),
            'Form',
            'Errors'
        ));
    }
}

Then inside one of my controller-action, I have this:

    public function sample2Action()
    {
        require_once  'forms/sample/sampleform.php';
        $form = new forms_sample_sampleform();
        //get the request object
        $request = $this->getRequest();
        if($request->isPost()) {
            if($form->isValid($request->getPost())) {
                //do some saves here
                var_dump($request->getPost());
            }
        }
        $this->view->form = $form;
    }

Now, in my view script. These are the .phtml files.

<h4>Please register with us!</h4>
<form action="&lt;?= $this-&gt;escape($this-&gt;form-&gt;getAction()) ?&gt;" method="&lt; ?= $this-&gt;escape($this-&gt;form-&gt;getMethod()) ?&gt;">
 
<fieldset>
    <legend>Demographics</legend>
 
        Please provide us the following information so we can know more about
        you.
 
    &lt; ?= $this-&gt;form-&gt;age ?&gt;
    &lt; ?= $this-&gt;form-&gt;country ?&gt;
 
</fieldset>
 
<fieldset>
    <legend>User Information</legend>
 
        Now please tell us who you are and how to contact you.
 
    &lt; ?= $this-&gt;form-&gt;username ?&gt;
    &lt; ?= $this-&gt;form-&gt;firstname ?&gt;
    &lt; ?= $this-&gt;form-&gt;lastname ?&gt;
    &lt; ?= $this-&gt;form-&gt;email ?&gt;
    &lt; ?= $this-&gt;form-&gt;gender ?&gt;
    &lt; ?= $this-&gt;form-&gt;agreement ?
 
</fieldset>
 
&lt; ?= $this-&gt;form-&gt;save ?&gt;
</form>

Enjoy!

Feel free to comment/suggest… ;)

Some references in order to help you understand the form decorators ($elementDecorators, etc.) can be found in this thread from Nabble Zend Framework.

UPDATE:
The $elementDecorators variable above might seem a little bit confusing, so here is another example on how to handle it. This one sets a “class” for the first cell and the second cell of the table.

    public $elementDecorators = array(
                                    'Label',
                                    array(array('labelTd'=>'HtmlTag'),
                                          array('tag'=>'td', 'class'=>'label_cell')),
                                    array(array('elemTdOpen'=>'HtmlTag'),
                                          array('tag'=>'td', 'openOnly'=>true,
                                                'class'=>'input_cell', 'placement'=>'append')),
                                    'ViewHelper',
                                    'Errors',
                                    array('Description', array('tag' => 'div')),
                                    array(array('elemTdClose'=>'HtmlTag'),
                                          array('tag'=>'td', 'closeOnly'=>true, 'placement'=>'append')),
                                    array(array('row' => 'HtmlTag'), array('tag' => 'tr')));
 
    public $checkboxDecorator = array(
                                    'ViewHelper',
                                    'Errors',
                                    'Description',
                                    array('HtmlTag',array('tag' => 'td')),
                                    array('Label',array('tag' => 'td','class' =>'element')),
                                    array('Description', array('tag' => 'span')),
                                    array(array('row' => 'HtmlTag'), array('tag' => 'tr')));
 
    public $buttonDecorators = array(
                                    'ViewHelper',
                                    array('HtmlTag',array('tag' => 'td')),
                                    //array('Label',array('tag' => 'td')), NO LABELS FOR BUTTONS
                                    array(array('row' => 'HtmlTag'), array('tag' => 'tr')));

The best color picker for Mac OS

Filed Under (General) by Wenbert on 25-10-2008

Tagged Under : , , ,

Lightweight. Fast. Simple. It’s Pipette.

Type casting in MySQL: Comparing string numbers and getting the right result

Filed Under (General) by Wenbert on 22-10-2008

Tagged Under : ,

I recently ran into a problem where I had to compare “1000″ and “999″ — take note of the quotes, these values are strings. I did a SUBSTRING() in order to get those 2 strings, er “numbers”.

In this SQL statement, it asks whether “1000″ is greater than “999″. At first glance, the expected result would be ‘Yes’ but when you run that query, the result is ‘NO’. More info for MySQL If statements here.

SELECT
    IF("1000" &gt; "999",'Yes','NO')
FROM table WHERE 1

By doing a CAST() on the string, you will the correct result.

SELECT
IF( CAST("1000" AS DECIMAL) &gt; CAST("9" AS DECIMAL),'Yes','NO')
FROM table WHERE 1

More info for the MySQL CAST() function here.

ZF Quickstart Downloadable - Copy to your htdocs and then run it

Filed Under (General) by Wenbert on 21-10-2008

Tagged Under : , ,

Download the file. Untar, put in your htdocs folder (/var/www/zf_working) and the your should be able to run Zend Framework using: http://localhost/zf_working

I have tested this in two machines only :P I changed some paths in the bootstrap files from the ZF Quickstart tutorial. This file is provided as is — I’m at work right now and will be downloading when I get home to start one of my pet projects.

A Paper on SQL Injection

Filed Under (General) by Wenbert on 17-10-2008

Tagged Under : ,

It is a long read but this post links to PDF file about SQL Injection — here is the abstract:

 

ABSTRACT

Googling for “SQL injection” gets about 4 million hits. The topic excites interest and superstitious fear. This whitepaper dymystifies the topic and explains a straightforward approach to writing database PL/SQL programs that provably guarantees their immunity to SQL injection.

Only when a PL/SQL subprogram executes SQL that it creates at run time is there a risk of SQL injection; and you’ll see that it’s easier than you might think to freeze the SQL at PL/SQL compile time. Then you’ll understand that you need the rules which prevent the risk only for the rare scenarios that do require run-time-created SQL. It turns out that these rules are simple to state and easy to follow.

 

I have not read the entire paper yet — that is why I’m posting this one on my blog.

Useful Mac OS shortcuts

Filed Under (General) by Wenbert on 15-10-2008

Tagged Under : , ,

Okay, I am just going to copy-paste a post from another website and then just credit the author here.

⌘ = Command, the most common modifier.

⌥ = Option or Alt.

⇧ = Shift

^ = Control. Not used as frequently but is still there.

Basic ones…
⌘ + Q = Quit
⌘ + W = Close window
⌘ + O = Open a file in your application
⌘ + P = Print
⌘ + C = Copy
⌘ + V = Paste
⌘ + X = Cut
⌘ + S = Save
⌘ + Z = Undo
⌘ + A = Select All
⌘ + Y or Space = Quicklook
⌘ + Tab = Cycle through windows
And the rest of it….
⌘ + ⇧+ 3 = Take fullscreen picture
⌘ + ⇧ + 4 = Take selected area screenshot
⌘ + ⇧ + 4 + Space = Take screenshot of window or menu
⌥ + ⌘ + Escape = Bring up Force Quit window
⌘ + Space = Spotlight search
⌘ + ` (back tick) = Cycle through applications windows
⌘ + . = Cancel operation
⌘ + ⇧ + ? = Open help
⌘ + I = Get Info
⌘ + [ = Go backwards in history in Finder
⌘ + ] = Go forwards in history in Finder
⌘ + Up Arrow = Go to previous folder in hierarchy
⌘ + Down Arrow = Open folder of file in Finder
⌘ + ⌥ + T = Show hide Finder’s toolbar
⌘ + Delete = Move item to Trash
⌘ + ⇧ + Delete = Empty Trash
⌘ + E = Eject disk
⌘ + F = Find
⌘ + G = Next result in Find option
⌘ + H = Hide application
⌘ + M = Minimize
⌘ + N = New window
⌘ + ⌥ + W = Close all windows
⌘ + ⇧ + Z = Redo
⌘ + ⇧ + H = Go to Home folder
⌘ + ⇧ + D = Go to Desktop
⌘ + ⇧ + C = Go to Computer
⌘ + ⇧ + K = Go to Network
⌘ + ⇧ + I = Go to iDisk
⌘ + ⇧ + A = Go to Application
⌘ + ⇧ + U = Go to Utilities
⌘ + ⇧ + G = Go to folder
⇧ + ⌘ + Q = Log out
⇧ + ⌘ + ⌥ + Q = Log out immediately.
⌘ + D = Duplicate in Finder
⌘ + ⌥ + 8 = Turn on Voice Over
⌘ + ⌥ + + = Zoom in (if turned on)
⌘ + ⌥ + - =Zoom Out
⌘ + ⌥ + D = Show/Hide Dock
^ + Eject = Show shutdown dialog
^ + ⌘ + Eject = Close all and restart
⌥ + ⌘ + Eject = Sleep
⌘ + Shift + F = Find file by name
⌘ + R = Refresh widget
Seriously, thanks to http://www.mactricksandtips.com

Visual jQuery updated!

Filed Under (General) by Wenbert on 10-10-2008

Tagged Under : ,

For those who do not know, Visual jQuery has been updated!

Using Yahoo’s API to convert currencies with PHP

Filed Under (General) by Wenbert on 04-10-2008

Tagged Under : , , , ,

I have been looking around Yahoo Developer Network for this but found nothing relating to using the Currency Converter API.

You can make something similar to convert any currency to another currency using the Yahoo! database!

Here is what I came up with:

< ?php
$from   = 'USD'; //US Dollar
$to     = 'PHP'; //to Philippine Peso
 
$url = 'http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s='. $from . $to .'=X';
$handle = @fopen($url, 'r');
if ($handle) {
    $result = fgets($handle, 4096);
    fclose($handle);
}
 
$array = explode(',',$result);
 
var_dump($array);
/*
OUTPUTS: 
  array(4) {
  [0]=>
  string(10) ""USDPHP=X""
  [1]=>
  string(6) "47.125"
  [2]=>
  string(11) ""10/3/2008""
  [3]=>
  string(10) ""5:03pm"
"
}
 */

This is the simplest example I could give. And it works!

Subscribe to Rss Feed : Rss

Bad Behavior has blocked 619 access attempts in the last 7 days.