* 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;
}
/** 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 = "<form action='#' method='$method'";
@@ -204,6 +215,9 @@ class formfield
public $defaults;
/** The type of the field (text, password, checkbox, select)*/
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)*/
public $multiple;
/** The name of group for the fields */
@@ -251,15 +265,16 @@ class formfield
$res .= ">";
$res .= htmlspecialchars ($this->label);
if (isset ($this->mandatory) && $this->mandatory !== FALSE)
$res .= "&nbsp;<span style='color:red'>*</span>";
$res .= " <span style='color:red'>*</span>";
else
$res .= "&nbsp;&nbsp;";
$res .= " ";
$res .= "</label>\n";
$res .= " <div class='col-sm-10'>\n";
if (is_string ($this->defaults))
$this->defaults = array ($this->defaults);
foreach ($this->titles as $key=>$val)
{
$res .= "<div class='checkbox'>";
$res .= " <input type='hidden'";
$res .= " name='$this->formName"."[";
$res .= htmlspecialchars ($this->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 .= "</div>\n";
}
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";
}
$res .= " </div>\n"; // End controls
$res .= " </div>\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 .= "&nbsp;<span style='color:red'>*</span>";
$res .= " <span style='color:red'>*</span>";
else
$res .= "&nbsp;&nbsp;";
$res .= " ";
$res .= "</label>\n";
$res .= " <div class='col-sm-10'>\n";
$res .= " <input type='password'";
@@ -350,10 +379,26 @@ class formfield
$res .= " class='form-control'";
if (isset ($this->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 .= " <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";
}
$res .= " </div>\n"; // End controls
$res .= " </div>\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 .= "&nbsp;<span style='color:red'>*</span>";
$res .= " <span style='color:red'>*</span>";
else
$res .= "&nbsp;&nbsp;";
$res .= " ";
$res .= "</label>\n";
$res .= " <div class='col-sm-10'>\n";
if (is_string ($this->defaults))
@@ -390,7 +435,8 @@ class formfield
$res .= "/>\n";
foreach ($this->titles as $key=>$val)
{
$res .= " <label class='radio'>";
$res .= "<div class='radio'>";
$res .= " <label>";
$res .= "<input type='radio'";
$res .= " name='$this->formName"."[";
$res .= htmlspecialchars ($this->name, ENT_QUOTES)."]'";
@@ -405,17 +451,31 @@ class formfield
elseif (isset ($this->defaults[0]) &&
$this->defaults[0] === $val)
$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";
$res .= "</label>\n"; // End label radio
$res .= "</div>";
}
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";
}
$res .= " </div>\n"; // End controls
$res .= " </div>\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 .= "&nbsp;<span style='color:red'>*</span>";
$res .= " <span style='color:red'>*</span>";
else
$res .= "&nbsp;&nbsp;";
$res .= " ";
$res .= "</label>\n";
$res .= " <div class='col-sm-10'>\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 .= " </select>\n";
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
{
@@ -563,9 +637,9 @@ class formfield
$res .= ">";
$res .= htmlspecialchars ($this->label);
if (isset ($this->mandatory) && $this->mandatory !== FALSE)
$res .= "&nbsp;<span style='color:red'>*</span>";
$res .= " <span style='color:red'>*</span>";
else
$res .= "&nbsp;&nbsp;";
$res .= " ";
$res .= "</label>\n";
$res .= " <div class='col-sm-10'>\n";
$res .= " <textarea";
@@ -584,6 +658,11 @@ class formfield
if (!isset ($this->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 .= "</textarea>\n";
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 form-group
return $res;
@@ -615,9 +703,9 @@ class formfield
$res .= ">";
$res .= htmlspecialchars ($this->label);
if (isset ($this->mandatory) && $this->mandatory !== FALSE)
$res .= "&nbsp;<span style='color:red'>*</span>";
$res .= " <span style='color:red'>*</span>";
else
$res .= "&nbsp;&nbsp;";
$res .= " ";
$res .= "</label>\n";
$res .= " <div class='col-sm-10'>\n";
$res .= " <input type='text'";
@@ -636,10 +724,26 @@ class formfield
$res .= " class='form-control'";
if (isset ($this->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 .= " <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 form-group
return $res;