cvs: pear /HTML_OOH_Form/elements form_element.php
| From: | Ulf Wendel | Date: | Tue, 20 Nov 2001 01:10:48 +0000 |
| Subject: | cvs: pear /HTML_OOH_Form/elements form_element.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-1377@lists.php.net to get a copy of this message | ||
uw Mon Nov 19 20:10:48 2001 EDT
Modified files:
/pear/HTML_OOH_Form/elements form_element.php
Log:
Almost all form_element had to be changed as some tasks like validation
are from now on performed by a container
Index: pear/HTML_OOH_Form/elements/form_element.php diff -u pear/HTML_OOH_Form/elements/form_element.php:1.6 pear/HTML_OOH_Form/elements/form_element.php:1.7 --- pear/HTML_OOH_Form/elements/form_element.php:1.6 Sat Nov 3 07:53:18 2001 +++ pear/HTML_OOH_Form/elements/form_element.php Mon Nov 19 20:10:48 2001 @@ -16,7 +16,7 @@ // | Authors: Ulf Wendel <ulf.wendel@phpdoc.de> | // +----------------------------------------------------------------------+ // -// $Id: form_element.php,v 1.6 2001/11/03 12:53:18 uw Exp $ +// $Id: form_element.php,v 1.7 2001/11/20 01:10:48 uw Exp $ require_once('HTML_OOH_Form/form_commonobject.php'); @@ -24,7 +24,7 @@ * Superclass of all form elements. * * @author Ulf Wendel <ulf.wendel@phpdoc.de> -* @version $Id: form_element.php,v 1.6 2001/11/03 12:53:18 uw Exp $ +* @version $Id: form_element.php,v 1.7 2001/11/20 01:10:48 uw Exp $ * @access public * @package HTML_OOH_Form * @abstract @@ -65,15 +65,7 @@ */ var $name = ''; - - /** - * Value of the element. - * - * @var mixed $value - */ - var $value = ''; - /** * Additional HTML attributes. * @@ -90,8 +82,8 @@ * @var boolean $frozen */ var $frozen = false; - - + + /** * HTML/CSS class attribute. * @@ -126,8 +118,8 @@ * @see $required_fields, $shared_optional_fields, checkConfiguration() */ var $shared_required_fields = array ( - 'type' => 'string', - 'name' => 'string' + 'type' => 'string', + 'name' => 'string' ); @@ -168,6 +160,7 @@ ); + /** * List of optional fields provided by a derived class. * @@ -183,36 +176,16 @@ * @var string */ var $method = ''; - - - /** - * Check for ... errors. - * - * @var array - */ - var $val_events = array( - 'length' => true, - 'valid' => true, - 'intro' => true - ); - - /** - * JavaScript validation mode. - * - * 'weak' => onSubmit or 'strong' => onChange/onClick. - * - * @var string - */ - var $js_mode = ''; - /** - * Name of the form that contains the element. + * Optional label. + * + * This value is only meaningful for layout engines. * * @var string */ - var $form_name = ''; + var $label = ''; /** @@ -242,13 +215,19 @@ /** - * Optional label. - * - * This value is only meaningful for layout engines. - * + * Container object used to store the element value + * + * @var object container + */ + var $container; + + + /** + * Name of the container class to use if the user did not specify one. + * * @var string */ - var $label = ''; + var $default_container_class = ''; /** @@ -270,27 +249,53 @@ * All other values start JavaScript validation with 'onSubmit'. */ function form_element ($element_data, $method, $form_name = '', $js_mode = '') { - if (!($ok = $this->checkConfiguration($element_data))) - return; + + // KLUDGE: ask the container which slots it accepts and use ... + $allowed = get_object_vars($this); + if (isset($element_data['container'])) { + + if (is_object($element_data['container'])) { + // the user took care of the object creation + // this is usually the case if we share the container between several object + $this->container = &$element_data['container']; + } else { + // the user told us which kind of container to use + $class = 'form_container_' . $element_data['container']; + require_once('HTML_OOH_Form/container/' . $class . '.php'); + $this->container = new $class($element_data); + } + + } else { + + $class = ($this->default_container_class) ? 'form_container_' . $this->default_container_class : 'form_container'; + require_once('HTML_OOH_Form/container/' . $class . '.php'); + $this->container = new $class($element_data); + + } + foreach ($element_data as $slot => $value) - if ('validator' == $slot) + if ('validator' == $slot) { $this->setValidator($value, (isset($element_data['validator_callback'])) ? $element_data['validator_callback'] : false); - else + } else if ('frozen' == $slot) { + $this->frozen = $value; + $this->container->frozen = $value; + } else if ('container' == $slot) { + continue; + } else if (isset($allowed[$slot])) { $this->$slot = $value; - + } + $this->type = isset($element_data['type']) ? $element_data['type'] : 'unknown'; - $this->name = $element_data['name']; if ('' != $js_mode) $js_mode = ('weak' == strtolower($js_mode)) ? 'weak' : 'strong'; - $this->js_mode = $js_mode; + $this->form_name = $form_name; $this->method = strtoupper($method); $this->setup(); - } // end constructor /** @@ -404,16 +409,19 @@ $this->flag_config_ok = $ok; return $ok; } // end func checkConfiguration + /** * Returns the value of an element. * - * @return mixed $value + * @param boolean encode the value? + * @return mixed $value * @access public */ - function getValue() { - return $this->value; + function getValue($encoded = false) { + return $this->container->getValue($encoded); } // end func getValue() + /** * Returns the type of this element. @@ -456,7 +464,7 @@ * @access public */ function getValidator() { - return array($this->validator, $this->validator_callback, $this->js_validator); + return $this->container->getValidator(); } // end func getValidator /** @@ -470,15 +478,7 @@ * @see getValidator() */ function setValidator($validator, $flag_callback = false) { - if ('string' != gettype($validator)) { - $this->exceptions[] = new form_error('The name of a validator must be a string.', __FILE__, __LINE__); - return false; - } - - $this->validator = $validator; - $this->validator_callback = $flag_callback; - - return true; + return $this->container->getValidator(); } // end func setValidator /** @@ -487,7 +487,8 @@ * @access public */ function freeze() { - $this->frozen = true; + $this->container->freeze(); + $this->freeze = true; } // end func freeze /** @@ -496,6 +497,7 @@ * @access public */ function unfreeze() { + $this->container->unfreeze(); $this->frozen = false; } // end func unfreeze @@ -538,7 +540,7 @@ function getfrozen($subelement = '') { return sprintf('%s%s', $this->getFrozenHiddenElement(), - $this->value + $this->container->getValue() ); } // end func getfrozen() @@ -547,9 +549,9 @@ * * @brother get() */ - function show($value = '') { + function show($subelement = '') { - $html = $this->get($value); + $html = $this->get($subelement); print $html; return $html; @@ -564,80 +566,10 @@ * @abstract */ function validate() { - return ''; + return $this->container->validate(); } // end func validate /** - * Validate the length of the input value. - * - * @param mixed $value Optional value, if not specified $this->value gets used. - * @return string $errormessage - */ - function validateLength($value = '') { - - $value = ($value) ? $value : $this->value; - - if ($this->doValidation('length') && (strlen($value) < $this->minlength || ($this->maxlength >= 0 && strlen($value) > $this->maxlength))) - return $this->length_e . ' '; - return ''; - } // end func validateLength - - /** - * Compare the element value with the specified RegExp. - * - * @param mixed $value Optional value, if not specified $this->value gets used. - * @return string $errormessage - * @see validateLength() - */ - function validateRegEx($value = '') { - - $reg = '/' . $this->valid_regex . '/'; - $reg.= ($this->valid_icase) ? 'i' : ''; - if ($this->doValidation('valid') && !preg_match($reg, ($value) ? $value : $this->value)) { - return $this->valid_e . ' '; - } - return ''; - } // end func validateRegEx - - /** - * Validate this event? - * - * @param string $event - * @return boolean $ok - * @throws form_error - */ - function doValidation($event) { - if ('' == $event || !isset($this->val_events[$event])) { - $this->exceptions[] = new form_error("Unknown event: '$event'", __FILE__, __LINE__); - return false; - } - - if (false == $this->val_events[$event] || true == $this->frozen) - return false; - - $ok = false; - switch (strtolower($event)) { - - case 'length': - if ('' != $this->length_e && (0 != $this->minlength || $this->maxlength >= 0) ) - $ok = true; - break; - - case 'valid': - if ('' != $this->valid_e && '' != $this->valid_regex) - $ok = true; - break; - - case 'intro': - if ('' != $this->intro_e && '' != $this->intro) - $ok = true; - break; - } - - return $ok; - } // end func doValidation - - /** * Defines the events that will be validated. * * You can tell every (default) validator which 'events' @@ -667,28 +599,7 @@ * @see $val_events */ function react($events, $status = true) { - - if ('all' == $events) - $events = $this->val_events; - - if (!is_array($events) && isset($this->val_events[$events])) - $events = array($events => $events); - - if (!is_array($events)) { - $this->exceptions[] = new form_error("Unknown event '$events'.", __FILE__, __LINE__); - return false; - } - - $status = ($status) ? true : false; - $ok = true; - - foreach ($events as $event => $v) - if (isset($this->val_events[$event])) - $this->val_events[$event] = $status; - else - $ok = false; - - return $ok; + return $this->container($events, $status); } // end func react /** @@ -698,10 +609,112 @@ * @access public */ function setValue($value) { - $this->value = $value; + $this->container->setValue($value); } // end func setValue /** + * Returns JavaScript validation code. + * + * See form_js_simple.js and form_js_complex.js for details on the JavaScript + * validation code. This function build a record to 'feed' the JavaScript form + * object. + * + * @param boolean Build JS for form_js_complex.js? + * @access public + * @return array + */ + function getJS($complex = false) { + + $eldata = ''; + $events = ''; + + // Do not change the order of the code! + + // decode userinput + list($decode_func, $decode_fname) = $this->container->getJSdecode(); + if ($decode_fname) + $eldata .= sprintf('"d", "%s", ', $decode_fname); + + // encode userinput + list($encode_func, $encode_fname) = $this->container->getJSencode(); + if ($encode_fname) + $eldata .= sprintf('"e", "%s", ', $encode_fname); + + // set value function + list($setvalue_func, $setvalue_fname) = $this->getJSsetvalue(); + if ($setvalue_fname && $encode_fname) + $eldata .= sprintf('"s", "%s", ', $setvalue_fname); + + // Function used to get the current value of the element. + // Caution: 'v' must be the first entry in the validation data record. + // 'v', 'my_get_value_function' + list($getvalue_func, $getvalue_fname) = $this->getJSvalue(); + if (!$complex && $getvalue_fname) + $eldata .= sprintf('"v", "%s", ', $getvalue_fname); + + list($c_eldata, $events) = $this->container->getJS($complex); + $eldata .= $c_eldata; + + // format record entry + if ($eldata) { + $eldata = sprintf('"%s", [%s], ', + $this->name, + substr($eldata, 0, -2) + ); + if ($complex) + $eldata .= sprintf('"%s",%s', $events, "\n"); + } + + return array( + "eldata" => $eldata, + "js_validator" => $this->js_validator, + "getvalue_func" => $getvalue_func, + "getvalue_fname" => $getvalue_fname, + "decode_func" => $decode_func, + "decode_fname" => $decode_fname, + "encode_func" => $encode_func, + "encode_fname" => $encode_fname, + "setvalue_func" => $setvalue_func, + "setvalue_fname" => $setvalue_fname + ); + + } // end func getJS + + + /** + * JavaScript function used to set the value of the element. + * + * @return array [function, function name] + */ + function getJSsetvalue() { + /* return array('function fe_setv(f, e, v) { document.forms[f].elements[e].value = v; }', 'fe_setv'); */ + return array('', ''); + } // end func getJSsetvalue + + + /** + * JavaScript function used to get the value of the element. + * + * Most elements can use document.forms[..].elements[...].value to get the + * value of an element but some need a more sophisticated function. These + * elements must override this function. + * + * The JavaScript function provided by this elements should be names 'form_elmenttype', + * e.g. 'form_select'. The (JS) function gets called with two arguments: + * string formname, string elementname. It returns the value of the element, see + * form_element_select for an example. + * + * The function returns the JavaScript code itself and a name for the code + * snippet that allows the form object to avoid sending the code twice. + * + * @return array [JavaScript function code, JavaScript function name] + */ + function getJSvalue() { + return array('["", ""]', ""); + } // end func getJSvalue + + + /** * Return a JavaScript onChange attribute if strong Javascript validation is active * * @return string @@ -751,7 +764,7 @@ return sprintf('<input type="hidden" name="%s" value="%s">', $this->name, - ('' != $value) ? $value : $this->value + ('' != $value) ? $value : $this->container->getValue() ); } // end func getFrozenHiddenElement @@ -768,104 +781,12 @@ return sprintf('<table cellpadding="0" cellspacing="0" %s><tr><td>%s</td></tr></table>', ($border) ? ' border="1"' : '', - ('' != $value) ? $value : $this->value + ('' != $value) ? $value : $this->container->getValue() ); } // end func getFrozenTable /** - * Returns JavaScript validation code. - * - * See form_js_simple.js and form_js_complex.js for details on the JavaScript - * validation code. This function build a record to 'feed' the JavaScript form - * object. - * - * @param boolean Build JS for form_js_complex.js? - * @access public - * @return array - */ - function getJS($complex = false) { - - $eldata = ''; - $events = ''; - list($getvalue_func, $getvalue_fname) = $this->getJSvalue(); - - // Function used to get the current value of the element. - // Caution: 'v' must be the first entry in the validation data record. - // Do not change the order of the code! - // 'v', 'my_get_value_function' - if (!$complex && $getvalue_fname) - $eldata .= sprintf('"v", "%s", ', $getvalue_fname); - - // Length validation: 'l', minlength, maxlength, 'length_e' - if ($this->doValidation('length')) { - $eldata .= sprintf('"l", %d, %d, "%s", ', - $this->minlength, - $this->maxlength, - str_replace('"', '\"', $this->length_e) - ); - $events .= 'l'; - } - - // RegExp validation: 'r', 'valid_regex', 'valid_modifier', 'valid_e' - if ($this->doValidation('valid')) { - $eldata .= sprintf('"r", "%s", "%s%s", "%s", ', - str_replace('"', '\"', $this->valid_regex), - ($this->valid_icase) ? 'i' : '', - (isset($this->valid_global) && $this->valid_global) ? 'g' : '', - str_replace('"', '\"', $this->valid_e) - ); - $events .= 'r'; - } - - // Select / Intro validation: 'i', 'intro', 'intro_e' - if ($this->doValidation('intro')) { - - reset($this->intro); - list($intro_value, $intro_label) = each($this->intro); - - $eldata .= sprintf('"i", "%s", "%s", ', - $intro_value, - str_replace('"', '\"', $this->intro_e) - ); - $events .= 'i'; - } - - // format record entry - if ($eldata) { - $eldata = sprintf('"%s", [%s], ', - $this->name, - substr($eldata, 0, -2) - ); - if ($complex) - $eldata .= sprintf('"%s",%s', $events, "\n"); - } - - return array($eldata, $getvalue_func, $getvalue_fname, $this->js_validator); - } // end func getJS - - /** - * JavaScript function used to get the value of the element. - * - * Most elements can use document.forms[..].elements[...].value to get the - * value of an element but some need a more sophisticated function. These - * elements must override this function. - * - * The JavaScript function provided by this elements should be names 'form_elmenttype', - * e.g. 'form_select'. The (JS) function gets called with two arguments: - * string formname, string elementname. It returns the value of the element, see - * form_element_select for an example. - * - * The function returns the JavaScript code itself and a name for the code - * snippet that allows the form object to avoid sending the code twice. - * - * @return array [JavaScript function code, JavaScript function name] - */ - function getJSvalue() { - return array('["", ""]', ""); - } // end func getJSvalue - - /** * Returns additional JavaScript required by some complex form elements. * * Some elements need extra JavaScript functionality to be @@ -901,6 +822,42 @@ function isHidden() { return $this->flag_hidden; } // end func isHidden + + + /** + * Returns the name of the custom validation function. + * + * @return array [validator function name, flag callback, js_validator] + * @see setValidator() + * @access public + */ + function getValidator() { + return array($this->validator, $this->validator_callback, $this->js_validator); + } // end func getValidator + + + /** + * Sets the name of the custom validation function. + * + * @param string $validator + * @param boolean use a callback function? + * @return boolean $ok + * @throws form_error + * @access public + * @see getValidator() + */ + function setValidator($validator, $flag_callback = false) { + if ('string' != gettype($validator)) { + $this->exceptions[] = new form_error('The name of a validator must be a string.', __FILE__, __LINE__); + return false; + } + + $this->validator = $validator; + $this->validator_callback = $flag_callback; + + return true; + } // end func setValidator + } // end class form_element ?>
Index: pear/HTML_OOH_Form/elements/form_element.php diff -u pear/HTML_OOH_Form/elements/form_element.php:1.6 pear/HTML_OOH_Form/elements/form_element.php:1.7 --- pear/HTML_OOH_Form/elements/form_element.php:1.6 Sat Nov 3 07:53:18 2001 +++ pear/HTML_OOH_Form/elements/form_element.php Mon Nov 19 20:10:48 2001 @@ -16,7 +16,7 @@ // | Authors: Ulf Wendel <ulf.wendel@phpdoc.de> | // +----------------------------------------------------------------------+ // -// $Id: form_element.php,v 1.6 2001/11/03 12:53:18 uw Exp $ +// $Id: form_element.php,v 1.7 2001/11/20 01:10:48 uw Exp $ require_once('HTML_OOH_Form/form_commonobject.php'); @@ -24,7 +24,7 @@ * Superclass of all form elements. * * @author Ulf Wendel <ulf.wendel@phpdoc.de> -* @version $Id: form_element.php,v 1.6 2001/11/03 12:53:18 uw Exp $ +* @version $Id: form_element.php,v 1.7 2001/11/20 01:10:48 uw Exp $ * @access public * @package HTML_OOH_Form * @abstract @@ -65,15 +65,7 @@ */ var $name = ''; - - /** - * Value of the element. - * - * @var mixed $value - */ - var $value = ''; - /** * Additional HTML attributes. * @@ -90,8 +82,8 @@ * @var boolean $frozen */ var $frozen = false; - - + + /** * HTML/CSS class attribute. * @@ -126,8 +118,8 @@ * @see $required_fields, $shared_optional_fields, checkConfiguration() */ var $shared_required_fields = array ( - 'type' => 'string', - 'name' => 'string' + 'type' => 'string', + 'name' => 'string' ); @@ -168,6 +160,7 @@ ); + /** * List of optional fields provided by a derived class. * @@ -183,36 +176,16 @@ * @var string */ var $method = ''; - - - /** - * Check for ... errors. - * - * @var array - */ - var $val_events = array( - 'length' => true, - 'valid' => true, - 'intro' => true - ); - - /** - * JavaScript validation mode. - * - * 'weak' => onSubmit or 'strong' => onChange/onClick. - * - * @var string - */ - var $js_mode = ''; - /** - * Name of the form that contains the element. + * Optional label. + * + * This value is only meaningful for layout engines. * * @var string */ - var $form_name = ''; + var $label = ''; /** @@ -242,13 +215,19 @@ /** - * Optional label. - * - * This value is only meaningful for layout engines. - * + * Container object used to store the element value + * + * @var object container + */ + var $container; + + + /** + * Name of the container class to use if the user did not specify one. + * * @var string */ - var $label = ''; + var $default_container_class = ''; /** @@ -270,27 +249,53 @@ * All other values start JavaScript validation with 'onSubmit'. */ function form_element ($element_data, $method, $form_name = '', $js_mode = '') { - if (!($ok = $this->checkConfiguration($element_data))) - return; + + // KLUDGE: ask the container which slots it accepts and use ... + $allowed = get_object_vars($this); + if (isset($element_data['container'])) { + + if (is_object($element_data['container'])) { + // the user took care of the object creation + // this is usually the case if we share the container between several object + $this->container = &$element_data['container']; + } else { + // the user told us which kind of container to use + $class = 'form_container_' . $element_data['container']; + require_once('HTML_OOH_Form/container/' . $class . '.php'); + $this->container = new $class($element_data); + } + + } else { + + $class = ($this->default_container_class) ? 'form_container_' . $this->default_container_class : 'form_container'; + require_once('HTML_OOH_Form/container/' . $class . '.php'); + $this->container = new $class($element_data); + + } + foreach ($element_data as $slot => $value) - if ('validator' == $slot) + if ('validator' == $slot) { $this->setValidator($value, (isset($element_data['validator_callback'])) ? $element_data['validator_callback'] : false); - else + } else if ('frozen' == $slot) { + $this->frozen = $value; + $this->container->frozen = $value; + } else if ('container' == $slot) { + continue; + } else if (isset($allowed[$slot])) { $this->$slot = $value; - + } + $this->type = isset($element_data['type']) ? $element_data['type'] : 'unknown'; - $this->name = $element_data['name']; if ('' != $js_mode) $js_mode = ('weak' == strtolower($js_mode)) ? 'weak' : 'strong'; - $this->js_mode = $js_mode; + $this->form_name = $form_name; $this->method = strtoupper($method); $this->setup(); - } // end constructor /** @@ -404,16 +409,19 @@ $this->flag_config_ok = $ok; return $ok; } // end func checkConfiguration + /** * Returns the value of an element. * - * @return mixed $value + * @param boolean encode the value? + * @return mixed $value * @access public */ - function getValue() { - return $this->value; + function getValue($encoded = false) { + return $this->container->getValue($encoded); } // end func getValue() + /** * Returns the type of this element. @@ -456,7 +464,7 @@ * @access public */ function getValidator() { - return array($this->validator, $this->validator_callback, $this->js_validator); + return $this->container->getValidator(); } // end func getValidator /** @@ -470,15 +478,7 @@ * @see getValidator() */ function setValidator($validator, $flag_callback = false) { - if ('string' != gettype($validator)) { - $this->exceptions[] = new form_error('The name of a validator must be a string.', __FILE__, __LINE__); - return false; - } - - $this->validator = $validator; - $this->validator_callback = $flag_callback; - - return true; + return $this->container->getValidator(); } // end func setValidator /** @@ -487,7 +487,8 @@ * @access public */ function freeze() { - $this->frozen = true; + $this->container->freeze(); + $this->freeze = true; } // end func freeze /** @@ -496,6 +497,7 @@ * @access public */ function unfreeze() { + $this->container->unfreeze(); $this->frozen = false; } // end func unfreeze @@ -538,7 +540,7 @@ function getfrozen($subelement = '') { return sprintf('%s%s', $this->getFrozenHiddenElement(), - $this->value + $this->container->getValue() ); } // end func getfrozen() @@ -547,9 +549,9 @@ * * @brother get() */ - function show($value = '') { + function show($subelement = '') { - $html = $this->get($value); + $html = $this->get($subelement); print $html; return $html; @@ -564,80 +566,10 @@ * @abstract */ function validate() { - return ''; + return $this->container->validate(); } // end func validate /** - * Validate the length of the input value. - * - * @param mixed $value Optional value, if not specified $this->value gets used. - * @return string $errormessage - */ - function validateLength($value = '') { - - $value = ($value) ? $value : $this->value; - - if ($this->doValidation('length') && (strlen($value) < $this->minlength || ($this->maxlength >= 0 && strlen($value) > $this->maxlength))) - return $this->length_e . ' '; - return ''; - } // end func validateLength - - /** - * Compare the element value with the specified RegExp. - * - * @param mixed $value Optional value, if not specified $this->value gets used. - * @return string $errormessage - * @see validateLength() - */ - function validateRegEx($value = '') { - - $reg = '/' . $this->valid_regex . '/'; - $reg.= ($this->valid_icase) ? 'i' : ''; - if ($this->doValidation('valid') && !preg_match($reg, ($value) ? $value : $this->value)) { - return $this->valid_e . ' '; - } - return ''; - } // end func validateRegEx - - /** - * Validate this event? - * - * @param string $event - * @return boolean $ok - * @throws form_error - */ - function doValidation($event) { - if ('' == $event || !isset($this->val_events[$event])) { - $this->exceptions[] = new form_error("Unknown event: '$event'", __FILE__, __LINE__); - return false; - } - - if (false == $this->val_events[$event] || true == $this->frozen) - return false; - - $ok = false; - switch (strtolower($event)) { - - case 'length': - if ('' != $this->length_e && (0 != $this->minlength || $this->maxlength >= 0) ) - $ok = true; - break; - - case 'valid': - if ('' != $this->valid_e && '' != $this->valid_regex) - $ok = true; - break; - - case 'intro': - if ('' != $this->intro_e && '' != $this->intro) - $ok = true; - break; - } - - return $ok; - } // end func doValidation - - /** * Defines the events that will be validated. * * You can tell every (default) validator which 'events' @@ -667,28 +599,7 @@ * @see $val_events */ function react($events, $status = true) { - - if ('all' == $events) - $events = $this->val_events; - - if (!is_array($events) && isset($this->val_events[$events])) - $events = array($events => $events); - - if (!is_array($events)) { - $this->exceptions[] = new form_error("Unknown event '$events'.", __FILE__, __LINE__); - return false; - } - - $status = ($status) ? true : false; - $ok = true; - - foreach ($events as $event => $v) - if (isset($this->val_events[$event])) - $this->val_events[$event] = $status; - else - $ok = false; - - return $ok; + return $this->container($events, $status); } // end func react /** @@ -698,10 +609,112 @@ * @access public */ function setValue($value) { - $this->value = $value; + $this->container->setValue($value); } // end func setValue /** + * Returns JavaScript validation code. + * + * See form_js_simple.js and form_js_complex.js for details on the JavaScript + * validation code. This function build a record to 'feed' the JavaScript form + * object. + * + * @param boolean Build JS for form_js_complex.js? + * @access public + * @return array + */ + function getJS($complex = false) { + + $eldata = ''; + $events = ''; + + // Do not change the order of the code! + + // decode userinput + list($decode_func, $decode_fname) = $this->container->getJSdecode(); + if ($decode_fname) + $eldata .= sprintf('"d", "%s", ', $decode_fname); + + // encode userinput + list($encode_func, $encode_fname) = $this->container->getJSencode(); + if ($encode_fname) + $eldata .= sprintf('"e", "%s", ', $encode_fname); + + // set value function + list($setvalue_func, $setvalue_fname) = $this->getJSsetvalue(); + if ($setvalue_fname && $encode_fname) + $eldata .= sprintf('"s", "%s", ', $setvalue_fname); + + // Function used to get the current value of the element. + // Caution: 'v' must be the first entry in the validation data record. + // 'v', 'my_get_value_function' + list($getvalue_func, $getvalue_fname) = $this->getJSvalue(); + if (!$complex && $getvalue_fname) + $eldata .= sprintf('"v", "%s", ', $getvalue_fname); + + list($c_eldata, $events) = $this->container->getJS($complex); + $eldata .= $c_eldata; + + // format record entry + if ($eldata) { + $eldata = sprintf('"%s", [%s], ', + $this->name, + substr($eldata, 0, -2) + ); + if ($complex) + $eldata .= sprintf('"%s",%s', $events, "\n"); + } + + return array( + "eldata" => $eldata, + "js_validator" => $this->js_validator, + "getvalue_func" => $getvalue_func, + "getvalue_fname" => $getvalue_fname, + "decode_func" => $decode_func, + "decode_fname" => $decode_fname, + "encode_func" => $encode_func, + "encode_fname" => $encode_fname, + "setvalue_func" => $setvalue_func, + "setvalue_fname" => $setvalue_fname + ); + + } // end func getJS + + + /** + * JavaScript function used to set the value of the element. + * + * @return array [function, function name] + */ + function getJSsetvalue() { + /* return array('function fe_setv(f, e, v) { document.forms[f].elements[e].value = v; }', 'fe_setv'); */ + return array('', ''); + } // end func getJSsetvalue + + + /** + * JavaScript function used to get the value of the element. + * + * Most elements can use document.forms[..].elements[...].value to get the + * value of an element but some need a more sophisticated function. These + * elements must override this function. + * + * The JavaScript function provided by this elements should be names 'form_elmenttype', + * e.g. 'form_select'. The (JS) function gets called with two arguments: + * string formname, string elementname. It returns the value of the element, see + * form_element_select for an example. + * + * The function returns the JavaScript code itself and a name for the code + * snippet that allows the form object to avoid sending the code twice. + * + * @return array [JavaScript function code, JavaScript function name] + */ + function getJSvalue() { + return array('["", ""]', ""); + } // end func getJSvalue + + + /** * Return a JavaScript onChange attribute if strong Javascript validation is active * * @return string @@ -751,7 +764,7 @@ return sprintf('<input type="hidden" name="%s" value="%s">', $this->name, - ('' != $value) ? $value : $this->value + ('' != $value) ? $value : $this->container->getValue() ); } // end func getFrozenHiddenElement @@ -768,104 +781,12 @@ return sprintf('<table cellpadding="0" cellspacing="0" %s><tr><td>%s</td></tr></table>', ($border) ? ' border="1"' : '', - ('' != $value) ? $value : $this->value + ('' != $value) ? $value : $this->container->getValue() ); } // end func getFrozenTable /** - * Returns JavaScript validation code. - * - * See form_js_simple.js and form_js_complex.js for details on the JavaScript - * validation code. This function build a record to 'feed' the JavaScript form - * object. - * - * @param boolean Build JS for form_js_complex.js? - * @access public - * @return array - */ - function getJS($complex = false) { - - $eldata = ''; - $events = ''; - list($getvalue_func, $getvalue_fname) = $this->getJSvalue(); - - // Function used to get the current value of the element. - // Caution: 'v' must be the first entry in the validation data record. - // Do not change the order of the code! - // 'v', 'my_get_value_function' - if (!$complex && $getvalue_fname) - $eldata .= sprintf('"v", "%s", ', $getvalue_fname); - - // Length validation: 'l', minlength, maxlength, 'length_e' - if ($this->doValidation('length')) { - $eldata .= sprintf('"l", %d, %d, "%s", ', - $this->minlength, - $this->maxlength, - str_replace('"', '\"', $this->length_e) - ); - $events .= 'l'; - } - - // RegExp validation: 'r', 'valid_regex', 'valid_modifier', 'valid_e' - if ($this->doValidation('valid')) { - $eldata .= sprintf('"r", "%s", "%s%s", "%s", ', - str_replace('"', '\"', $this->valid_regex), - ($this->valid_icase) ? 'i' : '', - (isset($this->valid_global) && $this->valid_global) ? 'g' : '', - str_replace('"', '\"', $this->valid_e) - ); - $events .= 'r'; - } - - // Select / Intro validation: 'i', 'intro', 'intro_e' - if ($this->doValidation('intro')) { - - reset($this->intro); - list($intro_value, $intro_label) = each($this->intro); - - $eldata .= sprintf('"i", "%s", "%s", ', - $intro_value, - str_replace('"', '\"', $this->intro_e) - ); - $events .= 'i'; - } - - // format record entry - if ($eldata) { - $eldata = sprintf('"%s", [%s], ', - $this->name, - substr($eldata, 0, -2) - ); - if ($complex) - $eldata .= sprintf('"%s",%s', $events, "\n"); - } - - return array($eldata, $getvalue_func, $getvalue_fname, $this->js_validator); - } // end func getJS - - /** - * JavaScript function used to get the value of the element. - * - * Most elements can use document.forms[..].elements[...].value to get the - * value of an element but some need a more sophisticated function. These - * elements must override this function. - * - * The JavaScript function provided by this elements should be names 'form_elmenttype', - * e.g. 'form_select'. The (JS) function gets called with two arguments: - * string formname, string elementname. It returns the value of the element, see - * form_element_select for an example. - * - * The function returns the JavaScript code itself and a name for the code - * snippet that allows the form object to avoid sending the code twice. - * - * @return array [JavaScript function code, JavaScript function name] - */ - function getJSvalue() { - return array('["", ""]', ""); - } // end func getJSvalue - - /** * Returns additional JavaScript required by some complex form elements. * * Some elements need extra JavaScript functionality to be @@ -901,6 +822,42 @@ function isHidden() { return $this->flag_hidden; } // end func isHidden + + + /** + * Returns the name of the custom validation function. + * + * @return array [validator function name, flag callback, js_validator] + * @see setValidator() + * @access public + */ + function getValidator() { + return array($this->validator, $this->validator_callback, $this->js_validator); + } // end func getValidator + + + /** + * Sets the name of the custom validation function. + * + * @param string $validator + * @param boolean use a callback function? + * @return boolean $ok + * @throws form_error + * @access public + * @see getValidator() + */ + function setValidator($validator, $flag_callback = false) { + if ('string' != gettype($validator)) { + $this->exceptions[] = new form_error('The name of a validator must be a string.', __FILE__, __LINE__); + return false; + } + + $this->validator = $validator; + $this->validator_callback = $flag_callback; + + return true; + } // end func setValidator + } // end class form_element ?>