* form : add setters for fields

* form : The label is now optional
* form : add logging support for exceptions


git-svn-id: https://svn.fournier38.fr/svn/ProgSVN/trunk@4173 bf3deb0d-5f1a-0410-827f-c0cc1f45334c
This commit is contained in:
2018-03-21 10:55:06 +00:00
parent 7c5bdf4cd2
commit 3c0cecd3eb

314
form.php
View File

@@ -38,6 +38,11 @@ class form
/** Define a class for form object */
public $formClass = "form-horizontal";
/** The logging callable method */
private $loggingCallable = null;
/** The logging basemsg */
private $loggingBasemsg = "";
/** Create a form
* @param string|null $formName The form name
*/
@@ -46,6 +51,29 @@ class form
$this->formName = $formName;
}
/** Set logging class an method
* @param callable $loggingCallable The callable function. This method will
* receive two params : the LOG level (LOG_ERROR...) and the message
* @param string|null $basemsg The basemsg added at the beginning of the log
*/
public function logging ($loggingCallable, $loggingBasemsg = "")
{
$this->loggingCallable = $loggingCallable;
$this->loggingBasemsg = $loggingBasemsg;
}
/** The private method to log if the $this->loggingCallable is defined
*/
private function loggingCallable ($prio, $msg)
{
if (! is_callable ($this->loggingCallable))
return;
$base = "";
if ($this->loggingBasemsg !== "")
$base = $this->loggingBasemsg. " ";
call_user_func ($this->loggingCallable, $prio, $base.$msg);
}
/** Save the array of fields into the structure.
* Available :
* - name : name of the field in the HTML page
@@ -106,6 +134,8 @@ class form
}
else
{
$this->loggingCallable (LOG_ERR,
"Unknown FORM method (GET or POST allowed)");
throw new Exception (dgettext("domframework",
"Unknown FORM method (GET or POST allowed)"));
}
@@ -119,7 +149,10 @@ class form
}
catch (Exception $e)
{
throw new Exception ($e->getMessage(), 500);
$this->loggingCallable (LOG_ERR, $e->getMessage ());
throw new Exception (dgettext("domframework",
"Can not read the data from the form : ".
"Expired or missing CSRF Token"), 500);
}
// Remove the field CSRF : can not be used outside the form
unset ($values[$this->csrfField]);
@@ -155,7 +188,11 @@ class form
$errors = array())
{
if (count ($this->fields) === 0)
{
$this->loggingCallable (LOG_ERR,
"Can't display a form without defined field");
throw new Exception ("Can't display a form without defined field", 500);
}
if (isset ($_SESSION))
$_SESSION["domframework"]["form"]["fields"] = $this->fields;
$this->method = strtolower ($method);
@@ -390,9 +427,13 @@ class formfield
public $defaults;
/** The type of the field (text, password, checkbox, select)*/
public $type="text";
/** The state of the field : hidden or show */
public $hidden = false;
/** Allow a help message to be displayed below the field. In case of error,
* it is overrided by the error message */
public $help;
/** Display the placeholder if needed */
public $placeholder = false;
/** The multiplicity of selection of the field (available in select only)*/
public $multiple;
/** The name of group for the fields */
@@ -411,10 +452,11 @@ class formfield
public $titlewidth = 2;
/** The Bootstrap width of the column of fields */
public $fieldwidth = 10;
/** When adding a field, the name and the label are the minimum mandatory
* @param string $name Name of the field
* @param string $label Label of the field */
public function __construct ($name, $label)
* @param string|null $label Label of the field */
public function __construct ($name, $label = "")
{
$this->name = $name;
$this->label = $label;
@@ -427,6 +469,99 @@ class formfield
return $this->$func ();
}
// Setters for all the properties of the class
// {{{
/** Set the type of the field
* @param string $val The value of the type of the field
*/
public function type ($val)
{
$this->type = $val;
return $this;
}
/** Set the hidden of the field
* @param string $val The value of the hidden of the field
*/
public function hidden ($val)
{
$this->type = !! $val;
return $this;
}
/** Set the help of the field
* @param string $val The value of the help of the field
*/
public function help ($val)
{
$this->help = $val;
return $this;
}
/** Set the placeholder
* @param string $val The value of the placeholder
*/
public function placeholder ($val)
{
$this->placeholder = $val;
return $this;
}
/** Set the multiple
* @param string $val The value of the multiple
*/
public function multiple ($val)
{
$this->multiple = $val;
return $this;
}
/** Set the group
* @param string $val The value of the group
*/
public function group ($val)
{
$this->group = $val;
return $this;
}
/** Set the readonly
* @param string $val The value of the readonly
*/
public function readonly ($val)
{
$this->readonly = !! $val;
return $this;
}
/** Set the mandatory
* @param string $val The value of the mandatory
*/
public function mandatory ($val)
{
$this->mandatory = !! $val;
return $this;
}
/** Set the rows
* @param string $val The value of the rows
*/
public function rows ($val)
{
$this->rows = $val;
return $this;
}
/** Set the cols
* @param string $val The value of the cols
*/
public function cols ($val)
{
$this->cols = $val;
return $this;
}
// }}}
/** Return the checkbox defined */
public function fieldcheckbox ()
{
@@ -437,21 +572,24 @@ class formfield
if (isset ($this->errors))
$res .= " has-".$this->errors[0];
$res .= "'>\n";
$res .= " <label class='col-sm-$this->titlewidth control-label' for='".
$this->formName."_";
$res .= htmlspecialchars ($this->name, ENT_QUOTES);
if (count ($this->titles) > 1)
$res .= "_0";
$res .= "'";
if (isset ($this->hidden) && $this->hidden !== FALSE)
$res .= " style='display:none'";
$res .= ">";
$res .= htmlspecialchars ($this->label);
if (isset ($this->mandatory) && $this->mandatory !== FALSE)
$res .= " <span style='color:red'>*</span>";
else
$res .= " ";
$res .= "</label>\n";
if ($this->label !== "")
{
$res .= " <label class='col-sm-$this->titlewidth control-label' for='".
$this->formName."_";
$res .= htmlspecialchars ($this->name, ENT_QUOTES);
if (count ($this->titles) > 1)
$res .= "_0";
$res .= "'";
if (isset ($this->hidden) && $this->hidden !== FALSE)
$res .= " style='display:none'";
$res .= ">";
$res .= htmlspecialchars ($this->label);
if (isset ($this->mandatory) && $this->mandatory !== FALSE)
$res .= " <span style='color:red'>*</span>";
else
$res .= " ";
$res .= "</label>\n";
}
$res .= " <div class='col-sm-$this->fieldwidth'>\n";
if (count ($this->titles) === 0)
$this->titles = array ("");
@@ -574,18 +712,21 @@ class formfield
if (isset ($this->errors))
$res .= " has-".$this->errors[0];
$res .= "'>\n";
$res .= " <label class='col-sm-$this->titlewidth control-label' for='".
$this->formName."_";
$res .= htmlspecialchars ($this->name, ENT_QUOTES)."'";
if (isset ($this->hidden) && $this->hidden !== FALSE)
$res .= " style='display:none'";
$res .= ">";
$res .= htmlspecialchars ($this->label);
if (isset ($this->mandatory) && $this->mandatory !== FALSE)
$res .= " <span style='color:red'>*</span>";
else
$res .= " ";
$res .= "</label>\n";
if ($this->label !== "")
{
$res .= " <label class='col-sm-$this->titlewidth control-label' for='".
$this->formName."_";
$res .= htmlspecialchars ($this->name, ENT_QUOTES)."'";
if (isset ($this->hidden) && $this->hidden !== FALSE)
$res .= " style='display:none'";
$res .= ">";
$res .= htmlspecialchars ($this->label);
if (isset ($this->mandatory) && $this->mandatory !== FALSE)
$res .= " <span style='color:red'>*</span>";
else
$res .= " ";
$res .= "</label>\n";
}
$res .= " <div class='col-sm-$this->fieldwidth'>\n";
$res .= " <input type='password'";
$res .= " name='$this->formName"."[";
@@ -642,18 +783,21 @@ class formfield
if (isset ($this->errors))
$res .= " has-".$this->errors[0];
$res .= "'>\n";
$res .= " <label class='col-sm-$this->titlewidth control-label' for='".
$this->formName."_";
$res .= htmlspecialchars ($this->name, ENT_QUOTES)."_0'";
if (isset ($this->hidden) && $this->hidden !== FALSE)
$res .= " style='display:none'";
$res .= ">";
$res .= htmlspecialchars ($this->label);
if (isset ($this->mandatory) && $this->mandatory !== FALSE)
$res .= " <span style='color:red'>*</span>";
else
$res .= " ";
$res .= "</label>\n";
if ($this->label !== "")
{
$res .= " <label class='col-sm-$this->titlewidth control-label' for='".
$this->formName."_";
$res .= htmlspecialchars ($this->name, ENT_QUOTES)."_0'";
if (isset ($this->hidden) && $this->hidden !== FALSE)
$res .= " style='display:none'";
$res .= ">";
$res .= htmlspecialchars ($this->label);
if (isset ($this->mandatory) && $this->mandatory !== FALSE)
$res .= " <span style='color:red'>*</span>";
else
$res .= " ";
$res .= "</label>\n";
}
$res .= " <div class='col-sm-$this->fieldwidth'>\n";
if (is_string ($this->defaults))
$this->defaults = array ($this->defaults);
@@ -719,18 +863,21 @@ class formfield
if (isset ($this->errors))
$res .= " has-".$this->errors[0];
$res .= "'>\n";
$res .= " <label class='col-sm-$this->titlewidth control-label' for='".
$this->formName."_";
$res .= htmlspecialchars ($this->name, ENT_QUOTES)."'";
if (isset ($this->hidden) && $this->hidden !== FALSE)
$res .= " style='display:none'";
$res .= ">";
$res .= htmlspecialchars ($this->label);
if (isset ($this->mandatory) && $this->mandatory !== FALSE)
$res .= " <span style='color:red'>*</span>";
else
$res .= " ";
$res .= "</label>\n";
if ($this->label !== "")
{
$res .= " <label class='col-sm-$this->titlewidth control-label' for='".
$this->formName."_";
$res .= htmlspecialchars ($this->name, ENT_QUOTES)."'";
if (isset ($this->hidden) && $this->hidden !== FALSE)
$res .= " style='display:none'";
$res .= ">";
$res .= htmlspecialchars ($this->label);
if (isset ($this->mandatory) && $this->mandatory !== FALSE)
$res .= " <span style='color:red'>*</span>";
else
$res .= " ";
$res .= "</label>\n";
}
$res .= " <div class='col-sm-$this->fieldwidth'>\n";
if (isset ($this->defaults) && is_array ($this->defaults))
{
@@ -782,8 +929,13 @@ class formfield
foreach ($this->defaults as $key=>$val)
{
if (! is_string ($val))
{
$this->loggingCallable (LOG_ERR,
"Value as defaut for $this->name::$key is not ".
"a string (".gettype ($val).")");
throw new \Exception ("Value as defaut for $this->name::$key is not ".
"a string (".gettype ($val).")");
}
$res .= " <option value='";
$res .= htmlspecialchars ($key, ENT_QUOTES)."'";
if (isset ($this->values) &&
@@ -875,18 +1027,21 @@ class formfield
if (isset ($this->errors))
$res .= " has-".$this->errors[0];
$res .= "'>\n";
$res .= " <label class='col-sm-$this->titlewidth control-label' for='".
$this->formName."_";
$res .= htmlspecialchars ($this->name, ENT_QUOTES)."'";
if (isset ($this->hidden) && $this->hidden !== FALSE)
$res .= " style='display:none'";
$res .= ">";
$res .= htmlspecialchars ($this->label);
if (isset ($this->mandatory) && $this->mandatory !== FALSE)
$res .= " <span style='color:red'>*</span>";
else
$res .= " ";
$res .= "</label>\n";
if ($this->label !== "")
{
$res .= " <label class='col-sm-$this->titlewidth control-label' for='".
$this->formName."_";
$res .= htmlspecialchars ($this->name, ENT_QUOTES)."'";
if (isset ($this->hidden) && $this->hidden !== FALSE)
$res .= " style='display:none'";
$res .= ">";
$res .= htmlspecialchars ($this->label);
if (isset ($this->mandatory) && $this->mandatory !== FALSE)
$res .= " <span style='color:red'>*</span>";
else
$res .= " ";
$res .= "</label>\n";
}
$res .= " <div class='col-sm-$this->fieldwidth'>\n";
$res .= " <textarea";
$res .= " name='$this->formName"."[";
@@ -945,18 +1100,21 @@ class formfield
if (isset ($this->errors))
$res .= " has-".$this->errors[0];
$res .= "'>\n";
$res .= " <label class='col-sm-$this->titlewidth control-label' for='".
$this->formName."_";
$res .= htmlspecialchars ($this->name, ENT_QUOTES)."'";
if (isset ($this->hidden) && $this->hidden !== FALSE)
$res .= " style='display:none'";
$res .= ">";
$res .= htmlspecialchars ($this->label);
if (isset ($this->mandatory) && $this->mandatory !== FALSE)
$res .= " <span style='color:red'>*</span>";
else
$res .= " ";
$res .= "</label>\n";
if ($this->label !== "")
{
$res .= " <label class='col-sm-$this->titlewidth control-label' for='".
$this->formName."_";
$res .= htmlspecialchars ($this->name, ENT_QUOTES)."'";
if (isset ($this->hidden) && $this->hidden !== FALSE)
$res .= " style='display:none'";
$res .= ">";
$res .= htmlspecialchars ($this->label);
if (isset ($this->mandatory) && $this->mandatory !== FALSE)
$res .= " <span style='color:red'>*</span>";
else
$res .= " ";
$res .= "</label>\n";
}
$res .= " <div class='col-sm-$this->fieldwidth'>\n";
$res .= " <input type='text'";
$res .= " name='$this->formName"."[";