Jump to content

PEAR: DB_DataObject_FormBuilder w/ Templates?


shazam

Recommended Posts


Hello,

I have been playing around with the PEAR HTML_QuickForm & DB_DataObject_FormBuilder packages.

While I figured out how to extend the QuickForm Default renderer to meet my needs, I am having trouble figuring out how to do the same with FormBuilder.

Here is an example of working code w/ QuickForm (hacked together from many examples on the web):

[code]
<link rel="stylesheet" type="text/css" href="/style/style.css">

<?php

require_once "HTML/QuickForm.php";
require_once "HTML/QuickForm/Renderer/Default.php";

class CSSRender extends HTML_QuickForm_Renderer_Default {

var $_headerTemplate = "<tr class=form>
                          <th class=form colspan=\"2\" scope=\"row\">
  {header}
  </th>
</tr>";

var $_elementTemplate = "<tr class=\"form\">
                          <td class=\"form\" id=\"label\">
    {label}
    <!-- BEGIN required -->
      <span class=\"required\">*</span>
    <!-- END required -->
  </td>
  <td class=\"form\">
    <!-- BEGIN error -->
  <span class=\"error\">{error}</span><br />
<!-- END error -->
  {element}
</td>
  </tr>";

var $_formTemplate = "<form class=\"form\" {attributes}>
                        {hidden}
<table class=\"form\">
  {content}
</table>
  </form>";

var $_requiredNoteTemplate = "<tr>
                                <td class=\"form\"></td>
<td class=\"form\">{requiredNote}</td>
  </tr>";

}

$form = new HTML_QuickForm('simple_test', 'get');
$renderer = new CSSRender();
$form->addElement('header', 'head', 'Contact Information','class=form');
$form->addElement('text', 'name', 'Name','class=form');
$form->addElement('text', 'phone', 'Phone','class=form');
$buttons[] = &HTML_QuickForm::createElement('reset', 'btnClear', 'Clear');
$buttons[] = &HTML_QuickForm::createElement('submit', 'btnSubmit', 'Submit');
$form->addGroup($buttons, null, null, '&nbsp;');

$form->addRule('name', 'Name required', 'required', '', 'client');
$form->addRule('phone', 'Phone number required', 'required', '', 'client');

if ($form->validate()) {
  # If the form validates then freeze the data
  $form->freeze();
}

$form->accept($renderer);
echo $renderer->toHtml();

?>
[/code]

Essentially, I can use the same Renderer w/ FormBuilder, but I cannot figure out how to convert the following QuickForm code to FormBuilder (the key being the 'class=form' option for addElement):

[code]
$form->addElement('header', 'head', 'Contact Information','class=form');
$form->addElement('text', 'name', 'Name','class=form');
$form->addElement('text', 'phone', 'Phone','class=form');
[/code]

FormBuilder, of course, auto-generates the form elements from database tables.  I suppose I need to find a way to modify how FB creates the elements...

Any ideas or examples on how to do this?

Thanks!

- B
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.