* form : don't allow to display a form without defined fields

* form : add a "addfield" method to add a field in form
  * form : remove the   in labels to be W3C compliant
  * form : rework the checkbox/radio to be compliant with bootstrap and ARIA
  * form : add the help support (which is hidden when there is an error)


git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@2207 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2015-08-05 11:48:20 +00:00
parent b18af8497f
commit 59ae5b8e28

184
form.php
View File

@@ -34,23 +34,25 @@ class form
$this->formName = $formName; $this->formName = $formName;
} }
/** Save the fields into the structure. /** Save the array of fields into the structure.
Available : Available :
- name : name of the field in the HTML page - name : name of the field in the HTML page
- label : label written to the describe the field - label : label written to the describe the field
- [titles] : text written in radio/checkboxes - [titles] : text written in radio/checkboxes
- [defaults] : default values. Must be array for checkbox/select, and - [defaults] : default values. Must be array for checkbox/select, and
string for others string for others
- [type] : text, password, hidden, checkbox, select, radio, submit, - [type] : text, password, hidden, checkbox, select, radio, submit,
textarea textarea
text by default text by default
- [multiple] : Multiple selection are possible (if the type supports it) - [help] : The Help message (written below the field). Overwrite in
- [group] : define a fieldset and define the title with groupe name case of error
Warning : all the elements of the same group must be - [multiple] : Multiple selection are possible (if the type supports it)
consecutive ! - [group] : define a fieldset and define the title with groupe name
- [readonly] : put a read-only flag on the field (the user see it but Warning : all the elements of the same group must be
can't interract on it. The value will be sent to next consecutive !
page - [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 - [mandatory] : boolean to add a red star at end of label
- [hidden] : hide the field (add a style='display:hidden' to the field) - [hidden] : hide the field (add a style='display:hidden' to the field)
- [rows] : Number of rows - [rows] : Number of rows
@@ -63,6 +65,13 @@ class form
$this->fields = $fields; $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 /** 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 NEVER read the values from $_POST in your codes or CSRF will not be
checked */ checked */
@@ -129,6 +138,8 @@ class form
public function printHTML ($method = 'post', $values = NULL, public function printHTML ($method = 'post', $values = NULL,
$errors = array()) $errors = array())
{ {
if (count ($this->fields) === 0)
throw new Exception ("Can't display a form without defined field", 500);
$this->method = strtolower ($method); $this->method = strtolower ($method);
$res = ""; $res = "";
$res = "<form action='#' method='$method'"; $res = "<form action='#' method='$method'";
@@ -204,6 +215,9 @@ class formfield
public $defaults; public $defaults;
/** The type of the field (text, password, checkbox, select)*/ /** The type of the field (text, password, checkbox, select)*/
public $type="text"; public $type="text";
/** Allow a help message to be displayed below the field. In case of error,
it is override by the error message */
public $help;
/** The multiplicity of selection of the field (available in select only)*/ /** The multiplicity of selection of the field (available in select only)*/
public $multiple; public $multiple;
/** The name of group for the fields */ /** The name of group for the fields */
@@ -251,15 +265,16 @@ class formfield
$res .= ">"; $res .= ">";
$res .= htmlspecialchars ($this->label); $res .= htmlspecialchars ($this->label);
if (isset ($this->mandatory) && $this->mandatory !== FALSE) if (isset ($this->mandatory) && $this->mandatory !== FALSE)
$res .= "&nbsp;<span style='color:red'>*</span>"; $res .= " <span style='color:red'>*</span>";
else else
$res .= "&nbsp;&nbsp;"; $res .= " ";
$res .= "</label>\n"; $res .= "</label>\n";
$res .= " <div class='col-sm-10'>\n"; $res .= " <div class='col-sm-10'>\n";
if (is_string ($this->defaults)) if (is_string ($this->defaults))
$this->defaults = array ($this->defaults); $this->defaults = array ($this->defaults);
foreach ($this->titles as $key=>$val) foreach ($this->titles as $key=>$val)
{ {
$res .= "<div class='checkbox'>";
$res .= " <input type='hidden'"; $res .= " <input type='hidden'";
$res .= " name='$this->formName"."["; $res .= " name='$this->formName"."[";
$res .= htmlspecialchars ($this->name, ENT_QUOTES)."][$key]'"; $res .= htmlspecialchars ($this->name, ENT_QUOTES)."][$key]'";
@@ -278,16 +293,30 @@ class formfield
elseif (isset ($this->defaults[$key]) && elseif (isset ($this->defaults[$key]) &&
$this->defaults[$key] !== "") $this->defaults[$key] !== "")
$res .= " checked='checked'"; $res .= " checked='checked'";
$res .= " class='form-control'";
if (isset ($this->hidden) && $this->hidden !== FALSE) if (isset ($this->hidden) && $this->hidden !== FALSE)
$res .= " style='display:none'"; $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 .= "/>";
$res .= "$val\n"; $res .= "$val\n";
$res .= "</div>\n";
} }
if (isset ($this->errors)) if (isset ($this->errors))
$res .= " <span class='help-block'>".$this->errors[1]. {
$res .= " <span class='help-block' id='$this->formName"."_";
$res .= htmlspecialchars ($this->name, ENT_QUOTES)."_help'>".
$this->errors[1]."</span>\n";
}
elseif (isset ($this->help))
{
$res .= " <span class='help-block' id='$this->formName"."_";
$res .= htmlspecialchars ($this->name, ENT_QUOTES)."_help'>".$this->help.
"</span>\n"; "</span>\n";
}
$res .= " </div>\n"; // End controls $res .= " </div>\n"; // End controls
$res .= " </div>\n"; // End form-group $res .= " </div>\n"; // End form-group
return $res; return $res;
@@ -329,9 +358,9 @@ class formfield
$res .= ">"; $res .= ">";
$res .= htmlspecialchars ($this->label); $res .= htmlspecialchars ($this->label);
if (isset ($this->mandatory) && $this->mandatory !== FALSE) if (isset ($this->mandatory) && $this->mandatory !== FALSE)
$res .= "&nbsp;<span style='color:red'>*</span>"; $res .= " <span style='color:red'>*</span>";
else else
$res .= "&nbsp;&nbsp;"; $res .= " ";
$res .= "</label>\n"; $res .= "</label>\n";
$res .= " <div class='col-sm-10'>\n"; $res .= " <div class='col-sm-10'>\n";
$res .= " <input type='password'"; $res .= " <input type='password'";
@@ -350,10 +379,26 @@ class formfield
$res .= " class='form-control'"; $res .= " class='form-control'";
if (isset ($this->hidden) && $this->hidden !== FALSE) if (isset ($this->hidden) && $this->hidden !== FALSE)
$res .= " style='display:none'"; $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"; $res .= "/>\n";
if (isset ($this->errors)) if (isset ($this->errors))
$res .= " <span class='help-block'>".$this->errors[1]. {
$res .= " <span class='help-block' id='$this->formName"."_";
$res .= htmlspecialchars ($this->name, ENT_QUOTES)."_help'>".
$this->errors[1]."</span>\n";
}
elseif (isset ($this->help))
{
$res .= " <span class='help-block' id='$this->formName"."_";
$res .= htmlspecialchars ($this->name, ENT_QUOTES)."_help'>".$this->help.
"</span>\n"; "</span>\n";
}
$res .= " </div>\n"; // End controls $res .= " </div>\n"; // End controls
$res .= " </div>\n"; // End form-group $res .= " </div>\n"; // End form-group
return $res; return $res;
@@ -376,9 +421,9 @@ class formfield
$res .= ">"; $res .= ">";
$res .= htmlspecialchars ($this->label); $res .= htmlspecialchars ($this->label);
if (isset ($this->mandatory) && $this->mandatory !== FALSE) if (isset ($this->mandatory) && $this->mandatory !== FALSE)
$res .= "&nbsp;<span style='color:red'>*</span>"; $res .= " <span style='color:red'>*</span>";
else else
$res .= "&nbsp;&nbsp;"; $res .= " ";
$res .= "</label>\n"; $res .= "</label>\n";
$res .= " <div class='col-sm-10'>\n"; $res .= " <div class='col-sm-10'>\n";
if (is_string ($this->defaults)) if (is_string ($this->defaults))
@@ -390,7 +435,8 @@ class formfield
$res .= "/>\n"; $res .= "/>\n";
foreach ($this->titles as $key=>$val) foreach ($this->titles as $key=>$val)
{ {
$res .= " <label class='radio'>"; $res .= "<div class='radio'>";
$res .= " <label>";
$res .= "<input type='radio'"; $res .= "<input type='radio'";
$res .= " name='$this->formName"."["; $res .= " name='$this->formName"."[";
$res .= htmlspecialchars ($this->name, ENT_QUOTES)."]'"; $res .= htmlspecialchars ($this->name, ENT_QUOTES)."]'";
@@ -405,17 +451,31 @@ class formfield
elseif (isset ($this->defaults[0]) && elseif (isset ($this->defaults[0]) &&
$this->defaults[0] === $val) $this->defaults[0] === $val)
$res .= " checked='checked'"; $res .= " checked='checked'";
$res .= " class='form-control'";
if (isset ($this->hidden) && $this->hidden !== FALSE) if (isset ($this->hidden) && $this->hidden !== FALSE)
$res .= " style='display:none'"; $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 .= "/>";
$res .= "$val"; $res .= "$val";
$res .= "</label>\n"; // End label radio $res .= "</label>\n"; // End label radio
$res .= "</div>";
} }
if (isset ($this->errors)) if (isset ($this->errors))
$res .= " <span class='help-block'>".$this->errors[1]. {
$res .= " <span class='help-block' id='$this->formName"."_";
$res .= htmlspecialchars ($this->name, ENT_QUOTES)."_help'>".
$this->errors[1]."</span>\n";
}
elseif (isset ($this->help))
{
$res .= " <span class='help-block' id='$this->formName"."_";
$res .= htmlspecialchars ($this->name, ENT_QUOTES)."_help'>".$this->help.
"</span>\n"; "</span>\n";
}
$res .= " </div>\n"; // End controls $res .= " </div>\n"; // End controls
$res .= " </div>\n"; // End form-group $res .= " </div>\n"; // End form-group
return $res; return $res;
@@ -438,9 +498,9 @@ class formfield
$res .= ">"; $res .= ">";
$res .= htmlspecialchars ($this->label); $res .= htmlspecialchars ($this->label);
if (isset ($this->mandatory) && $this->mandatory !== FALSE) if (isset ($this->mandatory) && $this->mandatory !== FALSE)
$res .= "&nbsp;<span style='color:red'>*</span>"; $res .= " <span style='color:red'>*</span>";
else else
$res .= "&nbsp;&nbsp;"; $res .= " ";
$res .= "</label>\n"; $res .= "</label>\n";
$res .= " <div class='col-sm-10'>\n"; $res .= " <div class='col-sm-10'>\n";
if (isset ($this->defaults) && is_array ($this->defaults)) if (isset ($this->defaults) && is_array ($this->defaults))
@@ -484,6 +544,11 @@ class formfield
$res .= " class='form-control'"; $res .= " class='form-control'";
if (isset ($this->rows)) if (isset ($this->rows))
$res .= " size='".$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"; $res .= ">\n";
foreach ($this->defaults as $key=>$val) foreach ($this->defaults as $key=>$val)
{ {
@@ -504,8 +569,17 @@ class formfield
$res .= " </select>\n"; $res .= " </select>\n";
if (isset ($this->errors)) if (isset ($this->errors))
$res .= " <span class='help-block'>".$this->errors[1]. {
"</span>\n"; $res .= " <span class='help-block' id='$this->formName"."_";
$res .= htmlspecialchars ($this->name, ENT_QUOTES)."_help'>".
$this->errors[1]."</span>\n";
}
elseif (isset ($this->help))
{
$res .= " <span class='help-block' id='$this->formName"."_";
$res .= htmlspecialchars ($this->name, ENT_QUOTES)."_help'>".$this->help
."</span>\n";
}
} }
else else
{ {
@@ -563,9 +637,9 @@ class formfield
$res .= ">"; $res .= ">";
$res .= htmlspecialchars ($this->label); $res .= htmlspecialchars ($this->label);
if (isset ($this->mandatory) && $this->mandatory !== FALSE) if (isset ($this->mandatory) && $this->mandatory !== FALSE)
$res .= "&nbsp;<span style='color:red'>*</span>"; $res .= " <span style='color:red'>*</span>";
else else
$res .= "&nbsp;&nbsp;"; $res .= " ";
$res .= "</label>\n"; $res .= "</label>\n";
$res .= " <div class='col-sm-10'>\n"; $res .= " <div class='col-sm-10'>\n";
$res .= " <textarea"; $res .= " <textarea";
@@ -584,6 +658,11 @@ class formfield
if (!isset ($this->rows)) if (!isset ($this->rows))
$this->rows = 4; $this->rows = 4;
$res .= " rows='".$this->rows."'"; $res .= " rows='".$this->rows."'";
if (isset ($this->errors) || isset ($this->help))
{
$res .= " aria-describedby='".$this->formName."_";
$res .= htmlspecialchars ($this->name, ENT_QUOTES)."_help'";
}
$res .= ">"; $res .= ">";
if (isset ($this->values)) if (isset ($this->values))
$res .= htmlspecialchars ($this->values, ENT_QUOTES); $res .= htmlspecialchars ($this->values, ENT_QUOTES);
@@ -591,8 +670,17 @@ class formfield
$res .= htmlspecialchars ($this->defaults, ENT_QUOTES); $res .= htmlspecialchars ($this->defaults, ENT_QUOTES);
$res .= "</textarea>\n"; $res .= "</textarea>\n";
if (isset ($this->errors)) if (isset ($this->errors))
$res .= " <span class='help-block'>".$this->errors[1]. {
"</span>\n"; $res .= " <span class='help-block' id='$this->formName"."_";
$res .= htmlspecialchars ($this->name, ENT_QUOTES)."_help'>".
$this->errors[1]."</span>\n";
}
elseif (isset ($this->help))
{
$res .= " <span class='help-block' id='$this->formName"."_";
$res .= htmlspecialchars ($this->name, ENT_QUOTES)."_help'>".$this->help
."</span>\n";
}
$res .= " </div>\n"; // End controls $res .= " </div>\n"; // End controls
$res .= " </div>\n"; // End form-group $res .= " </div>\n"; // End form-group
return $res; return $res;
@@ -615,9 +703,9 @@ class formfield
$res .= ">"; $res .= ">";
$res .= htmlspecialchars ($this->label); $res .= htmlspecialchars ($this->label);
if (isset ($this->mandatory) && $this->mandatory !== FALSE) if (isset ($this->mandatory) && $this->mandatory !== FALSE)
$res .= "&nbsp;<span style='color:red'>*</span>"; $res .= " <span style='color:red'>*</span>";
else else
$res .= "&nbsp;&nbsp;"; $res .= " ";
$res .= "</label>\n"; $res .= "</label>\n";
$res .= " <div class='col-sm-10'>\n"; $res .= " <div class='col-sm-10'>\n";
$res .= " <input type='text'"; $res .= " <input type='text'";
@@ -636,10 +724,26 @@ class formfield
$res .= " class='form-control'"; $res .= " class='form-control'";
if (isset ($this->hidden) && $this->hidden !== FALSE) if (isset ($this->hidden) && $this->hidden !== FALSE)
$res .= " style='display:none'"; $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"; $res .= "/>\n";
if (isset ($this->errors)) if (isset ($this->errors))
$res .= " <span class='help-block'>".$this->errors[1]. {
"</span>\n"; $res .= " <span class='help-block' id='$this->formName"."_";
$res .= htmlspecialchars ($this->name, ENT_QUOTES)."_help'>".
$this->errors[1]."</span>\n";
}
elseif (isset ($this->help))
{
$res .= " <span class='help-block' id='$this->formName"."_";
$res .= htmlspecialchars ($this->name, ENT_QUOTES)."_help'>".$this->help
."</span>\n";
}
$res .= " </div>\n"; // End controls $res .= " </div>\n"; // End controls
$res .= " </div>\n"; // End form-group $res .= " </div>\n"; // End form-group
return $res; return $res;