From 3c0cecd3eb206b28af9b155a434f9ef9c6f7cb43 Mon Sep 17 00:00:00 2001 From: Dominique Fournier Date: Wed, 21 Mar 2018 10:55:06 +0000 Subject: [PATCH] * 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 --- form.php | 314 +++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 236 insertions(+), 78 deletions(-) diff --git a/form.php b/form.php index a0ae83a..dab28e9 100644 --- a/form.php +++ b/form.php @@ -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 .= " \n"; + if ($this->label !== "") + { + $res .= " \n"; + } $res .= "
\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 .= " \n"; + if ($this->label !== "") + { + $res .= " \n"; + } $res .= "
\n"; $res .= " errors)) $res .= " has-".$this->errors[0]; $res .= "'>\n"; - $res .= " \n"; + if ($this->label !== "") + { + $res .= " \n"; + } $res .= "
\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 .= " \n"; + if ($this->label !== "") + { + $res .= " \n"; + } $res .= "
\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 .= "