input checkbox group from database sql dynamic
inputCheckboxDynamic
This element is checkbox group that takes values and labels from a database query (like selectDynamic or inputRadioDynamic does), and returns only those values that were checked.
Using a multiple select might be cumbersome for users: in such cases inputCheckboxDynamic is a great built-in alternative.
This element also features a very useful validation to minimum/maximum number of checkboxes required!
You can specify values for the unchecked and checked checkboxes. These values will be returned by $clonefish->getElementValues()
.
Note: according to the HTML specification, the unchecked checkbox values are missing when forms are submitted. Clonefish fixes this behaviour by design, so a checkbox value received from Clonefish is immediately insertable into a database for example.
Note: as this element contains multiple form inputs, there's no htmlid
setting for this element.
<?php
$config = Array(
'id' => Array(
'type' => 'inputCheckboxDynamic',
'displayname' => 'This is a dynamic checkbox group',
// first field: checkbox value,
// second field: checkbox label
'sql' => 'SELECT id, name FROM categories',
// an array to create additional checkboxes, with id => value pairs
'values' => Array(),
// eg. Array( 1 => 'Asia', 2 => 'Africa', 3 => 'Australia', ... )
'onvalue' => 1, // value returned for a checked checkbox
'offvalue' => 0, // value returned for an unchecked checkbox
// an array containing the ids (the first field from the SQL query).
// Values with these ids will be checked, so you can pre-check
// several/all checkboxes.
'value' => Array(), // eg. Array( 1, 3, 5 )
// an SQL query that returns ids to pre-check checkboxes
// similarly to the 'values' setting
'valuesql' => "SELECT categoryid FROM bookcategories WHERE id='12'",
// layout: the container for the checkbox group
'layout' => '%s',
// itemlayout: the layout of a checkbox row
'itemlayout' => "%checkbox% %label%<br />\n",
// divider settings: to create multiple columns
'divide' => 5, // default: null
'divider' => "</td><td>", // default: null, see example!
'html' => 'class="inputfieldstyle"',
'help' => 'validation failed for this element',
'rowlayout' => '...%element% %prefix% %postfix% etc...',
'prefix' => 'string to display before element',
'postfix' => 'string to display after element',
'readonly' => 1 | 0,
'display' => 1 | 0
)
);
?>
Live examples:
Validators to use together with this element:
form validation for developers!
clonefish is a proven, mature form generator class that helps PHP developers to create, validate and process secure, flexible and easy-to-maintain formslearn more