Source for file repeat.php
Documentation is available at repeat.php
* Usage example for HTML_QuickForm2 package: repeat element
require_once 'HTML/QuickForm2.php';
require_once 'HTML/QuickForm2/Renderer.php';
'country' => array (4 , 706 , 180 ),
'Pirate hideout. Aaargh!',
'Somewhere in the jungle'
'default' => array (true , false , false ),
'title' => array ('php.net', 'pear.php.net', 'google.com')
/* @var $fsOne HTML_QuickForm2_Container_Fieldset */
$fsOne = $form->addFieldset ()->setLabel ('Fieldset-based repeat element');
/* @var $repeatFs HTML_QuickForm2_Container_Repeat */
$repeatFs = $fsOne->addRepeat ()
->setId ('repeat-fieldset')
->setLabel ('Shipping addresses');
'' => "-- please select --",
180 => "Congo, Democratic Republic of",
$country = $repeatFs->addSelect ('country')->loadOptions ($countries)->setLabel ('Country:');
$repeatFs->addText ('region', array ('style' => 'width: 20em;'))->setLabel ('Region:');
$street = $repeatFs->addText ('street', array ('style' => 'width: 20em;'))->setLabel ('Street address:');
$repeatFs->addCheckbox ('default')->setContent ('default shipping address');
// button to remove a repeated item from a repeat, enabled automatically
$repeatFs->addButton ('remove', array ('type' => 'button'))
->setContent ('remove this address')
->addClass ('repeatRemove');
// setting rules for repeated elements, these will work properly server-side and client-side
$country->addRule ('required', 'Please select a country', null ,
$street->addRule ('required', 'Please input street address', null ,
/* @var $fsTwo HTML_QuickForm2_Container_Fieldset */
$fsTwo = $form->addFieldset ()->setLabel ('Group-based repeat element');
/* @var $repeatGroup HTML_QuickForm2_Container_Repeat */
$repeatGroup = $fsTwo->addRepeat (
null , array ('id' => 'repeat-group'),
->setLabel ('A link:')->setSeparator (' '))
)->setIndexField ('links[title]') // not strictly necessary, but doesn't hurt either
$repeatGroup->addText ('title', array ('style' => 'width: 15em;'));
// specially crafted value attribute to prevent adding index to name
$repeatGroup->addRadio ('main', array ('value' => 'yes_:idx:'))->setContent ('main');
// button to remove a repeated item from a repeat
$repeatGroup->addButton ('remove', array ('type' => 'button'))
->addClass ('repeatRemove');
// a button for adding repeated elements, with an explicit onclick
$fsTwo->addButton ('add', array (
'onclick' => "document.getElementById('repeat-group').repeat.add(); return false;"
))->setContent ('Add another link');
$form->addSubmit ('submit', array ('value' => 'Send this form'));
/* @var $renderer HTML_QuickForm2_Renderer_Default */
// a custom template for first repeat element, a link for adding repeated
// elements there will be automatically made active due to repeatAdd class
$renderer->setTemplateForId (
<div class="row repeat" id="{id}">
<qf:label><p>{label}</p></qf:label>
<a class="repeatAdd" href="#">Add another address...</a>
// Use this with the callback renderer
$renderer->setCallbackForId(
'repeat-fieldset', function ($renderer, $repeat) {
'<div class="row repeat" id="%s"><p>%s</p><br /><a class="repeatAdd" href="#">Add another address...</a></div>',
$repeat->getId(), $repeat->getLabel(), implode(array_pop($renderer->html))
$form->render ($renderer);
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
/* Set up custom font and form width */
font-family: Arial,sans-serif;
/* Use default styles included with the package */
if ('@data_dir@' != '@' . 'data_dir@') {
$filename = '@data_dir@/HTML_QuickForm2/quickform.css';
/* http://www.quirksmode.org/css/clearing.html */
#repeat-group .repeatItem { overflow: auto; width: 100%; }
/* zebra table for group-based repeat */
.quickform .repeat .odd { background-color: #FEE; }
.quickform .repeat .even { background-color: #EEF; }
// Inline QuickForm's javascript libraries
echo $renderer->getJavascriptBuilder ()->getLibraries (true , true );
<title>HTML_QuickForm2 repeat element example</title>
<script type="text/javascript">
// add event handlers to repeats
var repeatFs = document.getElementById('repeat-fieldset').repeat,
repeatGroup = document.getElementById('repeat-group').repeat;
repeatFs.onBeforeAdd = function()
var items = this.getElementsByClass('repeatItem', this.container);
// 5 visible items and 1 hidden prototype
alert('5 addresses should be enough for everybody!');
repeatFs.onBeforeRemove = function(item)
var items = this.getElementsByClass('repeatItem', this.container);
alert('You cannot remove the last address');
repeatGroup.onChange = function()
var items = this.getElementsByClass('repeatItem', this.container);
for (var i = 1, item; item = items[i]; i++) {
qf.classes.add(item, i % 2 ? 'odd' : 'even');
qf.classes.remove(item, i % 2 ? 'even' : 'odd');
// paint zebra for initial values
Documentation generated on Wed, 10 Apr 2019 08:56:11 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|