diff --git a/form.php b/form.php index dd9ed59..1c33634 100644 --- a/form.php +++ b/form.php @@ -34,23 +34,25 @@ class form $this->formName = $formName; } - /** Save the fields into the structure. + /** Save the array of fields into the structure. Available : - - name : name of the field in the HTML page - - label : label written to the describe the field - - [titles] : text written in radio/checkboxes - - [defaults] : default values. Must be array for checkbox/select, and - string for others - - [type] : text, password, hidden, checkbox, select, radio, submit, - textarea - text by default - - [multiple] : Multiple selection are possible (if the type supports it) - - [group] : define a fieldset and define the title with groupe name - Warning : all the elements of the same group must be - consecutive ! - - [readonly] : put a read-only flag on the field (the user see it but - can't interract on it. The value will be sent to next - page + - name : name of the field in the HTML page + - label : label written to the describe the field + - [titles] : text written in radio/checkboxes + - [defaults] : default values. Must be array for checkbox/select, and + string for others + - [type] : text, password, hidden, checkbox, select, radio, submit, + textarea + text by default + - [help] : The Help message (written below the field). Overwrite in + case of error + - [multiple] : Multiple selection are possible (if the type supports it) + - [group] : define a fieldset and define the title with groupe name + Warning : all the elements of the same group must be + consecutive ! + - [readonly] : put a read-only flag on the field (the user see it but + can't interract on it. The value will be sent to next + page - [mandatory] : boolean to add a red star at end of label - [hidden] : hide the field (add a style='display:hidden' to the field) - [rows] : Number of rows @@ -63,6 +65,13 @@ class form $this->fields = $fields; } + /** Add a field to the form. For the details of a field, see the description + in fields method */ + public function addfield ($field) + { + $this->fields[] = $field; + } + /** Return the values provided by the user. Test the CSRF before continue NEVER read the values from $_POST in your codes or CSRF will not be checked */ @@ -129,6 +138,8 @@ class form public function printHTML ($method = 'post', $values = NULL, $errors = array()) { + if (count ($this->fields) === 0) + throw new Exception ("Can't display a form without defined field", 500); $this->method = strtolower ($method); $res = ""; $res = "
label); if (isset ($this->mandatory) && $this->mandatory !== FALSE) - $res .= " *"; + $res .= " *"; else - $res .= "  "; + $res .= " "; $res .= "\n"; $res .= "
\n"; if (is_string ($this->defaults)) $this->defaults = array ($this->defaults); foreach ($this->titles as $key=>$val) { + $res .= "
"; $res .= " name, ENT_QUOTES)."][$key]'"; @@ -278,16 +293,30 @@ class formfield elseif (isset ($this->defaults[$key]) && $this->defaults[$key] !== "") $res .= " checked='checked'"; - $res .= " class='form-control'"; if (isset ($this->hidden) && $this->hidden !== FALSE) $res .= " style='display:none'"; + if (isset ($this->errors) || isset ($this->help)) + { + $res .= " aria-describedby='".$this->formName."_"; + $res .= htmlspecialchars ($this->name, ENT_QUOTES)."_help'"; + } $res .= "/>"; $res .= "$val\n"; + $res .= "
\n"; } if (isset ($this->errors)) - $res .= " ".$this->errors[1]. + { + $res .= " ". + $this->errors[1]."\n"; + } + elseif (isset ($this->help)) + { + $res .= " ".$this->help. "\n"; + } $res .= "
\n"; // End controls $res .= " \n"; // End form-group return $res; @@ -329,9 +358,9 @@ class formfield $res .= ">"; $res .= htmlspecialchars ($this->label); if (isset ($this->mandatory) && $this->mandatory !== FALSE) - $res .= " *"; + $res .= " *"; else - $res .= "  "; + $res .= " "; $res .= "\n"; $res .= "
\n"; $res .= " hidden) && $this->hidden !== FALSE) $res .= " style='display:none'"; + if (isset ($this->cols)) + $res .= " size='".$this->cols."'"; + if (isset ($this->errors) || isset ($this->help)) + { + $res .= " aria-describedby='".$this->formName."_"; + $res .= htmlspecialchars ($this->name, ENT_QUOTES)."_help'"; + } $res .= "/>\n"; if (isset ($this->errors)) - $res .= " ".$this->errors[1]. + { + $res .= " ". + $this->errors[1]."\n"; + } + elseif (isset ($this->help)) + { + $res .= " ".$this->help. "\n"; + } $res .= "
\n"; // End controls $res .= " \n"; // End form-group return $res; @@ -376,9 +421,9 @@ class formfield $res .= ">"; $res .= htmlspecialchars ($this->label); if (isset ($this->mandatory) && $this->mandatory !== FALSE) - $res .= " *"; + $res .= " *"; else - $res .= "  "; + $res .= " "; $res .= "\n"; $res .= "
\n"; if (is_string ($this->defaults)) @@ -390,7 +435,8 @@ class formfield $res .= "/>\n"; foreach ($this->titles as $key=>$val) { - $res .= "
\n"; // End controls $res .= " \n"; // End form-group return $res; @@ -438,9 +498,9 @@ class formfield $res .= ">"; $res .= htmlspecialchars ($this->label); if (isset ($this->mandatory) && $this->mandatory !== FALSE) - $res .= " *"; + $res .= " *"; else - $res .= "  "; + $res .= " "; $res .= "\n"; $res .= "
\n"; if (isset ($this->defaults) && is_array ($this->defaults)) @@ -484,6 +544,11 @@ class formfield $res .= " class='form-control'"; if (isset ($this->rows)) $res .= " size='".$this->rows."'"; + if (isset ($this->errors) || isset ($this->help)) + { + $res .= " aria-describedby='".$this->formName."_"; + $res .= htmlspecialchars ($this->name, ENT_QUOTES)."_help'"; + } $res .= ">\n"; foreach ($this->defaults as $key=>$val) { @@ -504,8 +569,17 @@ class formfield $res .= " \n"; if (isset ($this->errors)) - $res .= " ".$this->errors[1]. - "\n"; + { + $res .= " ". + $this->errors[1]."\n"; + } + elseif (isset ($this->help)) + { + $res .= " ".$this->help + ."\n"; + } } else { @@ -563,9 +637,9 @@ class formfield $res .= ">"; $res .= htmlspecialchars ($this->label); if (isset ($this->mandatory) && $this->mandatory !== FALSE) - $res .= " *"; + $res .= " *"; else - $res .= "  "; + $res .= " "; $res .= "\n"; $res .= "
\n"; $res .= " rows)) $this->rows = 4; $res .= " rows='".$this->rows."'"; + if (isset ($this->errors) || isset ($this->help)) + { + $res .= " aria-describedby='".$this->formName."_"; + $res .= htmlspecialchars ($this->name, ENT_QUOTES)."_help'"; + } $res .= ">"; if (isset ($this->values)) $res .= htmlspecialchars ($this->values, ENT_QUOTES); @@ -591,8 +670,17 @@ class formfield $res .= htmlspecialchars ($this->defaults, ENT_QUOTES); $res .= "\n"; if (isset ($this->errors)) - $res .= " ".$this->errors[1]. - "\n"; + { + $res .= " ". + $this->errors[1]."\n"; + } + elseif (isset ($this->help)) + { + $res .= " ".$this->help + ."\n"; + } $res .= "
\n"; // End controls $res .= "
\n"; // End form-group return $res; @@ -615,9 +703,9 @@ class formfield $res .= ">"; $res .= htmlspecialchars ($this->label); if (isset ($this->mandatory) && $this->mandatory !== FALSE) - $res .= " *"; + $res .= " *"; else - $res .= "  "; + $res .= " "; $res .= "\n"; $res .= "
\n"; $res .= " hidden) && $this->hidden !== FALSE) $res .= " style='display:none'"; + if (isset ($this->cols)) + $res .= " size='".$this->cols."'"; + if (isset ($this->errors) || isset ($this->help)) + { + $res .= " aria-describedby='".$this->formName."_"; + $res .= htmlspecialchars ($this->name, ENT_QUOTES)."_help'"; + } $res .= "/>\n"; if (isset ($this->errors)) - $res .= " ".$this->errors[1]. - "\n"; + { + $res .= " ". + $this->errors[1]."\n"; + } + elseif (isset ($this->help)) + { + $res .= " ".$this->help + ."\n"; + } $res .= "
\n"; // End controls $res .= " \n"; // End form-group return $res;