--- nfo/php/libs/com.newsblob.phphtmllib/form/FormContent.inc 2004/05/06 16:27:22 1.3
+++ nfo/php/libs/com.newsblob.phphtmllib/form/FormContent.inc 2005/08/11 14:09:26 1.4
@@ -2,9 +2,9 @@
/**
* This file contains the FormContent class.
*
- * $Id: FormContent.inc,v 1.3 2004/05/06 16:27:22 jonen Exp $
+ * $Id: FormContent.inc,v 1.4 2005/08/11 14:09:26 jonen Exp $
*
- * @author Walter A. Boring IV
+ * @author Walter A. Boring IV
* @author Suren Markossian
* @package phpHtmlLib
* @subpackage FormProcessing
@@ -21,11 +21,11 @@
/**
* This class is used to build and render the form.
* It builds a form by creating FormElement objects
- * which have automatic validation. It leaves the
+ * which have automatic validation. It leaves the
* layout of the form up to the child class. It has
- * a mechanism in place to automagically show a
+ * a mechanism in place to automagically show a
* confirmation 'page' after the data has been submitted
- * and validated. It also provides a hook for any
+ * and validated. It also provides a hook for any
* 'back end' validation of data. Finally, it provides
* a function for handling the action of the form, which
* only gets call after ALL validation has passed.
@@ -36,8 +36,8 @@
* build the FormElement objects that will be used
* by the form. This function is called EVERY time
* the FormContent class is instantiated. After you
- * create the FormElement, you call the
- * FormContent::add_element() method, to add the
+ * create the FormElement, you call the
+ * FormContent::add_element() method, to add the
* FormElement object to the form. You will then
* call the 2 methods FormContent::element_label() and
* FormContent::element_form() to get access to the
@@ -60,15 +60,15 @@
*
*
* form_backend_validation() - This method enables you to
- * do any "back end" validation of data. Such as, check
+ * do any "back end" validation of data. Such as, check
* for a duplicate in the DB on a create/new form. This
* is called after the FormElement's validation methods have
* passed.
*
* form_action() - This method is called after ALL validation
- * was successfull, including each FormElement object's
+ * was successfull, including each FormElement object's
* validation methods, as well as the FormContent::form_backend_validation()
- *
+ *
*
*
* @package phpHtmlLib
@@ -171,13 +171,19 @@
var $_required_field_marker = "*";
/**
+ * The form Element's flag to add the :
+ * character at the end of the label.
+ */
+ var $_label_colon_flag = FALSE;
+
+ /**
* This holds how many actions we have
* for this form content
*/
var $_action_counter = 0;
/**
- * Automatically strip slashes from
+ * Automatically strip slashes from
* form values?
*/
var $_stripslashes = FALSE;
@@ -197,12 +203,35 @@
/**
* The onsubmit value for the form
- * tag. FormElement childs can
+ * tag. FormElement childs can
* automatically add to this
- * by implementing the
+ * by implementing the
* form_tag_onsubmit() method
*/
var $_form_on_submit = '';
+
+ /**
+ * The onsubmit value for the form
+ * tag. This is used by form action
+ * elements to add disable on submit
+ */
+ var $_form_action_elements_on_submit = '';
+
+ /**
+ * This is the FormValidation object
+ * used to validate the form elements
+ */
+ var $_FormValidation = NULL;
+
+
+ /**
+ * Indicates this entire FormContent
+ * is in readonly mode. This forces
+ * all of the FormElement to be in
+ * readonly mode.
+ *
+ */
+ var $_is_readonly = FALSE;
function FormContent($width="100%", $cancel_action=NULL) {
@@ -267,7 +296,7 @@
}
/**
- * This method allows this class to do any
+ * This method allows this class to do any
* data munging prior to the form_confirm
* method being called @ render time.
*
@@ -294,7 +323,7 @@
$this->_elements[$name]->_pre_confirm();
}
}
-
+
/**
* This function is used to show an intermediary
@@ -311,7 +340,7 @@
* @param string - the title for the table
* @param boolean - show the action buttons?
* @return mixed - either raw html, or some
- * container HTMLTag object.
+ * container HTMLTag object.
*/
function form_confirm( $title = "Form Confirmation", $show_buttons=TRUE ) {
$table = new InfoTable($title, $this->_width);
@@ -346,10 +375,9 @@
*/
function build_confirm_table( &$table ) {
foreach( $this->_elements as $label => $element) {
- $c = container(_HTML_SPACE,_HTML_SPACE,
- $element->get_value_text());
+ $c = container(_HTML_SPACE, $element->get_value_text());
$c->set_collapse();
- $div = html_div("", $element->get_label() );
+ $div = html_div("", $element->get_label($this) );
$div->set_style("white-space:nowrap;");
$table->add_row( $div, $c);
}
@@ -360,7 +388,7 @@
* have all been validated, and the form_confirm
* has been confirmed. It enables the form to
* validate any data against a DB or other backend
- * processing. This will always get called
+ * processing. This will always get called
* before the form_action method is called to ensure
* that all form data is valid before form_action()
* is called.
@@ -429,27 +457,25 @@
* @return TABLEtag object.
*/
function form_errors() {
- $table = new InfoTable($this->_form_errors_title, $this->get_form_errors_width());
+ $table = new ErrorBoxWidget($this->_form_errors_title,
+ $this->get_form_errors_width());
$errors = FALSE;
- //walk each visible form element and see if there is an error in it
- foreach( $this->_elements as $label => $element ) {
- if ($element->has_error()) {
- $e_errors = $element->get_errors();
- foreach( $e_errors as $err ) {
- $span = new SPANtag( array("style" => "padding-right:10px;white-space:nowrap;"),
- $err['label'] );
- $table->add_row($span, $err['message']);
- }
-
- $errors = TRUE;
- }
- }
- if ($errors) {
- return $table;
- } else {
- return NULL;
- }
+ //walk each visible form element and see if there is an error in it
+ foreach( $this->_elements as $label => $element ) {
+ if ($element->has_error()) {
+ $e_errors = $element->get_errors();
+ foreach( $e_errors as $err ) {
+ $table->add($err['label'], $err['message']);
+ }
+ $errors = TRUE;
+ }
+ }
+ if ($errors) {
+ return $table;
+ } else {
+ return NULL;
+ }
}
@@ -467,7 +493,7 @@
$errors = $element->get_errors();
foreach ( $errors as $err ) {
$ret[$err['label']] = $err['message'];
- }
+ }
}
}
return $ret;
@@ -499,6 +525,10 @@
if ($element->_has_form_on_submit) {
$this->_form_on_submit .= $element->form_tag_onsubmit();
}
+
+ if ($this->_label_colon_flag) {
+ $element->set_colon_flag(TRUE);
+ }
}
/**
@@ -510,8 +540,57 @@
function add_hidden_element( $label, $value=NULL ) {
$element = new FEHidden( $label, $value );
$element->set_stripslashes( $this->_stripslashes );
+ $element->set_form_name( $this->_form_name );
$this->_hidden_elements[$label] = &$element;
}
+
+ /**
+ * This function adds a form submit button
+ * with the appropriate action.
+ *
+ * @param string - the labe/action for the submit
+ * button.
+ * @param bool disable the button
+ * @param string - the onSubmit function (if any)
+ * @param boolean - disable opon submit?
+ * @return INPUTtag object
+ */
+ function add_action_element($label, $disabled = false, $submit_function = false,
+ $disable_on_submit = TRUE) {
+
+ $style="vertical-align:middle;";
+ // here we mantain a fixed button size if a label length is reasonably small
+ // i.e. OK or Cancel buttons or use padding otherwise
+ if (strlen($label)<8) $style .= "width:100px;";
+ else $style .= "padding-left:5px;padding-right:5px;";
+
+ $button_name = FORM_ACTION . $this->_action_counter++;
+ $button_id = $this->_form_name . "action_" . $button_name;
+
+ $attributes = array("id" => $button_id,
+ "onclick" =>"this.form." . FORM_ACTION . ".value='".$label."';",
+ "style" => $style);
+
+ if ($disabled) $attributes[] = "disabled";
+
+ $this->_action_elements[$label] = form_submit($button_name, $label, $attributes);
+
+ if ($disable_on_submit) {
+ $this->_form_action_elements_on_submit .= "if (document.getElementById('" . $button_id . "')) document.getElementById('" . $button_id . "').disabled=true;";
+ }
+ }
+
+ /**
+ * This function returns a submit action
+ * by label
+ *
+ * @param string label
+ *
+ * @return object
+ */
+ function get_action_element($label) {
+ return $this->_action_elements[$label];
+ }
/**
* This method returns the label object
@@ -534,7 +613,7 @@
*/
function hidden_element_label($label) {
$this->_test_element($label, "element_label", TRUE);
- return $this->_hidden_elements[$label]->get_label();
+ return $this->_hidden_elements[$label]->get_label($this);
}
/**
@@ -547,7 +626,7 @@
*/
function element_form($label) {
$this->_test_element($label, "element_form");
- return $this->_elements[$label]->get_element();
+ return $this->_elements[$label]->get_form_element($this->is_readonly());
}
/**
@@ -615,7 +694,7 @@
/**
* This method is a helper method to set the
* FormElement's tabindex.
- *
+ *
* @param string - the form label
* @param int - the FormElement's tabindex
* @return none
@@ -628,7 +707,7 @@
/**
* This method is a helper method to get the
* FormElement's tabindex.
- *
+ *
* @param string - the form label
* @param int - the FormElement's tabindex
* @return the FormElement's current tabindex
@@ -638,12 +717,21 @@
return $this->_elements[$label]->get_tabindex();
}
-
+ /**
+ * This method is used to test
+ * if the element exists in a form
+ *
+ * @param string - the form label
+ * @return bool
+ */
+ function element_exists($label) {
+ return (isset($this->_elements[$label]) || isset($this->_hidden_elements[$label]));
+ }
/**
* This method is used to create a new error element
* during the call to form_action(). This enables us
- * to do error handling during a transaction into
+ * to do error handling during a transaction into
* a DB.
*
* @param string - the label
@@ -654,10 +742,31 @@
$this->_elements[$label]->set_error_message($message);
} else {
$this->add_element( new FEError($label, $message) );
- }
+ }
}
+ /**
+ * This sets the readonly flag.
+ * When this flag is set, all of the
+ * FormElements will be set to readonly.
+ *
+ * @param bool TRUE = readonly
+ */
+ function set_readonly($flag=TRUE) {
+ $this->_is_readonly = $flag;
+ }
+
+ /**
+ * Is this element in read only mode?
+ *
+ * @return bool
+ */
+ function is_readonly() {
+ return $this->_is_readonly;
+ }
+
+
/*****************************/
/* Util methods */
/*****************************/
@@ -666,6 +775,10 @@
$this->_form_name = $name;
}
+ function get_form_name() {
+ return $this->_form_name;
+ }
+
/**
* Save the action for the form
@@ -709,7 +822,7 @@
return $this->_width;
} else {
return $this->_form_errors_width;
- }
+ }
}
/**
@@ -718,7 +831,7 @@
* By default it uses the width of the
* form as its size.
*
- * @param string - the width
+ * @param string - the width
*/
function set_form_errors_width($width) {
$this->_form_errors_width = $width;
@@ -734,8 +847,8 @@
function set_form_errors_title($title) {
$this->_form_errors_title = $title;
}
-
-
+
+
/**
@@ -815,7 +928,7 @@
/**
* This sets the action message.
* This is called from withint the
- * form_action() method
+ * form_action() method
*
* @param string - the action message
*/
@@ -826,7 +939,7 @@
/**
* This method sets the javasript action
- * to be taken when the cancel button
+ * to be taken when the cancel button
* is clicked. Calling this method
* w/ a non NULL value automatically enables
* the Cancel button.
@@ -837,7 +950,15 @@
$this->_cancel_action = $action;
}
-
+ /**
+ * This method sets the flag to have ALL of the
+ * FormElement's labels be appended with a :
+ * character. Some folks like this for some
+ * completely unexplained reason.
+ */
+ function set_colon_flag($flag=TRUE) {
+ $this->_label_colon_flag = $flag;
+ }
@@ -867,7 +988,7 @@
$blabel = FORM_ACTION.$this->_action_counter;
$this->_action_counter++;
- // The onClick is responsible for:
+ // The onclick is responsible for:
// -disabling the action button to discourage stupid users
// -setting the action in the _form_action hidden variable
// -calling the onSubmit function (if any), since form.submit() will not trigger
@@ -883,10 +1004,13 @@
if ($submit_function) {
$onclick .= $submit_function.";";
}
- $onclick .= "this.form.submit();";
+
+ if ($disable_on_submit) {
+ $onclick .= "this.form.submit();";
+ }
$attrib = array("style" => $style,
- "onClick" => $onclick);
+ "onclick" => $onclick);
if ($disabled) $attrib[] = "disabled";
return form_submit($blabel, $label, $attrib);
@@ -923,11 +1047,11 @@
$style = "padding-left: 5px;padding-right:5px;";
$attrib = array("style" => $style,
- "onClick" => $onclick);
+ "onclick" => $onclick);
$container->push(form_submit($blabel, $label,
array("style" => $style,
- "onClick" => $onclick)));
+ "onclick" => $onclick)));
return $container;
}
@@ -957,7 +1081,7 @@
$click .= " if(window.global_cntr<2) {";
$click .= $onclick."}";
}
-
+
$img = html_img($image_name);
$img->set_tag_attribute("border", 0 );
$img->set_tag_attribute("style", "cursor:hand;");
@@ -969,10 +1093,10 @@
/**
- * build a cancel button with a url
+ * build a cancel button with a url
* to go to
*
- * @param string - the cancel action
+ * @param string - the cancel action
* @return form button
*/
function add_cancel() {
@@ -980,21 +1104,34 @@
$cancel_button = form_button("cancel","Cancel",
array("type"=>"button",
"style"=>"width: 90px;",
- "onClick"=>
+ "onclick"=>
"javascript:document.location='".$this->_cancel_action."'"));
$cancel_button->set_style("vertical-align:middle");
return $cancel_button;
}
-
+
/**
* This function is used to set the required field marker
*
* @param string - the marker
*/
function set_required_marker($marker) {
- $this->_required_field_marker = $marker;
+ $this->_required_field_marker = $marker;
+ }
+
+ /**
+ * This function is used to get the required field marker
+ *
+ * @return string - the marker
+ */
+ function get_required_marker() {
+ if (is_object($this->_required_field_marker)) {
+ return $this->_required_field_marker->render();
+ } else {
+ return $this->_required_field_marker;
+ }
}
-
+
/**
* This sets the required text
*
@@ -1002,7 +1139,7 @@
*/
function set_required_text($text) {
$this->_required_field_text = $text;
-
+
}
@@ -1024,9 +1161,36 @@
if (!isset($this->_elements[$label])) {
trigger_error("FormContent::".$method."() - '".$label."' element doesn't exist", E_USER_ERROR);
}
- }
+ }
}
+ /**
+ * return the values of all visible form elements as an array
+ *
+ * @access public
+ * @return array
+ *
+ */
+ function get_all_element_values()
+ {
+
+ foreach ($this->_elements as $element) {
+ $data[$element->get_element_name()] = $element->get_value();
+ }
+
+ return $data;
+
+ }
+
+ /**
+ * This method is used to keep a local reference
+ * of the FormProcessor's FormValidation object
+ *
+ * @param FormValidation object
+ */
+ function _set_validation_object(&$FormValidation) {
+ $this->_FormValidation =& $FormValidation;
+ }
}
?>