Zend Form – custom decorators

28 April 2008

So I’m learning PHP and Zend Framework and MVC stuff. And I just figured out how to do a custom decorator on Zend_Form to change the way the form is written to HTML. The default puts each element label and input box into dd and dl tags. I just wanted to put each element in a div, with the input box on the same line as the label. There may be better ways to do this, but this is what I put in the IndexController.php file:

$form = new Zend_Form(array(
    'elementDecorators'=>array(
       'ViewHelper',
       array(
          'Description',
          array(
             'tag'=>'div',
             'class'=>'help'
          )
       ),
       'Errors',
       array('Label'),
       array(
          'HtmlTag',
           array(
              'tag'=>'div',
              'class'=>'field'
           )
        )
    ),
    'decorators'=>array(
        'FormElements',
        array(
          'HtmlTag',
          array('tag' => '<div>')
        ),
        'Form',
     ),
    'method' => 'post',
    'action' =>'/url/to/form',
    'elements' => array(
        'name' => array('text', array(
            'label' => 'Name:'
        )),
        'subject' => array('text', array(
            'label' => 'Subject:'
         )),
        'submit' => array('submit', array(
            'label' => 'Add',
        ))
    ),
 ));
 $form->submit->removeDecorator('Label');
 return $form;
}
 public function formAction()
 {
      $this->view->form = $form;
 }

The $form->submit->removeDecorator(‘Label’) bit removes the extra ‘Add’ from the submit button.

Then you just put
echo $this->form;
in your form.phtml

(This form does nothing)

This results in HTML like:

<form method="post" action="/route/to/form">
   <div>
      <div class="field">
         <label for="name" class="optional">Name:</label>
         <input type="text" name="subject" id="subject" value="">
      </div>
      <div class="field">
          <label for="subject" class="optional">Subject:</label>
          <input type="text" name="subject" id="subject" value="">
      </div>
      <div class="field">
           <input name="submit" id="submit" value="Add" type="submit">
      </div>
   </div>
</form>

Instead of the default dd and dl layed out form.

My PHP for beginners notes

19 March 2008

Disclaimer – there are likely to be mistakes in this, so please point them out! Here are my notes from my PHP for beginners course, which was excellent, and was run by Highlander. I know it’s better to use the PHP manual but it helps me to write these out so feel free to ignore these!

DAY 1

  • boolean = true or false
  • int = integer
  • float = fraction
  • string = text, in quotes
  • resource = external (not native to PHP) resource
  • $variablename

Associative arrays

  • $myArray = array (“keyname1″=>”value1″, “keyname2″ =>”value2″);
  • If no key name is provided, the default is 0, 1, 2… etc.
  • [square brackets are used to ACCESS array contents]
  • (normal brackets are used to SET UP functions and arrays)

HTML

<pre>
this will print on two
lines, even without a br tag
</pre>

Examples

$x = $x + 1;
//equals
x ++;

$n = 3;
echo $n * $n- -;

// (ignore spaces between minus symbols) equals
echo $n * ($n- -);

// two minus symbols after $n means run the line then apply (-1) to $n

$n = 3;
echo $n * – -$n;

// equals
– -$n;
echo $n*$n;

// two minus symbols before $n means apply (-1) to $n THEN run whole line.

(more…)

Dead #butterfly #taxidermyBiologyTea and pastryTom being BritishSam and teacupClaudia becomes British partyWell this can sod offMoustache Christmas cracker
Powered by Wordpress   |   Lunated designed by ZenVerse