Date: 01/01/01
- Next message: uw: "[phplib-dev] cvs commit"
- Next in thread: uw: "[phplib-dev] cvs commit"
- Maybe reply: uw: "[phplib-dev] cvs commit"
- Maybe reply: uw: "[phplib-dev] cvs commit"
- Maybe reply: uw: "[phplib-dev] cvs commit"
- Maybe reply: uw: "[phplib-dev] cvs commit"
- Maybe reply: uw: "[phplib-dev] cvs commit"
- Maybe reply: uw: "[phplib-dev] cvs commit"
- Maybe reply: max: "[phplib-dev] cvs commit"
- Maybe reply: max: "[phplib-dev] cvs commit"
- Maybe reply: max: "[phplib-dev] cvs commit"
- Maybe reply: max: "[phplib-dev] cvs commit"
- Maybe reply: max: "[phplib-dev] cvs commit"
- Maybe reply: max: "[phplib-dev] cvs commit"
- Maybe reply: uw: "[phplib-dev] cvs commit"
- Maybe reply: uw: "[phplib-dev] cvs commit"
- Maybe reply: uw: "[phplib-dev] cvs commit"
- Maybe reply: uw: "[phplib-dev] cvs commit"
- Maybe reply: uw: "[phplib-dev] cvs commit"
- Maybe reply: uw: "[phplib-dev] cvs commit"
- Maybe reply: uw: "[phplib-dev] cvs commit"
- Maybe reply: uw: "[phplib-dev] cvs commit"
- Maybe reply: uw: "[phplib-dev] cvs commit"
- Maybe reply: uw: "[phplib-dev] cvs commit"
- Maybe reply: uw: "[phplib-dev] cvs commit"
- Maybe reply: uw: "[phplib-dev] cvs commit"
- Maybe reply: uw: "[phplib-dev] cvs commit"
- Maybe reply: max: "[phplib-dev] cvs commit"
- Maybe reply: uw: "[phplib-dev] cvs commit"
- Maybe reply: max: "[phplib-dev] cvs commit"
- Maybe reply: uw: "[phplib-dev] cvs commit"
- Maybe reply: uw: "[phplib-dev] cvs commit"
- Maybe reply: uw: "[phplib-dev] cvs commit"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
From: uw
Date: Mon Jan 1 15:51:36 2001
Added files:
php-lib/php/form/form_js_complex.js
php-lib/php/form/form_js_simple.js
Removed files:
php-lib/php/form/form_baseobject.inc
php-lib/php/form/form_copiedapi.inc
Modified files:
php-lib/php/form/form.inc
php-lib/php/form/form_element.inc
php-lib/php/form/form_element_buttonobject.inc
php-lib/php/form/form_element_checkbox.inc
php-lib/php/form/form_element_checkobject.inc
php-lib/php/form/form_element_combo.inc
php-lib/php/form/form_element_date.inc
php-lib/php/form/form_element_file.inc
php-lib/php/form/form_element_fileupload.inc
php-lib/php/form/form_element_hidden.inc
php-lib/php/form/form_element_image.inc
php-lib/php/form/form_element_password.inc
php-lib/php/form/form_element_radio.inc
php-lib/php/form/form_element_reset.inc
php-lib/php/form/form_element_select.inc
php-lib/php/form/form_element_selectobject.inc
php-lib/php/form/form_element_submit.inc
php-lib/php/form/form_element_text.inc
php-lib/php/form/form_element_textarea.inc
php-lib/php/form/form_element_textobject.inc
php-lib/php/form/form_element_tree.inc
php-lib/php/form/form_error.inc
php-lib/php/form/form_loader.inc
php-lib/php/form/template_bridge.inc
Log message:
- refined the doc comments
- the PHPDoc report now has 1/5 of it's original size
- implemented JavaScript client side validation
- supposed to be the final structure
- beautified the class hierarchie
Index: php-lib/php/form/form.inc
diff -u php-lib/php/form/form.inc:1.14 php-lib/php/form/form.inc:1.15
--- php-lib/php/form/form.inc:1.14 Tue Dec 19 11:39:49 2000
+++ php-lib/php/form/form.inc Mon Jan 1 15:50:45 2001
@@ -1,17 +1,196 @@
<?php
/**
-* Public methods of the form class.
+* Object oriented HTML form generation and validation.
*
+* The form classes provide a way to generate and validate a HTML
+* form without the need to write a single line of HTML. Serverside and clientside
+* (JavaScript1.2) validation is done automatically. This code is the
+* successor of the OOH Forms. See pages/form/ for examples.
+*
+* A form consists of an object of this class which controls lots of form elements.
+* Form known all HTML form element types plus some compound types:
+*
+* - button
+* - checkbox
+* - combo (text + select box)
+* - date (lots of select boxes)
+* - file
+* - hidden
+* - image
+* - password
+* - radio
+* - select box
+* - submit
+* - text
+* - textarea
+* - tree (select box with options show as a tree)
+*
* <email protected> Ulf Wendel <ulf.wendel <email protected>>
-* <email protected> $Id: form.inc,v 1.14 2000/12/19 10:39:49 uw Exp $
+* <email protected> $Id: form.inc,v 1.15 2001/01/01 14:50:45 uw Exp $
+* <email protected> public
* <email protected> Form
*/
-class form extends form_copiedapi {
+class form extends form_commonobject {
+
+ /**
+ * Some default values.
+ *
+ * <email protected> array $defaults
+ * <email protected> setDefaults()
+ */
+ var $defaults = array(
+ # text element defaults
+ "textsize" => 0,
+ "textmaxlength" => 0,
+
+ # file element defaults
+ "filesize" => 0,
+
+ # image element defaults
+ "imageborder" => 0,
+
+ # select objects
+ "selectintro" => "",
+ "selectintro_e" => "",
+
+ # HTML CSS defaults
+ "cssid" => "",
+ "cssclass" => "",
+ "cssstyle" => ""
+ );
+
+ /**
+ * Array of all elements.
+ *
+ * <email protected> $elements array
+ */
+ var $elements = array();
+
+ /**
+ * List of all element names.
+ *
+ * <email protected> array
+ */
+ var $element_names = array();
+
+ /**
+ * Global element name prefix.
+ *
+ * In multipage forms a global element name prefix can be
+ * used to avoid name clashes.
+ *
+ * <email protected> string
+ * <email protected> setElementnamePrefix()
+ */
+ var $element_prefix = "";
+
+ /**
+ * Results of the last validation.
+ *
+ * <email protected> array Format - $validation_flags[ $name ] = $message
+ * <email protected> validate(), setValidationError(), getValidationResult(), $validation_msg
+ */
+ var $validation_flags = array();
+
+ /**
+ * String with all error messages.
+ *
+ * <email protected> string $validation_msg
+ * <email protected> validate(), setValidationError(), getValidationResult(), $validation_flags
+ */
+ var $validation_msg = "";
+
+ /**
+ * Array of hidden elements.
+ *
+ * Hidden elements are treated different than in the old oohforms. Now you can
+ * give a hidden element an html name (attribute name) and an alias name (elname)
+ * to access the element. That caused the introduction of some translation
+ * tables.
+ *
+ * <email protected> array
+ */
+ var $hidden_elements = array();
+
+ /**
+ * Array of radio elements.
+ *
+ * <email protected> array $radio_elements
+ */
+ var $radio_elements = array();
+
+ /**
+ * HTML name attribute of the opening form Tag.
+ *
+ * If no name is given no JavaScript validation will be done.
+ *
+ * <email protected> string
+ */
+ var $js_name = "";
+
+ /**
+ * JavaScript validation mode
+ *
+ * Form knows two kinds of JavaScript validation: "strong" and "weak".
+ * "strong" means that validation will be performed onChange/onClick,
+ * "weak" stands for onSubmit.
+ *
+ * <email protected> string "strong" or "weak"
+ */
+ var $js_mode = "strong";
+
+ /**
+ * JavaScript error message prefix.
+ *
+ * Prefix displayed in front of the errors in a JavaScript alert box.
+ *
+ * <email protected> string
+ * <email protected> $js_error_postfix, setJSError()
+ */
+ var $js_error_prefix = "There're errors in the form: \n\n";
+
+ /**
+ * JavaScript error message postfix.
+ *
+ * Postfix displayed right after the errors in a JavaScript alert box.
+ *
+ * <email protected> string
+ * <email protected> $js_error_prefix, setJSError()
+ */
+ var $js_error_postfix = "\n\nPlease correct the input.";
+
+ /**
+ * Method used in the form.
+ *
+ * <email protected> string $method Default is POST-
+ * <email protected> Start(), autoloadValues()
+ */
+ var $method = "POST";
+
+ /**
+ * Flag indication that you're using the Form Loader.
+ *
+ * addElement() will call form_elements_loader() if the flag is set to true.
+ * It's strongly recommended that you use the Form Loader unless you're
+ * using the Zend Cache or a similar product.
+ *
+ * <email protected> boolean
+ * <email protected> public
+ */
+ var $flag_loader = true;
+
+ /**
+ * Flag indicating that a file upload button is in the form.
+ *
+ * <email protected> boolean
+ */
+ var $flag_contains_file = false;
/**
* Sets the JavaScript validation mode.
*
- * <email protected> $js_mode = "weak"
+ * <email protected> string "weak" or "strong", if "strong" validation will be done onChange/onClick
+ * otherwise only onSubmit.
* <email protected> public
*/
function setJSMode($js_mode = "weak") {
@@ -21,37 +200,20 @@
/**
* Sets the method used by this form.
*
- * <email protected> string $method
+ * <email protected> string "POST" or "GET"
* <email protected> public
- * <email protected> autoloadValues(), method
+ * <email protected> autoloadValues(), $method
*/
function setMethod($method = "POST") {
$this->method = ("GET" == strtoupper($method)) ? "GET" : "POST";
} // end func setMethod
/**
- * Sets a global Prefix for element names.
- *
- * <email protected> string
- * <email protected> boolean
- * <email protected> $element_prefix
- */
- function setElementnamePrefix($prefix) {
- if ("" == $prefix) {
- $this->exeptions[] = new form_error("No prefix given", __FILE__, __LINE__);
- return false;
- }
-
- $this->element_prefix = $prefix;
- return true;
- } // end func setElementnamePrefix
-
-
- /**
- * Loads the form with HTTP_[POST|GET]_VARS
+ * Loads the form with HTTP_[POST|GET]_VARS.
*
- * <email protected> array $ellist
- * <email protected> bool $ok
+ * <email protected> mixed Name of one element or array of elements. An empty string
+ * causes a fallback to all elements.
+ * <email protected> boolean
* <email protected> public
*/
function autoloadValues($ellist = "") {
@@ -115,7 +277,7 @@
if (isset($this->elements[$name])) {
$this->elements[$name]->setValue($value);
-
+
} else if (isset($this->radio_elements[$name])) {
$elements = $this->radio_elements[$name];
@@ -132,9 +294,9 @@
/**
* Sets the value of one element or a group of elements.
*
- * <email protected> array $value Array of values, $values[ name ] = value
- * <email protected> boolean $ok
- * <email protected> FormError
+ * <email protected> array Array of values, $values[ name ] = value
+ * <email protected> boolean
+ * <email protected> form_error
* <email protected> public
*/
function setValues($values) {
@@ -148,7 +310,7 @@
if (isset($this->elements[$name])) {
$this->elements[$name]->setValue($value);
-
+
} else if (isset($this->radio_elements[$name])) {
foreach ($this->radio_elements[$name] as $elname => $v)
@@ -201,9 +363,8 @@
* Public handle to turn default validation off.
*
* <email protected> mixed $ellist Name of one element or array of elements.
- * <email protected> Validation
* <email protected> public
- * <email protected> ValidationOn()
+ * <email protected> Validation()
*/
function ValidationOff($ellist) {
return $this->Validation($ellist, false);
@@ -212,9 +373,7 @@
/**
* Public handle to turn default validation on.
*
- * <email protected> mixed $ellist Name of one element or array of elements.
- * <email protected> Validation
- * <email protected> ValidationOff()
+ * <email protected> ValidationOff()
* <email protected> public
*/
function ValidationOn($ellist) {
@@ -226,7 +385,7 @@
*
* <email protected> string $name element name
* <email protected> string $validator name of validation function
- * <email protected> FormError
+ * <email protected> form_error
* <email protected> boolean
*/
function setValidator($name, $validator) {
@@ -248,15 +407,15 @@
/**
* Generates the opening HTML form tag, <form>.
*
- * <email protected> string $js_name = ""
- * <email protected> enum(POST|GET) $method = "POST"
- * <email protected> string $action = ""
- * <email protected> string $target = ""
- * <email protected> string $form_name = ""
- * <email protected> string $html
+ * <email protected> string JavaScript form name. If empty no JavaScript validation will be done.
+ * <email protected> string HTML method attribute: "POST" or "GET".
+ * <email protected> string HTML action attribute. If empty $PHP_SELF gets used.
+ * <email protected> string HTML target attribute.
+ * <email protected> string HTML form-Tag
+ * <email protected> string $PHP_SELF
* <email protected> public
*/
- function getStart($js_name = "", $method = "POST", $action = "", $target = "", $form_name = "") {
+ function getStart($js_name = "", $method = "POST", $action = "", $target = "") {
global $PHP_SELF;
$html = sprintf('<form action="%s" target="%s" ',
@@ -273,9 +432,9 @@
$this->setMethod($method);
- if ("" != $js_name) {
+ if ($js_name) {
$this->js_name = $js_name;
- $html .= sprintf('name="%s" onSubmit="return %s_Validator(this, \'\')" ',
+ $html .= sprintf('name="%s" onSubmit="return _%s.validate();" ',
$js_name,
$js_name
);
@@ -287,11 +446,14 @@
/**
* Generate and print <form>-Tag
*
- * <email protected> getStart()
+ * <email protected> getStart()
+ * <email protected> public
*/
- function Start($js_name = "", $method = "POST", $action = "", $target = "", $form_name = "") {
- $html = $this->getStart($js_name, $method, $action, $target, $form_name);
+ function Start($js_name = "", $method = "POST", $action = "", $target = "") {
+
+ $html = $this->getStart($js_name, $method, $action, $target);
print $html;
+
return $html;
} // end func Start
@@ -300,10 +462,11 @@
*
* <email protected> string $additional_html Optional, additional HTML inserted in front of
* the generated HTML.
+ * <email protected> mixed $sess Session Class
* <email protected> string $html
* <email protected> public
*/
- function getFinish($additional_html = "") {
+ function getFinish($additional_html = "", $sess = "") {
$html = $additional_html;
@@ -311,18 +474,8 @@
foreach ($this->hidden_elements as $k => $elname)
$html .= $this->getElement($elname);
}
-
- $html .= "</form>" . $this->CR_HTML;
-
- if ("" != $this->js_name) {
- $html .= $this->getJSHead();
-
- foreach ($this->elements as $name => $el)
- $html .= $el->getJS();
-
- $html .= $this->getJSFoot();
- }
+ $html .= "</form>" . $this->CR_HTML . $this->getJS();
return $html;
} // end func getFinish
@@ -330,41 +483,47 @@
/**
* Generate and print closing </form>-Tag.
*
- * <email protected> getFinish()
+ * <email protected> getFinish()
+ * <email protected> public
*/
function Finish($additional_html = "", $sess = "") {
+
$html = $this->getFinish($additional_html, $sess);
print $html;
+
return $html;
} // end func Finish
/**
* Generate the complete form.
*
- * <email protected> string $js_name = ""
- * <email protected> enum(POST|GET) = "POST"
- * <email protected> string $action = $PHP_SELF
- * <email protected> string $target = ""
- * <email protected> string $form_name = ""
- * <email protected> string $additional_html = ""
- * <email protected> string $html
+ * <email protected> string JavaScript form name. If empty no JavaScript validation will be done.
+ * <email protected> string HTML method attribute: "POST" or "GET".
+ * <email protected> string HTML action attribute. If empty $PHP_SELF gets used.
+ * <email protected> string HTML target attribute.
+ * <email protected> string Userdefined HTML added right before the closing form-Tag
+ * <email protected> string HTML form-Tag
+ * <email protected> string $PHP_SELF
* <email protected> getStart(), getFinish()
* <email protected> public
*/
- function get($js_name = "", $method = "POST", $action = "", $target = "", $form_name = "", $additional_html) {
- return $this->getStart($js_name, $method, $action, $target, $form_name) . $this->getFinish($additional_html);
+ function get($js_name = "", $method = "POST", $action = "", $target = "", $additional_html = "") {
+ return $this->getStart($js_name, $method, $action, $target) . $this->getFinish($additional_html);
} // end func get
/**
* Generate and print the complete form.
*
- * <email protected> get()
+ * <email protected> get()
+ * <email protected> public()
*/
- function show($js_name = "", $method = "POST", $action = "", $target = "", $form_name = "", $additional_html) {
- $html = $this->get($js_name, $method, $action, $target, $form_name, $additional_html);
+ function show($js_name = "", $method = "POST", $action = "", $target = "", $additional_html = "") {
+
+ $html = $this->get($js_name, $method, $action, $target, $additional_html);
print $html;
+
return $html;
- } // end func print
+ } // end func show
/**
* Adds an element to the form object.
@@ -372,7 +531,7 @@
* <email protected> array $element_data
* <email protected> bool $ok
* <email protected> public
- * <email protected> FormError
+ * <email protected> form_error
*/
function addElement($element_data) {
if ("" == $element_data || (is_array($element_data) && 0 == count($element_data)) ) {
@@ -392,13 +551,11 @@
$this->addDefaultAttributes($element_data);
$objectname = "form_element_" . $element_data["type"];
- $el = new $objectname( $element_data, $this->method, $this->js_name, $this->js_mode );
+ $el = new $objectname($element_data, $this->method, $this->js_name, $this->js_mode);
-
- if (!$el->flag_config_ok) {
- #$this->introspection("el $objectname", $el);
- #$this->introspection("da", $element_data);
- }
+ // FIXME
+ if (!$el->flag_config_ok)
+ $this->introspection("el $objectname", $el);
if ($el->isHidden())
$this->hidden_elements[] = $element_data["name"];
@@ -424,14 +581,13 @@
/**
* Generate and return the html representation of an element.
*
- * <email protected> string $elname
- * <email protected> string $value, only used with radio!
- * <email protected> string $html
- * <email protected> FormError
+ * <email protected> string Element name
+ * <email protected> string Element value, only used with radio - FIXME
+ * <email protected> string HTML
+ * <email protected> form_error
* <email protected> public
*/
function getElement($elname, $value = "") {
-
if (!$this->ElementExists($elname)) {
$this->exceptions[] = new form_error("'$elname' is unknown.", __FILE__, __LINE__);
return "";
@@ -452,7 +608,7 @@
*
* <email protected> string $elname
* <email protected> string $type
- * <email protected> FormError
+ * <email protected> form_error
* <email protected> public
*/
function getType($elname) {
@@ -474,13 +630,12 @@
*
* <email protected> string $name element name
* <email protected> mixed $value
- * <email protected> FormError
+ * <email protected> form_error
* <email protected> public
*/
function getValue($name) {
-
if (!$this->ElementExists($name)) {
- $this->exceptions[] = new FormNoSuchElement("'$name' is unknown", __FILE__, __LINE__);
+ $this->exceptions[] = new form_error("'$name' is unknown", __FILE__, __LINE__);
return "";
}
@@ -503,9 +658,11 @@
*
* <email protected> getElement()
*/
- function showElement($name) {
- $html = $this->getElement($name);
+ function showElement($elname, $value = "") {
+
+ $html = $this->getElement($elname, $value);
print $html;
+
return $html;
} // end func showElement
@@ -531,11 +688,8 @@
$this->${validator}();
foreach ($vallist as $k => $name)
- if (isset($this->elements[$name])) {
- list($ok, $message) = $this->elements[$name]->validate();
- if (!$ok)
- $this->setValidationError($name, $message);
- }
+ if (isset($this->elements[$name]) && $message = $this->elements[$name]->validate())
+ $this->setValidationError($name, $message);
return $this->getValidationResult();
} // end func validate
@@ -546,7 +700,8 @@
* <email protected> string $name element name
* <email protected> string $message
* <email protected> boolean $ok
- * <email protected> FormError
+ * <email protected> form_error
+ * <email protected> public
* <email protected> getValidationResult(), validate()
*/
function setValidationError($name, $message) {
@@ -555,7 +710,7 @@
$this->exceptions[] = new form_error("'$name' is unknown", __FILE__, __LINE__);
return false;
}
-
+
$this->validation_msg .= $message . $this->CR_HTML;
$this->validation_flags[$name] = $message;
@@ -566,7 +721,7 @@
* Returns an array that holds the results of the last validation.
*
* <email protected> array ($error_msg, $error_flags)
- * <email protected> private
+ * <email protected> public
* <email protected> setValidationError(), validate()
*/
function getValidationResult() {
@@ -577,9 +732,8 @@
* Returns an array of all custom validation functions
* <email protected> mixed $ellist
* <email protected> array $validators $k => $custom_function
- * <email protected> private
*/
- function getCustomValidators($ellist="") {
+ function getCustomValidators($ellist = "") {
if (!is_array($ellist) && isset($this->elements[$ellist]))
$ellist = array($ellist);
@@ -621,12 +775,13 @@
/**
* Thaw out an element.
*
- * <email protected> mixed
- * <email protected> void
+ * <email protected> mixed Name of one element or list of elements to unfreeze. An empty
+ * string causes a fallback to all elements.
* <email protected> public
* <email protected> freeze()
*/
function unfreeze($flist = "") {
+
if ("" == $flist || !is_array($flist))
$flist = array($flist);
if (0 == count($flist))
@@ -639,12 +794,14 @@
} // end func unfreeze
/**
- * FIXME
* Adds JavaScript validation code to an element
+ *
+ * FIXME
+ *
* <email protected> string $name name of the element
* <email protected> string $code JavaScript code
* <email protected> bool $ok
- * <email protected> FormError
+ * <email protected> form_error
* <email protected> public
*/
function addJS($name, $code) {
@@ -662,64 +819,91 @@
return $this->elements[$name]->addJS($code);
} // end func addJS
- /**
- * Set JavaScript validation code.
- *
- * <email protected> string $name name of the element
- * <email protected> string $code JavaScript code
- * <email protected> bool $ok
- * <email protected> public
- * <email protected> FormError
- */
- function setJS($name, $code) {
-
- if ("" == $name || "" == $code) {
- $this->exceptions[] = new form_error("No name and or no code given.", __FILE__, __LINE__);
- return false;
- }
-
- if (!$this->elementExists($name)) {
- $this->exceptions[] = new form_error("'$name' is unknown", __FILE__, __LINE__);
- return false;
- }
-
- return $this->elements[$name]->setJS($code);
- } // end fun setJS
-
/**
* Returns JavaScript validation code.
*
- * <email protected> string|array $names="" Name of one element, Array of elements or
- * by default all elements
* <email protected> string $code JavaScript validation code
* <email protected> public
- * <email protected> FormError
+ * <email protected> form_error
*/
- function getJS($names = "") {
+ function getJS() {
if ("" == $this->js_name)
return "";
- if ("" == $names)
- $names = $this->element_names;
- if (!is_array($names))
- $names = array($names);
-
- if (0 == count($names)) {
- $this->exceptions[] = new form_error("No elements given or no elements in form.", __FILE__, __LINE__);
+ // get a list of custom javascript validators
+ $validators = $this->getCustomValidators();
+
+ // simple and short code without custom validators, otherwise
+ // long and complex code
+ $file = ($complex = count($validators)) ? "form_js_complex.js" : "form_js_simple.js";
+
+ // get the code
+ $fh = fopen( $file, "r");
+ if (!$fh) {
+ $this->exceptions[] = new form_error("Can't read JavaScript code file: '$file'.", __FILE__, __LINE__);
return "";
+ }
+ $base_code = fread($fh, filesize($file));
+ fclose($fh);
+
+ // customize the error message
+ $base_code = str_replace("{ERROR_MSG_PREFIX}", $this->js_error_prefix, $base_code);
+ $base_code = str_replace("{ERROR_MSG_POSTFIX}", $this->js_error_postfix, $base_code);
+
+ // get JS code from every element of the form
+ $js = "";
+ $get_value = "";
+ // list of already send get_value functions
+ $send = array();
+
+ foreach ($this->elements as $name => $el) {
+
+ list($eldata, $getvalue_func, $getvalue_fname, $js_validator) = $el->getJS($complex);
+ if ($js_validator)
+ $base_code = $base_code . "\n" . $js_validator;
+
+ $js .= $eldata;
+
+ // complex function needed to get the value of this form element
+ if ($getvalue_fname && !isset($send[$getvalue_fname])) {
+ $base_code = $base_code . "\n" . $getvalue_func;
+ $get_value.= sprintf('"%s", "%s", ', $el->getName(), $getvalue_fname);
+ $send[$getvalue_fname] = true;
+ }
+
}
-
- $code = "";
- foreach ($names as $k => $name)
- if (isset($this->elements[$name]))
- $code .= $this->elements[$name]->getJS();
-
- return $code;
+// form("forma", ["elname", [eldata], "elname", [eldata, flags]], ["elname", "gv"], ["val"]);
+// name, els, val, gv
+
+ // putting it all together
+ if ($complex)
+ $js = sprintf('[%s],%s[%s],%s["%s"]',
+ substr($js, 0, -2),
+ "\n",
+ substr($get_value, 0, -2),
+ "\n",
+ join('", ', $validators)
+ );
+ else
+ $js = sprintf('[%s]', substr($js, 0, -2));
+
+ $js = sprintf('<script language="JavaScript1.2"><!--//
+%s
+
+var _%s = new form("%s", %s);
+//--></script>',
+ $base_code,
+ $this->js_name,
+ $this->js_name,
+ $js
+ );
+
+ return $js;
} // end func getJS
/**
- * <email protected> unfreeze
+ * <email protected> unfreeze()
*/
function thawout($flist = "") {
return $this->unfreeze($flist);
@@ -736,93 +920,6 @@
} // end func getElements
/**
- * FIXME
- */
- function getJSHead() {
-
- if ("" == $this->js_name)
- return "";
-
- $js_code = sprintf('<script language="JavaScript1.2"><!--//%s',
- $this->CR_JS
- );
- $js_code .= sprintf('function %s_Validator(f, onchange) {
- error_flags = new Array(%d);
- error_flagtofield = new Array(%d);
- error_msg = "";
- err_id = 0;
-
- ',
- $this->js_name,
- count($this->elements)*2,
- count($this->elements)*2
- );
-
- return $this->clearCode($js_code);
- } // end func getJSHead
-
- /**
- * FIXME
- */
- function getJSFoot() {
-
- $js_code = sprintf('
- if (""==onchange && err_id>0) {
- %s_Complain(error_flags, error_flagtofield, error_msg, f, err_id);
- return false;
- }
- return true;
-
- }
-
- function %s_Complain(error_flags, error_flagtofield, error_msg, f, err_id) {
-
- message = "Ooops - I found some errors: " + err_id + "\n\n";
-
- for (i=0; i<error_flags.length; i++) {
- message+= (i+1)+ ".) [" + error_flagtofield[i] + "] ";
- message+= error_flags[i] + "\n";
- }
-
- if (""!=error_msg.value) {
- message+= "\n... all together:\n"+error_msg;
- }
-
- message+= "\n\nPlease correct them!";
-
- window.alert(message);
- focusfield = error_flagtofield[0];
- f.elements[focusfield].focus();
-
- return true;
- }
-
- function %s_ComplainOnChange(error_flags, error_flagtofield, f, err_id) {
-
- message = "Ooops - I found one error: hier" + err_id + "\n\n";
- message+= (err_id+1) + ".) [" + error_flagtofield[err_id] + "] ";
- message+= error_flags[err_id] + "\n";
- message+= "\nPlease correct this!";
-
- window.alert(message);
- focusfield = error_flagtofield[err_id];
- f.elements[focusfield].focus();
-
- }',
- $this->js_name,
- $this->js_name,
- $this->js_name
- );
-
- $js_code.= sprintf("//-->%s</script>%s",
- $this->CR_HTML,
- $this->CR_HTML
- );
-
- return $this->clearCode($js_code);
- } // end func getJSFoot
-
- /**
* Return an array of all options of a combo list.
*
* Comboboxes expand their option list. Take care that you save
@@ -833,7 +930,7 @@
*
* <email protected> mixed $ellist String with the name of one element or a list of elements.
* <email protected> array $options
- * <email protected> FormError
+ * <email protected> form_error
* <email protected> public
*/
function getOptions($ellist) {
@@ -856,9 +953,11 @@
/*
* Sets the options of a combobox.
*
- * <email protected> string|array $ellist
- * <email protected> bool $ok
- * <email protected> FormError
+ * FIXME: param wird nicht erkennt
+ *
+ * <email protected> boolean
+ * <email protected> mixed Optionlist: element name => array options
+ * <email protected> form_error
* <email protected> public
* <email protected> getOptions()
*/
@@ -874,6 +973,357 @@
return true;
} // end func setOptions
+
+ /**
+ * Checks the existance of a certain element.
+ *
+ * <email protected> string $name name of the requested element
+ * <email protected> bool $ok
+ */
+ function ElementExists($name) {
+
+ if ("" == $name) {
+ $this->exceptions[] = new form_error("No name specified.", __FILE__, __LINE__);
+ return false;
+ }
+
+ if (isset($this->elements[$name]) || isset($this->radio_elements[$name]))
+ return true;
+ else
+ return false;
+
+ } // end func ElementExists
+
+ /**
+ * Sets default element attributes.
+ *
+ * You can use this function to define default values for
+ * elements.
+ *
+ * <email protected> array $defaults $default [ attribute ] = value
+ * <email protected> boolean $ok
+ * <email protected> form_error
+ * <email protected> $defaults
+ */
+ function setDefaults($defaults) {
+
+ if (!is_array($defaults)) {
+ $this->exceptions[] = new form_error("No default values.", __FILE__, __LINE__);
+ return false;
+ }
+
+ $ok = true;
+ foreach ($defaults as $attribute => $value)
+ if (isset($this->defaults[$attribute])) {
+ $this->defaults[$attribute] = $value;
+ $ok = true;
+ }
+
+ return $ok;
+ } // end func setDefaults
+
+ /**
+ * Adds default values to an element if they haven't been overriden.
+ *
+ * <email protected> array
+ */
+ function addDefaultAttributes(&$element_data) {
+
+ switch($element_data["type"]) {
+
+ case "text":
+ if (0 != $this->defaults["textsize"] && !isset($element_data["size"]))
+ $element_data["size"] = $this->defaults["textsize"];
+
+ if (0 != $this->defaults["textmaxlength"] && !isset($element_data["maxlength"]))
+ $element_data["maxlength"] = $this->defaults["textmaxlength"];
+
+ break;
+
+ case "textarea":
+ if (0 != $this->defaults["textsize"] && !isset($element_data["cols"]))
+ $element_data["cols"] = $this->defaults["textsize"];
+
+ if (0 != $this->defaults["textmaxlength"] && !isset($element_data["maxlength"]))
+ $element_data["maxlength"] = $this->defaults["textmaxlength"];
+
+ break;
+
+ case "file":
+ case "fileupload":
+
+ if (0 != $this->defaults["filesize"] && !isset($element_data["size"]))
+ $element_data["size"] = $this->defaults["filesize"];
+
+ break;
+
+ case "image":
+
+ if (0 != $this->defaults["imageborder"] && !isset($element_data["border"]))
+ $element_data["border"] = $this->defaults["imageborder"];
+
+ break;
+
+ case "select":
+ case "combo":
+
+ if ("" != $this->defaults["selectintro_e"] && !isset($element_data["intro_e"]))
+ $element_data["intro_e"] = $this->defaults["selectintro_e"];
+ if ("" != $this->defaults["selectintro"] && !isset($element_data["intro"]))
+ $element_data["intro"] = $this->defaults["intro"];
+
+ break;
+
+ default:
+ break;
+ }
+
+ if (!isset($element_data["class"]) && "" != $this->defaults["cssclass"])
+ $element_data["class"] = $this->defaults["cssclass"];
+
+ if (!isset($element_data["id"]) && "" != $this->defaults["cssid"])
+ $element_data["id"] = $this->defaults["cssid"];
+
+ if (!isset($element_data["style"]) && "" != $this->defaults["cssstyle"])
+ $element_data["style"] = $this->defaults["cssstyle"];
+
+ } // end func addDefaultAttributes
+
+ /**
+ * FIXME
+ *
+ * <email protected> mixed $ellist
+ */
+ function getNameTranslationList($ellist) {
+
+ if ("" == $ellist)
+ $ellist = $this->getElementNames();
+
+ if (!is_array($ellist))
+ $ellist = array( $ellist );
+
+ $names = array();
+
+ reset($ellist);
+ while (list($k, $name)=each($ellist)) {
+ if (isset($this->radio_elements[$name])) {
+
+ $names[$name] = array(
+ "name" => $this->radio_elements[$name],
+ "value" => $this->getValue($name)
+ );
+
+ } else if (isset($this->elements[$name])) {
+
+ $names[$name] = array(
+ "name" => $this->elements[$name]->getName(),
+ "value" => $this->elements[$name]->getValue()
+ );
+
+ }
+ }
+
+ return $names;
+ } // end func getNameTranslationList
+
+ /**
+ * FIXME
+ */
+ function getElementnames() {
+
+ $elements = array();
+ foreach ($this->elements as $name => $el)
+ $elements[$name] = $name;
+
+ return $elements;
+ } // end func getElementnames
+
+ /**
+ * Reformate generated code
+ *
+ * <email protected> string $code
+ * <email protected> string $code
+ */
+ function clearCode($code) {
+ $code = ereg_replace("[\t]+", "\t", $code);
+ $code = ereg_replace("[ ]{2,}", "", $code);
+ return $code;
+ } // end func clearCode
+
+ /**
+ * FIXME
+ *
+ * Returns an array with elname => value pairs, that can be used
+ * to be stored in a session. new_assistant needs this
+ *
+ * <email protected> array $session_hash
+ * <email protected> public
+ */
+ function getSessionHash() {
+
+ $data = array();
+ foreach ($this->elements as $name => $value)
+ $data[$name] = $this->getValue($name);
+
+ return $data;
+ } // end func getSessionHash
+
+ /**
+ * Sets a global Prefix for element names.
+ *
+ * <email protected> string
+ * <email protected> boolean
+ * <email protected> $element_prefix
+ */
+ function setElementnamePrefix($prefix) {
+ if ("" == $prefix) {
+ $this->exeptions[] = new form_error("No prefix given", __FILE__, __LINE__);
+ return false;
+ }
+
+ $this->element_prefix = $prefix;
+ return true;
+ } // end func setElementnamePrefix
+
+ /**
+ * Is this checkboxobject checked?
+ *
+ * <email protected> string Element name
+ * <email protected> boolean
+ * <email protected> public
+ */
+ function isChecked($elname) {
+
+ if (!$this->isMethodAllowed("isChecked", $elname, array("radio", "checkbox")) )
+ return false;
+
+ return $this->elements[$elname]->isChecked();
+ } // end func isChecked
+
+ /**
+ * Returns the latest combo entry of the user
+ *
+ * <email protected> string $name element name
+ * <email protected> string $html
+ * <email protected> public
+ */
+ function getNewComboOption($name) {
+
+ if (!$this->isMethodAllowed("getNewComboOption", $name, "combo"))
+ return "";
+
+ return $this->elements[$name]->getNewComboOption();
+ } // end func getNewComboOption
+
+ /**
+ * Add a new option to a combo
+ *
+ * <email protected> string $name
+ * <email protected> string $label
+ * <email protected> string $value = ""
+ * <email protected> bool $ok
+ * <email protected> public
+ * <email protected> getNewComboOption(), SetOptions(), getOptions()
+ */
+ function addOption($name, $label, $value = "") {
+
+ if (!$this->isMethodAllowed("addOption", $name, array("combo", "select")))
+ return "";
+
+ return $this->elements[$name]->addOption($label, $value);
+ } // end func addOption
+
+ /**
+ * FIXME FIXME
+ * Returns the tree for a certain value of an treeview element
+ * It's just implementet to make the api complete
+ * <email protected> string|array $ellist
+ * <email protected> public
+ * <email protected> array $trees
+ */
+ function getTree($ellist) {
+ if (!is_array($ellist))
+ $ellist = array($ellist => "");
+
+ $trees = array();
+ foreach ($ellist as $name => $value)
+ if (isset($this->elements[$name]) && "tree" == $this->elements[$name]->getType())
+ $trees[$name] = $this->elements[$name]->getTree($value);
+
+ return $trees;
+ } // end func getTree
+
+ /**
+ * Set the tree of an treeview element
+ * Helpful with dynamically changing trees
+ * <email protected> array $ellist
+ * <email protected> public
+ * <email protected> bool $ok
+ * <email protected> form_error
+ */
+ function setTree($ellist) {
+
+ if (!is_array($ellist)) {
+ $this->exceptions[] = new form_error("No element list given.", __FILE__, __LINE__);
+ return false;
+ }
+
+ foreach ($ellist as $name => $value)
+ if (isset($this->elements[$name]) && "tree" == $this->elements[$name]->getType())
+ $this->elements[$name]->setTree($value);
+
+ return true;
+ } // end func setTree
+
+ /**
+ *
+ * <email protected> string $function Name of the function that calls this method
+ * <email protected> string $name Name of a form element
+ * <email protected> mixed $required_type
+ * <email protected> boolean $ok
+ * <email protected> form_error
+ */
+ function isMethodAllowed($function, $name, $required_type) {
+
+ if (!$this->elementExists($elname)) {
+ $this->exceptions[] = new form_error("$function(): '$elname' is unknown", __FILE__, __LINE__);
+ return false;
+ }
+
+ $type = $this->elements[$elname]->gettype();
+
+ if (!is_array($required_type))
+ $required_type = array ( $required_type );
+
+ $ok = false;
+ foreach ($required_type as $k => $required)
+ if ($required == $type) {
+ $ok = true;
+ break;
+ }
+
+ if (!$ok) {
+ $this->exceptions[] = new form_error("$function(): '$elname' is not a radio or checkbox element", __FILE__, __LINE__);
+ return false;
+ }
+
+ return $ok;
+ } // end func isMethodAllowed
+
+ /**
+ * Sets the JavaScript error message pre- and postfix text.
+ *
+ * <email protected> string Prefix
+ * <email protected> string Postfix
+ * <email protected> public
+ * <email protected> $js_error_prefix, $js_error_postfix
+ */
+ function setJSError($prefix, $postfix) {
+
+ $this->js_error_prefix = $prefix;
+ $this->js_error_postfix = $postfix;
+
+ } // end func setJSError
} // end class form
?>
Index: php-lib/php/form/form_element.inc
diff -u php-lib/php/form/form_element.inc:1.5 php-lib/php/form/form_element.inc:1.6
--- php-lib/php/form/form_element.inc:1.5 Tue Dec 12 02:37:33 2000
+++ php-lib/php/form/form_element.inc Mon Jan 1 15:50:48 2001
@@ -3,7 +3,8 @@
* Superclass of all form elements.
*
* <email protected> Ulf Wendel <uw <email protected>>
-* <email protected> $Id: form_element.inc,v 1.5 2000/12/12 01:37:33 uw Exp $
+* <email protected> $Id: form_element.inc,v 1.6 2001/01/01 14:50:48 uw Exp $
+* <email protected> public
* <email protected> Form
* <email protected>
*/
@@ -43,8 +44,7 @@
/**
* Value of the element.
*
- * <email protected> mixed $value On most elements it must be a skalar value,
- * expect arrays.
+ * <email protected> mixed $value
*/
var $value = "";
@@ -92,7 +92,7 @@
* on debugging form elements.
*
* <email protected> array $shared_required_fields $shared_required_fields[ $field ] = $type
- * <email protected> $requiered_fields, $shared_optional_fields, checkConfiguration()
+ * <email protected> $required_fields, $shared_optional_fields, checkConfiguration()
*/
var $shared_required_fields = array (
"type" => "string",
@@ -121,6 +121,9 @@
"elname" => "string",
"additional_html" => "string",
+ "validator" => "string",
+ "js_validator" => "string",
+
"frozen" => "boolean",
"class" => "string",
@@ -139,65 +142,53 @@
/**
* Method (POST|GET) used in the form.
*
- * <email protected> string $method
+ * <email protected> string
*/
var $method = "";
/**
* Check for ... errors.
*
- * <email protected> string $reacton
+ * <email protected> array
*/
var $val_events = array(
"length" => true,
"valid" => true,
- "intro" => true,
- "js_onsubmit" => false,
- "js_onchange" => false
+ "intro" => true
);
/**
- * Javascript validation code.
- *
- * <email protected> string $js_code
- */
- var $js_code = "";
-
- /**
- * Additional JavaScript code provided by the user
- *
- * <email protected> string $js_usercode
- */
- var $js_usercode = "";
-
- /**
- * Nameprefix of all JavaScript functions.
+ * JavaScript validation mode.
+ *
+ * "weak" => onSubmit or "strong" => onChange/onClick.
*
- * <email protected> string $js_name
- */
- var $js_name = "";
-
- /**
- * <email protected> class new_of/js_mode
+ * <email protected> string
*/
var $js_mode = "";
/**
- * Javascript validation code set by user?
- *
- * <email protected> boolean
+ * Name of the form that contains the element.
+ *
+ * <email protected> string
*/
- var $flag_setjs = false;
+ var $form_name = "";
/**
* Name of a custom validation function provided by the user.
*
* The function is part of MyForm.
*
- * <email protected> string $custom_validator = ""
+ * <email protected> string
*/
var $validator = "";
-
+
+ /**
+ * Custom JavaScript validator.
+ *
+ * <email protected> string
+ */
+ var $js_validator = "";
+
/**
* Creates a new form element.
*
@@ -210,20 +201,17 @@
* <email protected> enum $method Value of the method attribute of the HTML <form>
* tag where this element will be embedded. All values
* but "POST" (case invalid) are interpreted as "GET".
- * <email protected> string $js_name Optional, value of the name attttribute of the HTML <form>
- * tag where this element will be embedded. If a
- * name is given, JavaScript code will be generated.
- * <email protected> enum $js_mode Optional, JavaScript validation mode. If set to
- * "STRONG" (case invalid) each element gets checked
- * when changed (onChange) and when the form gets submitted (onSubmit).
+ * <email protected> string $form_name HTML name attribute of the form tag.
+ * <email protected> string $js_mode Optional, JavaScript validation mode. If set to
+ * "strong" (case insensitive) each element gets checked
+ * when changed (onChange/onClick) and when the form gets submitted (onSubmit).
* All other values start JavaScript validation with "onSubmit".
- * <email protected> bool false on failure otherwise true
*/
- function form_element ( $element_data, $method, $js_name = "", $js_mode = "" ) {
+ function form_element ($element_data, $method, $form_name = "", $js_mode = "") {
$ok = $this->checkConfiguration($element_data);
if (!$ok)
- return false;
+ return;
foreach ($element_data as $slot => $value)
if ("validator" == $slot)
@@ -232,19 +220,17 @@
$this->$slot = $value;
$this->type = isset($element_data["type"]) ? $element_data["type"] : "unknown";
+ $this->name = $element_data["name"];
- $this->js_name = $js_name;
+ if ("" != $js_mode)
+ $js_mode = ("weak" == strtolower($js_mode)) ? "weak" : "strong";
$this->js_mode = $js_mode;
- if ("" != $js_name) {
- $this->val_events["js_onsubmit"] = true;
- if ("strong" == $js_mode)
- $this->val_events["js_onchange"] = true;
- }
+ $this->form_name = $form_name;
$this->method = strtoupper($method);
$this->setup();
- $this->generateJS();
+
} // end constructor
/**
@@ -279,7 +265,7 @@
*
* <email protected> array $element_data Data used in form::add_element()
* <email protected> boolean $ok
- * <email protected> FormError
+ * <email protected> form_error
*/
function checkConfiguration($element_data) {
@@ -292,7 +278,7 @@
$ok = false;
} else {
-
+
foreach ($this->required_fields as $field => $type)
if (!isset($element_data[$field])) {
@@ -408,7 +394,7 @@
*
* <email protected> string $validator
&nbs

