WebsiteBaker Support (2.8.x) > Droplets & Snippets

x_cForm

(1/4) > >>

aldus:
A simple modul snipplet for a class for formulars forms

Once you have the modul installed you can use a class "cForm" to build forms.
Notice: this is not QuickForm (PEAR) and it is not mean as a portation to WebsiteBaker at all;
even no recoding of the package.

Current Version: 0.1.3.beta - 2008-10-07

update: multiple selections support

Requirements:

* WebsiteBaker 2.7
* PHP 5.2.x
Known issues

* No options/radio - groups
* No validations
* No file-upload
ToDo - Roadmap

* add options/radio - groups
* add simple client/serverside validations
* file-upload

Bugfix in 0.1.2 Alpha: Checkbox-bugfix for missplaced "'" single quote ...

Bugfix in 0.1.1 Alpha: Typos ("cancle" instead of "cancel")

Any kind of improvment, critics, recomentations, code-cleanings, warnings and grumble
are always wellcome.

Some examples:

If you want to use this class in the backend you are in the need to import it first, like

--- Code: ---<?php
require_once (WB_PATH.&#39;/modules/x_cform/include.php&#39;);
?>

--- End code ---
If you want to use it inside a template you don't need it.

Initial within some formattributes, you can use any valid attribute as you like inside an assoc. array.

--- Code: ---<?php
$form_attribs = Array (
    &#39;name&#39;        => "wb_addresses_form",
    &#39;class&#39;        => "xForm",
    &#39;action&#39;    => $_SERVER[&#39;PHP_SELF&#39;]
);

$form = new c_form( $form_attribs );
?>

--- End code ---

Add a hidden field with the name "id" and a typical value to the formular

--- Code: ---<?php
$form->addElement( Array (&#39;type&#39; => &#39;hidden&#39;, &#39;name&#39; => "id", "value" => $_REQUEST[&#39;id&#39;]) );
?>

--- End code ---

Add a textfield with an label to the formular

--- Code: ---<?php
$form->addElement( Array (&#39;type&#39; => &#39;text&#39;, &#39;label&#39;=> ucfirst($i), &#39;name&#39; => $i, &#39;value&#39; => $data[$i] ) );
?>

--- End code ---

Add a textarea to the formular

--- Code: ---<?php
$form->addElement( Array (&#39;type&#39; => &#39;textarea&#39;, &#39;label&#39;=> ucfirst($i), &#39;name&#39; => $i, &#39;content&#39; => $data[$i] ) );
?>

--- End code ---

Add a select to the formular
As a "content" you will have to pass an assoc. array in the form "Key" = value, where the Key is display
in the popup menu. You can also pass a "select"-value to the select.

--- Code: ---<?php
$content = array (
    &#39;Anna&#39;        => 1,
    &#39;Judith&#39;    => 2,
    &#39;Mary Lou&#39;    => 3,
    &#39;Xanthippe&#39;    => 4
}
$form->addElement( Array (&#39;type&#39; => &#39;select&#39;, &#39;label&#39; => ucfirst($i), &#39;name&#39; => $i, &#39;content&#39; => $group_select, &#39;select&#39;=>3 ) );
?>

--- End code ---

Add a submit-button within some javascript.

--- Code: ---<?php
$form->addElement ( Array (&#39;type&#39; => &#39;submit&#39;, &#39;name&#39; => &#39;submit&#39;, &#39;value&#39; => $TEXT[&#39;SAVE&#39;], &#39;onclick&#39; => "this.submit();") );
?>

--- End code ---
update 0.1.3: support for multiple select ...

--- Code: ---<?php

$form->addElement ( Array (
    &#39;type&#39;        => &#39;select&#39;, 
    &#39;name&#39;        => "group_id[]", 
    &#39;label&#39;        => $ADDRESSES[&#39;DISPLAYGROUP&#39;], 
    &#39;content&#39;    => $group_select, 
    &#39;select&#39;    => explode(",",$temp_data[&#39;groups&#39;]), // !
    &#39;multiple&#39;    =>&#39;multiple&#39;, 
    &#39;size&#39;        => 4,
    &#39;onload&#39;    => "alert(this);"
) );
?>

--- End code ---

echo the complete form out of a module or template

--- Code: ---<?php
echo $form->toHTML();
?>

--- End code ---

Also supported types are "html", for "pur" html-code, "button", "cancel" and "checkbox".

To modify the output you can manipulate/change the template-strings (all public):

- $form_template - holds the complete form, default is:

"\n<form {{ attribs }}>\n{{ hidden }}\n<table>\n{{ content }}</table>\n</form>\n";

within the markers for

* {{ attribs }} :: the formular-attributes, e.g. name, class, onchange, onsubmit, action, method ...
* {{ hidden }} :: where to place the hidden values; by default at the begin of the formular
* {{ content }} :: the complete formular-content
- $element_template - holds a single element, default is:

"<tr>\n\t<td class='left'>{{ label }}</td>\n\t<td class='right'>{{ content }}</td>\n</tr>\n"

within the markers for

* {{ label }} :: where to display the label of the element
* {{ content }} :: where to display the element
So if you want to display the formular-elements inside divs you can easy overwrite the templates as you needed.


Regards
Aldus



[gelöscht durch Administrator]

aldus:
Update/Bugfix for 0.1.2 alpha

- Bugfix for "checkboxes" -> missplaced single quote bug ... could be problematic for some browsers
- Minor typos in the "info.php!" ...

Regards
Aldus

Stefek:

--- Quote from: aldus on September 03, 2008, 03:41:10 PM ---Any kind of improvment, critics, recomentations, code-cleanings, warnings and grumble
are always wellcome.

--- End quote ---
Have some "grumble"  8-)

I don't get the clue what this is all about.
I can't figure out what to do with this - no idea.

But looks very interesting.
Maybe a working example would help understanding.

Regards,
Stefek

aldus:
Make a new Page, Type: "code2", and place the following inside (PHP)

--- Code: ---<?php
/**
 *    @version    0.1.0
 *    @date        2008-09-16
 *    @author        aldus
 *    @package    WebsiteBaker - code-examples
 *
 */

$form_attribs = Array (
    &#39;name&#39;    => "search",
    &#39;class&#39;    => "xForm",
    &#39;action&#39;=> WB_URL."/search/index.php"
);

$form = new c_form( $form_attribs );

$form->addElement( Array (&#39;type&#39; => &#39;hidden&#39;, &#39;name&#39; => "page_id", "value" => $page_id ) );
$form->addElement( Array (&#39;type&#39; => &#39;hidden&#39;, &#39;name&#39; => "section_id", "value" => $section_id ) );

$form->addElement( Array (
    &#39;type&#39;    => &#39;text&#39;, 
    &#39;label&#39;    => "Suchbegriff: ", 
    &#39;name&#39;    => "string", 
    &#39;style&#39; => "display:block;float:left;width:200px;"  
    )
);

$form->addElement( Array(&#39;type&#39; => &#39;html&#39;, &#39;content&#39; => "&nbsp;") );

$form->addElement( Array (
    &#39;type&#39;        => &#39;submit&#39;, 
    &#39;name&#39;        => &#39;submit&#39;, 
    &#39;value&#39;        => "Suchen", 
    &#39;onclick&#39;    => "this.submit();", 
    &#39;style&#39;        => "display:block;float:left;width:200px;"
    )
);

echo $form->toHTML();
?>

--- End code ---

As for other working examples you can take a look at "addresses" ...

Regards
Aldus

Stefek:

--- Quote from: aldus on September 16, 2008, 11:52:05 AM ---Make a new Page, Type: "code2", and place the following inside (PHP)

--- End quote ---
Cool.

Best Regards,
Stefek

Navigation

[0] Message Index

[#] Next page

Go to full version