Date: 05/19/01
- Next message: uw: "[phplib-dev] cvs commit"
- Previous message: uw: "[phplib-dev] cvs commit"
- Next in thread: uw: "[phplib-dev] cvs commit"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
From: uw
Date: Sat May 19 22:51:18 2001
Modified files:
php-lib/pages/form/xml/tests/form_defaults.php
php-lib/pages/form/xml/tests/form_tag.php
php-lib/pages/form/xml/tests/form_text.php
php-lib/pages/form/xml/tests/form_text.xml
php-lib/php/form/form.inc
php-lib/php/form/form_xmlfactory.inc
php-lib/php/form/elements/form_element.inc
php-lib/php/form/xml/form.xml
Log message:
- fixed custom validators
Index: php-lib/pages/form/xml/tests/form_defaults.php
diff -u php-lib/pages/form/xml/tests/form_defaults.php:1.1 php-lib/pages/form/xml/tests/form_defaults.php:1.2
--- php-lib/pages/form/xml/tests/form_defaults.php:1.1 Sat May 19 21:10:59 2001
+++ php-lib/pages/form/xml/tests/form_defaults.php Sat May 19 22:50:44 2001
@@ -54,7 +54,9 @@
<tr>
<td align="left" valign="top">XML File</td>
<td align="left" valign="top">
- <?php print nl2br(str_replace("<" , "<", implode("", file(FILE . ".xml")))); ?>
+ <pre>
+ <?php print str_replace("\t", " ", str_replace("<" , "<", implode("", file(FILE . ".xml")))); ?>
+ </pre>
</td>
</tr>
Index: php-lib/pages/form/xml/tests/form_tag.php
diff -u php-lib/pages/form/xml/tests/form_tag.php:1.1 php-lib/pages/form/xml/tests/form_tag.php:1.2
--- php-lib/pages/form/xml/tests/form_tag.php:1.1 Sat May 19 20:54:50 2001
+++ php-lib/pages/form/xml/tests/form_tag.php Sat May 19 22:50:44 2001
@@ -55,7 +55,9 @@
<tr>
<td align="left" valign="top">XML File</td>
<td align="left" valign="top">
- <?php print nl2br(str_replace("<" , "<", implode("", file(FILE . ".xml")))); ?>
+ <pre>
+ <?php print str_replace("\t", " ", str_replace("<" , "<", implode("", file(FILE . ".xml")))); ?>
+ </pre>
</td>
</tr>
Index: php-lib/pages/form/xml/tests/form_text.php
diff -u php-lib/pages/form/xml/tests/form_text.php:1.1 php-lib/pages/form/xml/tests/form_text.php:1.2
--- php-lib/pages/form/xml/tests/form_text.php:1.1 Sat May 19 22:07:20 2001
+++ php-lib/pages/form/xml/tests/form_text.php Sat May 19 22:50:45 2001
@@ -15,8 +15,9 @@
include_once(FORM_INCLUDE_DIR . "form.inc");
include_once(FORM_INCLUDE_DIR . "form_xmlfactory.inc");
- function callback($form) {
- print_r($form);
+ function callback(&$form) {
+ print "callback() before validation...";
+ $form->setValidationError("submit", "Set by the custom validator.");
}
$x = new form_xmlfactory(FILE . ".xml");
@@ -35,6 +36,12 @@
$x->showElement("elname_text");
$x->showElement("submit");
$x->Finish();
+ $x->validate();
+
+ print "\n\nValidation results:\n\n";
+ list($allmsg, $flags) = $x->getValidationResult();
+ foreach ($flags as $field => $msg)
+ printf("%15s %s\n", $field, $msg);
?>
</pre>
@@ -49,7 +56,9 @@
<tr>
<td align="left" valign="top">XML File</td>
<td align="left" valign="top">
- <?php print nl2br(str_replace("<" , "<", implode("", file(FILE . ".xml")))); ?>
+ <pre>
+ <?php print str_replace("\t", " ", str_replace("<" , "<", implode("", file(FILE . ".xml")))); ?>
+ </pre>
</td>
</tr>
Index: php-lib/pages/form/xml/tests/form_text.xml
diff -u php-lib/pages/form/xml/tests/form_text.xml:1.1 php-lib/pages/form/xml/tests/form_text.xml:1.2
--- php-lib/pages/form/xml/tests/form_text.xml:1.1 Sat May 19 22:07:21 2001
+++ php-lib/pages/form/xml/tests/form_text.xml Sat May 19 22:50:45 2001
@@ -28,7 +28,7 @@
<validation>
<length min="1" max="3">length_e</length>
<regexp reg="[a-z]" icase="true">valid_e</regexp>
- <phpfunction>callback</phpfunction>
+ <phpfunction callback="true">callback</phpfunction>
<jsfunction></jsfunction>
</validation>
</text>
Index: php-lib/php/form/form.inc
diff -u php-lib/php/form/form.inc:1.31 php-lib/php/form/form.inc:1.32
--- php-lib/php/form/form.inc:1.31 Sat May 19 20:56:44 2001
+++ php-lib/php/form/form.inc Sat May 19 22:50:46 2001
@@ -28,7 +28,7 @@
* - tree (select box with options show as a tree)
*
* <email protected> Ulf Wendel <ulf.wendel <email protected>>
-* <email protected> $Id: form.inc,v 1.31 2001/05/19 18:56:44 uw Exp $
+* <email protected> $Id: form.inc,v 1.32 2001/05/19 20:50:46 uw Exp $
* <email protected> public
* <email protected> Form
*/
@@ -149,7 +149,7 @@
* <email protected> string
* <email protected> $js_error_postfix, setJSError()
*/
- var $js_error_prefix = "There're errors in the form: \n\n";
+ var $js_error_prefix = "There're errors in the form: \\n\\n";
/**
* JavaScript error message postfix.
@@ -159,7 +159,7 @@
* <email protected> string
* <email protected> $js_error_prefix, setJSError()
*/
- var $js_error_postfix = "\n\nPlease correct the input.";
+ var $js_error_postfix = "\\n\\nPlease correct the input.";
/**
* Method used in the form.
@@ -426,24 +426,30 @@
/**
* Sets the name of a custom validation function.
- *
- * <email protected> string $name element name
- * <email protected> string $validator name of validation function
+ *
+ * Custom validators are called before default validation
+ * takes place. Custom validators can me methods of the form object
+ * (default) or "callback" functions which take the form object as a
+ * parameter (passed by reference).
+ *
+ * <email protected> string element name
+ * <email protected> string name of validation function
+ * <email protected> boolean use a callback function
* <email protected> form_error
* <email protected> boolean
*/
- function setValidator($name, $validator) {
+ function setValidator($name, $validator, $flag_callback = false) {
if (!$this->ElementExists($name)) {
$this->exceptions[] = new form_error("'$name' is not a known element", __FILE__, __LINE__);
return false;
}
- if (!method_exists($this, $validator)) {
+ if (!$flag_callback && !method_exists($this, $validator)) {
$this->exceptions[] = new form_error("Can't find the method '$validator'", __FILE__, __LINE__);
return false;
}
- $this->elements[$name]->setValidator($validator);
+ $this->elements[$name]->setValidator($validator, $flag_callback);
return true;
} // end func setValidator
@@ -503,15 +509,13 @@
function getFinish($additional_html = "", $sess = "") {
$html = ("" != $additional_html) ? $additional_html : $this->additional_html;
-
+
if (0 != count($this->hidden_elements)) {
foreach ($this->hidden_elements as $k => $elname)
$html .= $this->getElement($elname);
}
-
- $html .= "</form>" . $this->CR_HTML . $this->getJS();
-
- return $html;
+
+ return $html . "</form>" . $this->CR_HTML .$this->getJS();;
} // end func getFinish
/**
@@ -697,7 +701,7 @@
* <email protected> public
*/
function validate($vallist = "") {
-
+
if ($vallist && !is_array($vallist))
$vallist = array($vallist);
@@ -713,8 +717,15 @@
$this->validation_msg = "";
$this->validation_flags = array();
- foreach ($this->getCustomValidators($vallist) as $k => $validator)
- $this->${validator}();
+ foreach ($this->getCustomValidators($vallist) as $k => $validator) {
+
+ $func = $validator["php"];
+ if ($validator["callback"])
+ ${func}(&$this);
+ else
+ $this->${func}();
+
+ }
foreach ($vallist as $k => $name)
if (isset($this->elements[$name]) && $message = $this->elements[$name]->validate())
@@ -759,11 +770,12 @@
/**
* Returns an array of all custom validation functions
+ *
* <email protected> mixed $ellist
- * <email protected> array $validators $k => $custom_function
+ * <email protected> array $validators [$custom_validator => $flag_callback]
*/
function getCustomValidators($ellist = "") {
-
+
if (!is_array($ellist) && isset($this->elements[$ellist]))
$ellist = array($ellist);
@@ -774,8 +786,12 @@
foreach ($ellist as $name => $v)
if (isset($this->elements[$name])) {
$validator = $this->elements[$name]->getValidator();
- if ("" != $validator)
- $validators[] = $validator;
+ if ("" != $validator[0])
+ $validators[] = array(
+ "php" => $validator[0],
+ "callback" => $validator[1],
+ "js" => $validator[2]
+ );
}
return $validators;
@@ -860,13 +876,24 @@
return "";
$nolanguage = "";
+
// 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";
-
+ // and decide which js file to take
+
+
+ $validators = array();
+ foreach ($this->getCustomValidators() as $k => $validator)
+ if ("" != $validator["js"])
+ $validators[] = $validator["js"];
+
+ if (0 != count($validators)) {
+ $complex = true;
+ $file = "form_js_complex.js";
+ } else {
+ $complex = false;
+ $file = "form_js_simple.js";
+ }
+
// get the code
$file = FORM_INCLUDE_DIR . '/js/' . $file;
$fh = fopen($file, "r");
@@ -876,7 +903,7 @@
}
$base_code = fread($fh, filesize($file));
fclose($fh);
-
+
$js = "";
$custom = "";
$get_value = "";
@@ -940,7 +967,7 @@
$js = sprintf('[%s]', substr($js, 0, -2));
// ;-) compress the JS code....
- $base_code = $this->compressJS($base_code);
+# $base_code = $this->compressJS($base_code);
// customize the error message
$base_code = str_replace("{ERROR_MSG_PREFIX}", $this->js_error_prefix, $base_code);
Index: php-lib/php/form/form_xmlfactory.inc
diff -u php-lib/php/form/form_xmlfactory.inc:1.7 php-lib/php/form/form_xmlfactory.inc:1.8
--- php-lib/php/form/form_xmlfactory.inc:1.7 Sat May 19 20:56:45 2001
+++ php-lib/php/form/form_xmlfactory.inc Sat May 19 22:50:46 2001
@@ -157,9 +157,7 @@
$this->validation = array();
$this->parse($xml);
- if (!empty($this->defaults))
- $this->form->setDefaults($this->defaults);
-
+
if ("" != $this->js_error_prefix || "" != $this->js_error_postfix)
$this->form->setJSError($this->js_error_prefix, $this->js_error_postfix);
@@ -268,12 +266,12 @@
switch ($name) {
case "file":
if (isset($attrs["size"]))
- $this->defaults["filesize"] = (int)$attrs["size"];#
+ $this->defaults["filesize"] = (int)$attrs["size"];
break;
case "image":
if (isset($attrs["border"]))
- $this->defaults["imageborder"] = (int)$attrs["size"];
+ $this->defaults["imageborder"] = (int)$attrs["border"];
break;
case "text":
@@ -382,6 +380,11 @@
$this->form = new form($js_name, $method, $action, $name);
break;
+
+ case "phpfunction":
+ if (isset($attrs["callback"]))
+ $this->attributes["validator_callback"] = ("false" == $attrs["validator_callback"]) ? false : true;
+ break;
default:
break;
@@ -491,11 +494,23 @@
case "additionalhtml":
$this->form->setAdditionalHTML((string)$this->cdata);
break;
+
+ case "defaults":
+ if ($this->flag_defaults) {
+ $this->form->setDefaults($this->defaults);
+ $this->defaults = array();
+ $this->flag_defaults = false;
+ }
+
+ break;
default:
break;
}
-
+
+ // don't reuse the content
+ $this->cdata = "";
+
} // end func endElement
Index: php-lib/php/form/elements/form_element.inc
diff -u php-lib/php/form/elements/form_element.inc:1.5 php-lib/php/form/elements/form_element.inc:1.6
--- php-lib/php/form/elements/form_element.inc:1.5 Sat May 19 20:15:01 2001
+++ php-lib/php/form/elements/form_element.inc Sat May 19 22:50:47 2001
@@ -5,7 +5,7 @@
* Superclass of all form elements.
*
* <email protected> Ulf Wendel <uw <email protected>>
-* <email protected> $Id: form_element.inc,v 1.5 2001/05/19 18:15:01 uw Exp $
+* <email protected> $Id: form_element.inc,v 1.6 2001/05/19 20:50:47 uw Exp $
* <email protected> public
* <email protected> Form
* <email protected>
@@ -124,6 +124,7 @@
"additional_html" => "string",
"validator" => "string",
+ "validator_callback" => "boolean",
"js_validator" => "string",
"frozen" => "boolean",
@@ -175,6 +176,7 @@
*/
var $form_name = "";
+
/**
* Name of a custom validation function provided by the user.
*
@@ -183,7 +185,16 @@
* <email protected> string
*/
var $validator = "";
+
+ /**
+ * Is the custom validation function a callback function or a method of the form object?
+ *
+ * <email protected> boolean
+ */
+ var $validator_callback = false;
+
+
/**
* Custom JavaScript validator.
*
@@ -210,14 +221,12 @@
* All other values start JavaScript validation with "onSubmit".
*/
function form_element ($element_data, $method, $form_name = "", $js_mode = "") {
-$this->ed = $element_data;
- $ok = $this->checkConfiguration($element_data);
- if (!$ok)
+ if (!($ok = $this->checkConfiguration($element_data)))
return;
foreach ($element_data as $slot => $value)
if ("validator" == $slot)
- $this->setValidator($value);
+ $this->setValidator($value, (isset($element_data["validator_callback"])) ? $element_data["validator_callback"] : false);
else
$this->$slot = $value;
@@ -383,30 +392,33 @@
/**
* Returns the name of the custom validation function.
*
- * <email protected> string $validator
+ * <email protected> array [validator function name, flag callback, js_validator]
* <email protected> setValidator()
* <email protected> public
*/
function getValidator() {
- return $this->validator;
+ return array($this->validator, $this->validator_callback, $this->js_validator);
} // end func getValidator
/**
* Sets the name of the custom validation function.
*
* <email protected> string $validator
+ * <email protected> boolean use a callback function?
* <email protected> boolean $ok
* <email protected> form_error
* <email protected> public
* <email protected> getValidator()
*/
- function setValidator($validator) {
+ 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
Index: php-lib/php/form/xml/form.xml
diff -u php-lib/php/form/xml/form.xml:1.4 php-lib/php/form/xml/form.xml:1.5
--- php-lib/php/form/xml/form.xml:1.4 Sun May 13 17:52:56 2001
+++ php-lib/php/form/xml/form.xml Sat May 19 22:50:47 2001
@@ -17,7 +17,7 @@
<css class="css class" id="id">style</css>
</defaults>
- <elements nameprefix="prefix_">
+ <elements nameprefix="">
<calendar name="calendar" elname="calendar" frozen="false" minyear="2000" maxyear="2010" now="today" prev="<<" next=">>">
<preselect>
---------------------------------------------------------------------
To unsubscribe, e-mail: phplib-dev-unsubscribe <email protected>
For additional commands, e-mail: phplib-dev-help <email protected>
- Next message: uw: "[phplib-dev] cvs commit"
- Previous message: uw: "[phplib-dev] cvs commit"
- Next in thread: uw: "[phplib-dev] cvs commit"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]

