--- nfo/php/libs/com.newsblob.phphtmllib/form/FormContent.inc 2003/02/22 21:07:40 1.1 +++ nfo/php/libs/com.newsblob.phphtmllib/form/FormContent.inc 2003/09/20 00:18:43 1.2 @@ -2,7 +2,7 @@ /** * This file contains the FormContent class. * - * $Id: FormContent.inc,v 1.1 2003/02/22 21:07:40 jonen Exp $ + * $Id: FormContent.inc,v 1.2 2003/09/20 00:18:43 jonen Exp $ * * @author Walter A. Boring IV * @author Suren Markossian @@ -128,6 +128,20 @@ */ var $_width = "600"; + /** + * Holds the width (if any) of the errors + * table that will be rendered. If the + * value is null, the $this->_width value + * is used instead. + */ + var $_form_errors_width = null; + + + /** + * The form errors table title + */ + var $_form_errors_title = "Form Errors"; + /** * The message that is set @@ -156,6 +170,18 @@ */ var $_required_field_marker = "*"; + /** + * This holds how many actions we have + * for this form content + */ + var $_action_counter = 0; + + /** + * Automatically strip slashes from + * form values? + */ + var $_stripslashes = FALSE; + function FormContent($width="100%", $cancel_action=NULL) { @@ -208,6 +234,18 @@ } /** + * This method lets you provide any javascript + * that is associated with the form content. + * The FormProcessor will automatically call + * this and wrap it in a script tag. + * + * @return string - raw js. + */ + function javascript() { + return NULL; + } + + /** * This method allows this class to do any * data munging prior to the form_confirm * method being called @ render time. @@ -233,11 +271,13 @@ * have to do is show the data, and a confirm * submit button. * + * @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( ) { - $table = new InfoTable("Form Confirmation", $this->_width); + function form_confirm( $title = "Form Confirmation", $show_buttons=TRUE ) { + $table = new InfoTable($title, $this->_width); $this->build_confirm_table( $table ); @@ -251,7 +291,9 @@ $td->add(_HTML_SPACE, $this->add_cancel()); } - $table->add_row( $td ); + if ($show_buttons) { + $table->add_row( $td ); + } return $table; } @@ -361,15 +403,19 @@ * @return TABLEtag object. */ function form_errors() { - $table = new InfoTable("Form Errors", $this->_width); + $table = new InfoTable($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()) { - $span = new SPANtag( array("style" => "padding-right:10px;white-space:nowrap;"), - $label ); - $table->add_row($span, $element->get_error_message()); + $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; } } @@ -377,10 +423,31 @@ return $table; } else { return NULL; - } + } } + /** + * This method returns an array of errors + * for all the errors. + * + * @return array + */ + function get_error_array() { + $ret = array(); + //walk each visible form element and see if there is an error in it + foreach( $this->_elements as $label => $element ) { + if ($element->has_error()) { + $errors = $element->get_errors(); + foreach ( $errors as $err ) { + $ret[$err['label']] = $err['message']; + } + } + } + return $ret; + } + + /*****************************/ /* form element methods */ /*****************************/ @@ -392,6 +459,9 @@ * @param FormElement object */ function add_element( &$element ) { + $element->set_stripslashes( $this->_stripslashes ); + //in case the element needs it for js + $element->set_form_name( $this->_form_name ); $this->_elements[$element->get_label_text()] = &$element; } @@ -403,6 +473,7 @@ */ function add_hidden_element( $label, $value=NULL ) { $element = new FEHidden( $label, $value ); + $element->set_stripslashes( $this->_stripslashes ); $this->_hidden_elements[$label] = &$element; } @@ -414,10 +485,23 @@ * @return Object */ function element_label($label) { + $this->_test_element($label, "element_label"); return $this->_elements[$label]->get_label(); } /** + * This method returns the label object + * for a visible form element. + * + * @param string - the element's label + * @return Object + */ + function hidden_element_label($label) { + $this->_test_element($label, "element_label", TRUE); + return $this->_hidden_elements[$label]->get_label(); + } + + /** * This method returns the actual form * object that renders the form field. * Such as an INPUTtag object. @@ -426,6 +510,7 @@ * @return Object */ function element_form($label) { + $this->_test_element($label, "element_form"); return $this->_elements[$label]->get_element(); } @@ -437,6 +522,7 @@ * @return Object */ function &get_element($label) { + $this->_test_element($label, "get_element"); return $this->_elements[$label]; } @@ -449,6 +535,7 @@ * @param value - the new value */ function set_element_value($label, $value) { + $this->_test_element($label, "set_element_value"); $this->_elements[$label]->set_value($value); } @@ -461,6 +548,7 @@ * @return value - the new value */ function get_element_value($label) { + $this->_test_element($label, "get_element_value"); return $this->_elements[$label]->get_value(); } @@ -472,6 +560,7 @@ * @param value - the new value */ function set_hidden_element_value($label, $value) { + $this->_test_element($label, "set_hidden_element_value", TRUE); $this->_hidden_elements[$label]->set_value( $value ); } @@ -483,6 +572,7 @@ * @return value - the new value */ function get_hidden_element_value($label) { + $this->_test_element($label, "get_hidden_element_value", TRUE); return $this->_hidden_elements[$label]->get_value(); } @@ -539,10 +629,52 @@ * @param string - the form name */ function set_form_width($width) { - $this->_form_width = $width; + $this->_width = $width; } + /** + * This method returns the width + * of the form errors table. + * By default it is supposed to be + * the same width as the form itself. + * + * @return int + */ + function get_form_errors_width() { + if ($this->_form_errors_width == null) { + return $this->_width; + } else { + return $this->_form_errors_width; + } + } + + /** + * This method allows you to override + * the width of the form errors table. + * By default it uses the width of the + * form as its size. + * + * @param string - the width + */ + function set_form_errors_width($width) { + $this->_form_errors_width = $width; + } + + + /** + * This allows us to change the form errors + * table title + * + * @param string - the new title + */ + function set_form_errors_title($title) { + $this->_form_errors_title = $title; + } + + + + /** * This function is used to set the * default CSS class used on @@ -744,36 +876,32 @@ * @param string - the action * @param boolean - disable opon submit? * - * @return + * @return Atag object */ function add_image_action( $image_name, $action, $disable_on_submit=TRUE ) { - $container = container(); - - $container = new ContainerWidget; - // Unique button label + // Unique button label if (!$this->_action_counter) { $this->_action_counter = 1; } - $blabel = FORM_ACTION.$this->_action_counter; + $blabel = "_form_action".$this->_action_counter; $this->_action_counter++; - $img = form_image($blabel, $action, $image_name); - //NS 4 hack - $img->set_tag_attribute("border", 0 ); - $img->set_tag_attribute("style", "cursor:hand;"); - //build the action - $onclick = ""; + $onclick = "document.".$this->_form_name."._form_action.value='".$action."';"; + $onclick .= "document.".$this->_form_name.".submit();"; + if ($disable_on_submit) { - $onclick .= "this.form.".$blabel.".disabled=true;"; + $click = "javascript:if (!window.global_cntr) {window.global_cntr = 1;} else {window.global_cntr++;} "; + $click .= " if(window.global_cntr<2) {"; + $click .= $onclick."}"; } - $onclick .= "this.form.".FORM_ACTION.".value='".$action."';"; - $onclick .= "this.form.submit();"; - - $img->set_tag_attribute("onClick", $onclick); - //IE HACK - $container->push($img); + + $img = html_img($image_name); + $img->set_tag_attribute("border", 0 ); + $img->set_tag_attribute("style", "cursor:hand;"); - return $container; + $link = html_a("#", $img); + $link->set_tag_attribute("onclick", $click); + return $link; } @@ -813,6 +941,47 @@ return " "; } } + + /** + * 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 sets the required text + * + * @param string + */ + function set_required_text($text) { + $this->_required_field_text = $text; + + } + + + /** + * This method tests to see if we + * have an element named $label, + * so we can operate on it. + * + * @param string - the element's label + * @param string - the name of the method that called us + * @param boolean - test a hidden element? + */ + function _test_element( $label, $method, $hidden=FALSE ) { + if ($hidden) { + if (!isset($this->_hidden_elements[$label])) { + trigger_error("FormContent::".$method."() - '".$label."' element doesn't exist", E_USER_ERROR); + } + } else { + if (!isset($this->_elements[$label])) { + trigger_error("FormContent::".$method."() - '".$label."' element doesn't exist", E_USER_ERROR); + } + } + } }