From 49e6128951e8d8b340ea6027735c8b3566c44b6b Mon Sep 17 00:00:00 2001 From: M Date: Thu, 3 Dec 2015 22:11:13 -0500 Subject: + Started Form class definition --- app/class/form.class.php | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 app/class/form.class.php (limited to 'app/class/form.class.php') diff --git a/app/class/form.class.php b/app/class/form.class.php new file mode 100644 index 0000000..e398690 --- /dev/null +++ b/app/class/form.class.php @@ -0,0 +1,35 @@ +textFields = array(); + + $this->errorlist = array(); + $this->warninglist = array(); + $this->noticelist = array(); + } + + /* + * Add new text field to the form + */ + function field_text($name, $req = true) + { + if ($req !== true) + $req = false; + + $this->textFields[] = array( + 'name' => $name, + 'req' => $req + ); + } +} + +?> -- cgit v1.2.3 From 59962f7c260aaa0661b0c811e6b553d1a850032b Mon Sep 17 00:00:00 2001 From: M Date: Sat, 5 Dec 2015 15:03:48 -0500 Subject: + Added numeric and enum types to Form class --- app/class/form.class.php | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'app/class/form.class.php') diff --git a/app/class/form.class.php b/app/class/form.class.php index e398690..ffee3d7 100644 --- a/app/class/form.class.php +++ b/app/class/form.class.php @@ -11,6 +11,8 @@ class Form function __construct() { $this->textFields = array(); + $this->numbFields = array(); + $this->enumFields = array(); $this->errorlist = array(); $this->warninglist = array(); @@ -30,6 +32,41 @@ class Form 'req' => $req ); } + + /* + * Add new numeric field to the form + */ + function field_numeric($name, $req = true, $integer = true, $min = null, $max = null) + { + if ($req !== true) + $req = false; + + if ($integer !== true) + $integer = false; + + $this->numbFields[] = array( + 'name' => $name, + 'req' => $req, + 'int' => $integer, + 'min' => $min, + 'max' => $max + ); + } + + /* + * Add new enumeration field to the form + */ + function field_enum($name, $req = true, $values) + { + if ($req !== true) + $req = false; + + $this->enumFields[] = array( + 'name' => $name, + 'req' => $req, + 'vals' => $values + ); + } } ?> -- cgit v1.2.3 From 91659b121e63735a7620663c0f43f5c5adef77d4 Mon Sep 17 00:00:00 2001 From: M Date: Sat, 5 Dec 2015 18:54:01 -0500 Subject: + Implemented populate function in Form class + Added helper function in Form class, logError ! Finished Form class for now --- app/class/form.class.php | 100 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 97 insertions(+), 3 deletions(-) (limited to 'app/class/form.class.php') diff --git a/app/class/form.class.php b/app/class/form.class.php index ffee3d7..502e348 100644 --- a/app/class/form.class.php +++ b/app/class/form.class.php @@ -14,9 +14,15 @@ class Form $this->numbFields = array(); $this->enumFields = array(); - $this->errorlist = array(); - $this->warninglist = array(); - $this->noticelist = array(); + $this->errorlist = array(); + } + + /* + * Log an error + */ + function logError($str) + { + $this->errorlist[] = $str; } /* @@ -67,6 +73,94 @@ class Form 'vals' => $values ); } + + /* + * Populate the form with input data from web page + */ + function populate($input) + { + /* detect duplicate names */ + $names = array(); + foreach ($this->textFields as $fld) + $names[] = $fld['name']; + foreach ($this->numbFields as $fld) + $names[] = $fld['name']; + foreach ($this->enumFields as $fld) + $names[] = $fld['name']; + + if (count(array_unique($names)) != count($names)) + { + $this->logError("Internal error: Duplicate field names defined in form"); + return false; + } + + /* init text fields */ + foreach ($this->textFields as $fld) + { + if (isset($input[$fld['name']])) + $this->$fld['name'] = htmlEntities($input[$fld['name']], ENT_QUOTES); + + else if ($fld['req']) + $this->logError($fld['name'] . " is required"); + } + + /* init numeric fields */ + foreach ($this->numbFields as $fld) + { + if (isset($input[$fld['name']])) + { + if (!is_numeric($input[$fld['name']])) + { + $this->logError($fld['name'] . " must be numeric"); + continue; + } + + if ($fld['int'] && (floor($input[$fld['name']]) != $input[$fld['name']])) + { + $this->logError($fld['name'] . " must be an integer"); + continue; + } + + if (!is_null($fld['min']) && ($input[$fld['name']] < $fld['min'])) + { + $this->logError($fld['name'] . " must be no less than " . $fld['min']); + continue; + } + + if (!is_null($fld['max']) && ($input[$fld['name']] > $fld['max'])) + { + $this->logError($fld['name'] . " must be no more than " . $fld['max']); + continue; + } + + $this->$fld['name'] = $input[$fld['name']]; + } + + else if ($fld['req']) + $this->logError($fld['name'] . " is required"); + } + + /* init enum fields */ + foreach ($this->enumFields as $fld) + { + if (isset($input[$fld['name']])) + { + if (array_search($input[$fld['name']], $fld['vals']) === false) + { + $this->logError($fld['name'] . " is not an appropriate value"); + continue; + } + + $this->$fld['name'] = $input[$fld['name']]; + } + + else if ($fld['req']) + $this->logError($fld['name'] . " is required"); + } + + /* return */ + return count($this->errorlist) == 0; + } } ?> -- cgit v1.2.3 From 9bab1e5c3d7dae9603c5f2172b2a620465caab0e Mon Sep 17 00:00:00 2001 From: M Date: Sat, 5 Dec 2015 21:37:03 -0500 Subject: * Form class fields now have the ability to set a default value. Default value is applied if the supplied $input array has no key matching the field name. --- app/class/form.class.php | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) (limited to 'app/class/form.class.php') diff --git a/app/class/form.class.php b/app/class/form.class.php index 502e348..e50876d 100644 --- a/app/class/form.class.php +++ b/app/class/form.class.php @@ -28,21 +28,22 @@ class Form /* * Add new text field to the form */ - function field_text($name, $req = true) + function field_text($name, $deflt = null, $req = true) { if ($req !== true) $req = false; $this->textFields[] = array( - 'name' => $name, - 'req' => $req + 'name' => $name, + 'deflt' => $deflt, + 'req' => $req ); } /* * Add new numeric field to the form */ - function field_numeric($name, $req = true, $integer = true, $min = null, $max = null) + function field_numeric($name, $min = null, $max = null, $deflt = null, $integer = true, $req = true) { if ($req !== true) $req = false; @@ -51,26 +52,28 @@ class Form $integer = false; $this->numbFields[] = array( - 'name' => $name, - 'req' => $req, - 'int' => $integer, - 'min' => $min, - 'max' => $max + 'name' => $name, + 'min' => $min, + 'max' => $max, + 'deflt' => $deflt, + 'int' => $integer, + 'req' => $req ); } /* * Add new enumeration field to the form */ - function field_enum($name, $req = true, $values) + function field_enum($name, $values, $deflt = null, $req = true) { if ($req !== true) $req = false; $this->enumFields[] = array( - 'name' => $name, - 'req' => $req, - 'vals' => $values + 'name' => $name, + 'vals' => $values, + 'deflt' => $deflt, + 'req' => $req ); } @@ -100,6 +103,9 @@ class Form if (isset($input[$fld['name']])) $this->$fld['name'] = htmlEntities($input[$fld['name']], ENT_QUOTES); + else if (!is_null($fld['deflt'])) + $this->$fld['name'] = $fld['deflt']; + else if ($fld['req']) $this->logError($fld['name'] . " is required"); } @@ -136,6 +142,9 @@ class Form $this->$fld['name'] = $input[$fld['name']]; } + else if (!is_null($fld['deflt'])) + $this->$fld['name'] = $fld['deflt']; + else if ($fld['req']) $this->logError($fld['name'] . " is required"); } @@ -154,6 +163,9 @@ class Form $this->$fld['name'] = $input[$fld['name']]; } + else if (!is_null($fld['deflt'])) + $this->$fld['name'] = $fld['deflt']; + else if ($fld['req']) $this->logError($fld['name'] . " is required"); } -- cgit v1.2.3 From e6f3bf746fbb1d4c768a1d43e2a0233d0fb25f47 Mon Sep 17 00:00:00 2001 From: M Date: Sun, 6 Dec 2015 00:12:16 -0500 Subject: * Bug fix in Form class - populate function -- If a field was set in $input, but equal to "", the isset check would not behave as expected --- app/class/form.class.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'app/class/form.class.php') diff --git a/app/class/form.class.php b/app/class/form.class.php index e50876d..d3af399 100644 --- a/app/class/form.class.php +++ b/app/class/form.class.php @@ -100,7 +100,7 @@ class Form /* init text fields */ foreach ($this->textFields as $fld) { - if (isset($input[$fld['name']])) + if (isset($input[$fld['name']]) && $input[$fld['name']] != "") $this->$fld['name'] = htmlEntities($input[$fld['name']], ENT_QUOTES); else if (!is_null($fld['deflt'])) @@ -113,7 +113,7 @@ class Form /* init numeric fields */ foreach ($this->numbFields as $fld) { - if (isset($input[$fld['name']])) + if (isset($input[$fld['name']]) && $input[$fld['name']] != "") { if (!is_numeric($input[$fld['name']])) { @@ -152,7 +152,7 @@ class Form /* init enum fields */ foreach ($this->enumFields as $fld) { - if (isset($input[$fld['name']])) + if (isset($input[$fld['name']]) && $input[$fld['name']] != "") { if (array_search($input[$fld['name']], $fld['vals']) === false) { -- cgit v1.2.3 From 2896ade5e1257045513f871d59e6e4eaac27e317 Mon Sep 17 00:00:00 2001 From: M Date: Tue, 8 Dec 2015 18:51:20 -0500 Subject: + Added bool field type to Form class --- app/class/form.class.php | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'app/class/form.class.php') diff --git a/app/class/form.class.php b/app/class/form.class.php index d3af399..808de27 100644 --- a/app/class/form.class.php +++ b/app/class/form.class.php @@ -77,6 +77,14 @@ class Form ); } + /* + * Add new boolean field to the form + */ + function field_bool($name) + { + $this->field_enum($name, array("true", "false"), "false"); + } + /* * Populate the form with input data from web page */ -- cgit v1.2.3