From 2f10258ced4ab47311afffce07755551ac30e1df Mon Sep 17 00:00:00 2001
From: Adrian Lang <lang@cosmocode.de>
Date: Tue, 15 Jun 2010 10:08:38 +0200
Subject: [PATCH] =?UTF-8?q?Add=20an=20optional=20off=20value=20to=20Doku?=
 =?UTF-8?q?=5FForm=E2=80=99s=20checkboxes?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Since an unchecked HTML checkbox has no value at all, a hidden field may be
used to specify an off value for the checkbox.
---
 inc/form.php | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/inc/form.php b/inc/form.php
index cebaf4608..70190d2b4 100644
--- a/inc/form.php
+++ b/inc/form.php
@@ -484,6 +484,8 @@ function form_makeFileField($name, $label=null, $id='', $class='', $attrs=array(
  * form_makeCheckboxField
  *
  * Create a form element for a checkbox input element with label.
+ * If $value is an array, a hidden field with the same name and the value
+ * $value[1] is constructed as well.
  *
  * @see     form_makeFieldRight
  * @author  Tom N Harris <tnharris@whoopdedo.org>
@@ -818,6 +820,8 @@ function form_filefield($attrs) {
  *   _class : class attribute used on the label tag
  *   _text  : Text to display after the input. Not escaped.
  * Other attributes are passed to buildAttributes() for the input tag.
+ * If value is an array, a hidden field with the same name and the value
+ * $attrs['value'][1] is constructed as well.
  *
  * @author  Tom N Harris <tnharris@whoopdedo.org>
  */
@@ -827,7 +831,13 @@ function form_checkboxfield($attrs) {
     $s = '<label';
     if ($attrs['_class']) $s .= ' class="'.$attrs['_class'].'"';
     if (!empty($attrs['id'])) $s .= ' for="'.$attrs['id'].'"';
-    $s .= '><input type="checkbox" '.buildAttributes($attrs,true).'/>';
+    $s .= '>';
+    if (is_array($attrs['value'])) {
+        echo '<input type="hidden" name="' . hsc($attrs['name']) .'"'
+                 . ' value="' . hsc($attrs['value'][1]) . '" />';
+        $attrs['value'] = $attrs['value'][0];
+    }
+    $s .= '<input type="checkbox" '.buildAttributes($attrs,true).'/>';
     $s .= ' <span>'.$attrs['_text'].'</span></label>';
     if (preg_match('/(^| )block($| )/', $attrs['_class']))
         $s .= '<br />';
-- 
GitLab