4 |
* |
* |
5 |
* $Id$ |
* $Id$ |
6 |
* |
* |
7 |
* @author Walter A. Boring IV <waboring@buildabetterweb.com> |
* @author Walter A. Boring IV <waboring@newsblob.com> |
8 |
* @author Suren Markossian <suren@cbestwhat?> |
* @author Suren Markossian <suren@cbestwhat?> |
9 |
* @package phpHtmlLib |
* @package phpHtmlLib |
10 |
* @subpackage FormProcessing |
* @subpackage FormProcessing |
21 |
* This is the main engine for the processing |
* This is the main engine for the processing |
22 |
* of Forms. It builds the form tag, and calls |
* of Forms. It builds the form tag, and calls |
23 |
* the appropriate FormContent methods to build |
* the appropriate FormContent methods to build |
24 |
* the FormElement's and validation, as well as |
* the FormElement's and validation, as well as |
25 |
* backend processing to do the action after the |
* backend processing to do the action after the |
26 |
* data has been validated. |
* data has been validated. |
27 |
* |
* |
85 |
|
|
86 |
/** |
/** |
87 |
* The form was processed and passed |
* The form was processed and passed |
88 |
* the confirmation if any, and |
* the confirmation if any, and |
89 |
* it was successfull ? |
* it was successfull ? |
90 |
*/ |
*/ |
91 |
var $_confirmed_successfull = FALSE; |
var $_confirmed_successfull = FALSE; |
133 |
* form errors object that is to be |
* form errors object that is to be |
134 |
* used by this form. |
* used by this form. |
135 |
* |
* |
136 |
* You can override this method to |
* You can override this method to |
137 |
* use a different FormErrors object |
* use a different FormErrors object |
138 |
* for localization. |
* for localization. |
139 |
*/ |
*/ |
140 |
function setup_validation() { |
function setup_validation() { |
141 |
$this->_FormValidation = new FormValidation( new FormErrors ); |
$this->_FormValidation =& FormValidation::singleton(); |
142 |
} |
$this->_form_content->_set_validation_object($this->_FormValidation); |
143 |
|
} |
144 |
|
|
145 |
/** |
/** |
146 |
* This method does the logic of |
* This method does the logic of |
152 |
//we only need to process the form |
//we only need to process the form |
153 |
//if it has been visited. Otherwise |
//if it has been visited. Otherwise |
154 |
//it just gets rendered. |
//it just gets rendered. |
155 |
if (!empty($_REQUEST[FORM_VISITED]) && $_REQUEST[FORM_VISITED] == 1) { |
if (!empty($_REQUEST[$this->_form_attributes['name'].FORM_VISITED]) && |
156 |
|
$_REQUEST[$this->_form_attributes['name'].FORM_VISITED] == 1) { |
157 |
$this->_set_action(); |
$this->_set_action(); |
158 |
|
|
159 |
//let see if this was a confirmation page. |
//let see if this was a confirmation page. |
169 |
} |
} |
170 |
} |
} |
171 |
|
|
172 |
//now do the validation |
if ($this->_form_content->has_confirm()) { |
173 |
if (!$this->_confirmed) { |
//the form content has a confirmation |
174 |
//we haven't been confirmed, so we |
//we need to process |
175 |
//need to validate the form. |
$this->_has_errors = !$this->_pre_confirm(); |
176 |
if ($this->can_validate()) { |
} |
177 |
//looks like we should do validation |
|
178 |
$this->do_validation(); |
//we haven't been confirmed, so we |
179 |
} |
//need to validate the form. |
180 |
if (!$this->_has_errors) { |
if ($this->can_validate()) { |
181 |
//no errors were found |
//looks like we should do validation |
182 |
if ($this->_form_content->has_confirm()) { |
$this->do_validation(); |
183 |
//the form content has a confirmation |
} |
184 |
//we need to process |
if (!$this->_has_errors) { |
185 |
$this->_has_errors = !$this->_pre_confirm(); |
//no errors were found |
|
} else { |
|
|
//make sure we don't have any backend errors |
|
|
$this->_has_errors = !$this->_form_content->form_backend_validation(); |
|
|
if (!$this->_has_errors) { |
|
|
$this->_has_errors = !$this->_process_action(); |
|
|
if (!$this->_has_errors) { |
|
|
$this->_set_confirmed_success(TRUE); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} else { |
|
186 |
//make sure we don't have any backend errors |
//make sure we don't have any backend errors |
187 |
$this->_has_errors = !$this->_form_content->form_backend_validation(); |
$this->_has_errors = !$this->_form_content->form_backend_validation(); |
188 |
if (!$this->_has_errors) { |
if (!$this->_has_errors && (($this->_form_content->has_confirm() |
189 |
|
&& $this->_confirmed) || !$this->_form_content->has_confirm())) { |
190 |
|
// process action only at the final stage |
191 |
$this->_has_errors = !$this->_process_action(); |
$this->_has_errors = !$this->_process_action(); |
192 |
|
|
193 |
if (!$this->_has_errors) { |
if (!$this->_has_errors) { |
194 |
$this->_set_confirmed_success(TRUE); |
$this->_set_confirmed_success(TRUE); |
195 |
} |
} |
196 |
} |
} |
197 |
} |
} |
198 |
} |
} |
199 |
} |
} |
212 |
} |
} |
213 |
|
|
214 |
/** |
/** |
215 |
* This method calls the FormContent |
* This method calls the FormContent |
216 |
* to let it do any data munging before the |
* to let it do any data munging before the |
217 |
* confirmation page is rendered |
* confirmation page is rendered |
218 |
*/ |
*/ |
221 |
if ($this->_form_content->_has_file_element) { |
if ($this->_form_content->_has_file_element) { |
222 |
//we need to allow any/all of the file elements |
//we need to allow any/all of the file elements |
223 |
//save the temp files during a confirmation. |
//save the temp files during a confirmation. |
224 |
//if we don't, then the web server may delete |
//if we don't, then the web server may delete |
225 |
//them before confirmation has been accepted. |
//them before confirmation has been accepted. |
226 |
$this->_form_content->_pre_confirm(); |
$this->_form_content->_pre_confirm(); |
227 |
} |
} |
239 |
function do_validation() { |
function do_validation() { |
240 |
$keys = array_keys( $this->_form_content->_elements ); |
$keys = array_keys( $this->_form_content->_elements ); |
241 |
foreach( $keys as $key ) { |
foreach( $keys as $key ) { |
242 |
$valid = $this->_form_content->_elements[$key]->_do_validation($this->_FormValidation); |
if (!$this->_form_content->_elements[$key]->is_disabled()) { |
243 |
if (!$valid) { |
$valid = $this->_form_content->_elements[$key]->_do_validation($this->_FormValidation); |
244 |
$this->_has_errors = TRUE; |
if (!$valid) { |
245 |
|
$this->_has_errors = TRUE; |
246 |
|
} |
247 |
|
} else { |
248 |
|
//detect disabled field hack attempts |
249 |
|
if ($this->_form_content->_elements[$key]->has_error()) { |
250 |
|
$this->_has_errors = TRUE; |
251 |
|
} |
252 |
} |
} |
253 |
} |
} |
254 |
} |
} |
265 |
return $this->render_error($indent_level, $output_debug); |
return $this->render_error($indent_level, $output_debug); |
266 |
} else { |
} else { |
267 |
//there are no errors! |
//there are no errors! |
268 |
if (@$_REQUEST[FORM_VISITED] == 1) { |
if (@$_REQUEST[$this->_form_attributes['name'] . FORM_VISITED] == 1) { |
269 |
//looks like the form has been processed? |
//looks like the form has been processed? |
270 |
if ($this->_form_content->has_confirm() && !$this->_confirmed) { |
if ($this->_form_content->has_confirm() && !$this->_confirmed) { |
271 |
return $this->render_confirm($indent_level, $output_debug); |
return $this->render_confirm($indent_level, $output_debug); |
272 |
} else { |
} else { |
273 |
//Looks like the action worked |
//Looks like the action worked |
274 |
$success = $this->_form_content->form_success(); |
$success = $this->_form_content->form_success(); |
275 |
|
|
276 |
if ($this->_form_success_render) { |
if ($this->_form_success_render) { |
277 |
return $this->render_form($indent_level, $output_debug, |
return $this->render_form($indent_level, $output_debug, |
278 |
$success); |
$success); |
338 |
} else { |
} else { |
339 |
return $this->_form->render($indent_level, $output_debug); |
return $this->_form->render($indent_level, $output_debug); |
340 |
} |
} |
341 |
|
|
342 |
} |
} |
343 |
|
|
344 |
/** |
/** |
384 |
* @return raw html |
* @return raw html |
385 |
*/ |
*/ |
386 |
function render_error( $indent_level, $output_debug) { |
function render_error( $indent_level, $output_debug) { |
387 |
|
|
388 |
if ($this->_auto_show_errors) { |
if ($this->_auto_show_errors) { |
389 |
//Ok first lets build the error table |
//Ok first lets build the error table |
390 |
$wrapper = new DIVtag; |
$wrapper = new DIVtag; |
393 |
} else { |
} else { |
394 |
$wrapper = NULL; |
$wrapper = NULL; |
395 |
} |
} |
396 |
|
|
397 |
|
|
398 |
return $this->render_form( $indent_level, $output_debug, $wrapper); |
return $this->render_form( $indent_level, $output_debug, $wrapper); |
399 |
} |
} |
430 |
|
|
431 |
/** |
/** |
432 |
* This is used to test to see if the form action |
* This is used to test to see if the form action |
433 |
* was processed succesfully. |
* was processed succesfully. |
434 |
* This is usefull for external entities to determine |
* This is usefull for external entities to determine |
435 |
* if the form was processed, and it was successfull. |
* if the form was processed, and it was successfull. |
436 |
* |
* |
455 |
* This sets the flag that tells this class |
* This sets the flag that tells this class |
456 |
* to automatically call the form contents |
* to automatically call the form contents |
457 |
* form errors and display it or not |
* form errors and display it or not |
458 |
* |
* |
459 |
* @param boolean - show errors? |
* @param boolean - show errors? |
460 |
*/ |
*/ |
461 |
function set_auto_error_display($flag=TRUE) { |
function set_auto_error_display($flag=TRUE) { |
495 |
/** |
/** |
496 |
* This method returns an array of errors that |
* This method returns an array of errors that |
497 |
* happened in the form. |
* happened in the form. |
498 |
* |
* |
499 |
* @return array |
* @return array |
500 |
*/ |
*/ |
501 |
function get_error_array() { |
function get_error_array() { |
623 |
|
|
624 |
|
|
625 |
/** |
/** |
626 |
* This is used to set the action |
* This is used to set the action |
627 |
* submitted by the user |
* submitted by the user |
628 |
* |
* |
629 |
*/ |
*/ |
648 |
* NOTE: The FormContent child can automatically |
* NOTE: The FormContent child can automatically |
649 |
* set this value depending on the FormElement |
* set this value depending on the FormElement |
650 |
* children it contains. |
* children it contains. |
651 |
* |
* |
652 |
* @param string |
* @param string |
653 |
* @return none |
* @return none |
654 |
*/ |
*/ |
659 |
/** |
/** |
660 |
* Gets the current value of the form tag's |
* Gets the current value of the form tag's |
661 |
* onsubmit value |
* onsubmit value |
662 |
* |
* |
663 |
* @return string |
* @return string |
664 |
*/ |
*/ |
665 |
function get_onsubmit() { |
function get_onsubmit() { |
666 |
return $this->_form_attributes["onsubmit"]; |
return $this->_form_attributes["onsubmit"]; |
667 |
} |
} |
668 |
|
|
669 |
|
/** |
670 |
|
* Set a random attribute on the form tag. |
671 |
|
* You should know what you are doing as this |
672 |
|
* might invalidate the output html with the |
673 |
|
* W3C validator. |
674 |
|
* |
675 |
|
* @param string the key |
676 |
|
* @param string the value |
677 |
|
*/ |
678 |
|
function set_form_attribute($key, $value) { |
679 |
|
$this->_form_attributes[$key] = $value; |
680 |
|
} |
681 |
|
|
682 |
|
|
683 |
//************************************// |
//************************************// |
684 |
//* Some Private methods *// |
//* Some Private methods *// |
687 |
/** |
/** |
688 |
* This method initializes the FormContent |
* This method initializes the FormContent |
689 |
* during processing. |
* during processing. |
690 |
* |
* |
691 |
* @return none |
* @return none |
692 |
*/ |
*/ |
693 |
function _init_form_content() { |
function _init_form_content() { |
696 |
$this->_form_content->form_init_elements(); |
$this->_form_content->form_init_elements(); |
697 |
|
|
698 |
//first we need to |
//first we need to |
699 |
if (!@$_REQUEST[FORM_VISITED]) { |
if (!@$_REQUEST[$this->_form_attributes['name'] . FORM_VISITED]) { |
700 |
$this->_form_content->form_init_data(); |
$this->_form_content->form_init_data(); |
701 |
} |
} |
702 |
|
|
705 |
//add the enctype to the form tag attribute |
//add the enctype to the form tag attribute |
706 |
if ($this->_form_content->_has_file_element) { |
if ($this->_form_content->_has_file_element) { |
707 |
$this->set_form_enctype("multipart/form-data"); |
$this->set_form_enctype("multipart/form-data"); |
708 |
} |
} |
709 |
} |
} |
710 |
|
|
711 |
|
|
719 |
//see if we need to add the onsubmit attribute to the form |
//see if we need to add the onsubmit attribute to the form |
720 |
//this only needs to happen on the non-confirmation |
//this only needs to happen on the non-confirmation |
721 |
//portion of the forms. |
//portion of the forms. |
722 |
if (!isset($_REQUEST[FORM_VISITED])) { |
if (!isset($_REQUEST[$this->_form_attributes['name'] . FORM_VISITED])) { |
723 |
if (strlen($this->_form_content->_form_on_submit) > 0) { |
if (strlen($this->_form_content->_form_on_submit) > 0) { |
724 |
$set = TRUE; |
$set = TRUE; |
725 |
$this->set_onsubmit( $this->get_onsubmit().$this->_form_content->_form_on_submit ); |
$this->set_onsubmit( $this->get_onsubmit().$this->_form_content->_form_on_submit.$this->_form_content->_form_action_elements_on_submit ); |
726 |
} |
} |
727 |
} else { |
} else { |
728 |
//re-rendering the form and it has errors. |
//re-rendering the form and it has errors. |
729 |
//we need the onsubmit if they have it. |
//we need the onsubmit if they have it. |
730 |
if ($_REQUEST[FORM_VISITED] && $this->_has_errors) { |
if (isset($_REQUEST[$this->_form_attributes['name'] . FORM_VISITED]) && $this->_has_errors) { |
731 |
if (strlen($this->_form_content->_form_on_submit) > 0) { |
if (strlen($this->_form_content->_form_on_submit) > 0) { |
732 |
$set = TRUE; |
$set = TRUE; |
733 |
$this->set_onsubmit( $this->get_onsubmit().$this->_form_content->_form_on_submit ); |
$this->set_onsubmit($this->get_onsubmit().$this->_form_content->_form_on_submit.$this->_form_content->_form_action_elements_on_submit ); |
734 |
} |
} |
735 |
} |
} else if (isset($_REQUEST[FORM_CONFIRM]) && $_REQUEST[FORM_CONFIRM] == 1) { |
736 |
|
//form has been confirmed lets add it |
737 |
//form has been confirmed lets add it |
//in case we are showing the form again |
|
//in case we are showing the form again |
|
|
if (isset($_REQUEST[FORM_CONFIRM]) && $_REQUEST[FORM_CONFIRM] == 1) { |
|
738 |
if (strlen($this->_form_content->_form_on_submit) > 0) { |
if (strlen($this->_form_content->_form_on_submit) > 0) { |
739 |
$set = TRUE; |
$set = TRUE; |
740 |
$this->set_onsubmit( $this->get_onsubmit().$this->_form_content->_form_on_submit ); |
$this->set_onsubmit( $this->get_onsubmit().$this->_form_content->_form_on_submit.$this->_form_content->_form_action_elements_on_submit ); |
741 |
} |
} |
742 |
|
} else { |
743 |
|
$this->set_onsubmit($this->_form_content->_form_action_elements_on_submit); |
744 |
} |
} |
745 |
} |
} |
746 |
|
|
749 |
if ($value) { |
if ($value) { |
750 |
$form_attrs[$name] = $value; |
$form_attrs[$name] = $value; |
751 |
} |
} |
752 |
} |
} |
753 |
$this->_form = new FORMtag( $form_attrs ); |
$this->_form = new FORMtag( $form_attrs ); |
754 |
} |
} |
755 |
|
|
761 |
function _add_confirm_data() { |
function _add_confirm_data() { |
762 |
$keys = array_keys( $this->_form_content->_elements ); |
$keys = array_keys( $this->_form_content->_elements ); |
763 |
foreach( $keys as $key ) { |
foreach( $keys as $key ) { |
764 |
$this->_form->add($this->_form_content->_elements[$key]->get_confirm_element()); |
//make sure the element isn't disabled. |
765 |
|
if (!$this->_form_content->_elements[$key]->is_disabled()) { |
766 |
|
$this->_form->add($this->_form_content->_elements[$key]->get_confirm_element()); |
767 |
|
} |
768 |
} |
} |
769 |
|
|
770 |
$keys = array_keys( $this->_form_content->_hidden_elements ); |
$keys = array_keys( $this->_form_content->_hidden_elements ); |
795 |
*/ |
*/ |
796 |
function __hidden_fields() { |
function __hidden_fields() { |
797 |
$this->_form->add( form_hidden(FORM_ACTION), |
$this->_form->add( form_hidden(FORM_ACTION), |
798 |
form_hidden(FORM_VISITED,1) ); |
form_hidden($this->_form_attributes['name'] . FORM_VISITED,1) ); |
799 |
|
|
800 |
if ($this->_form_content->has_confirm() && !$this->_confirmed) { |
if ($this->_form_content->has_confirm() && !$this->_confirmed) { |
801 |
if (@!$_REQUEST[FORM_VISITED] || $this->_has_errors) { |
if (@!$_REQUEST[$this->_form_attributes['name'] . FORM_VISITED] || $this->_has_errors) { |
802 |
$this->_form->add( form_hidden(FORM_CONFIRM, 0 ) ); |
$this->_form->add( form_hidden(FORM_CONFIRM, 0 ) ); |
803 |
} else { |
} else { |
804 |
$this->_form->add( form_hidden(FORM_CONFIRM, 1 ) ); |
$this->_form->add( form_hidden(FORM_CONFIRM, 1 ) ); |