23 |
* get_value() - This gets the current |
* get_value() - This gets the current |
24 |
* 'value' of the FormElement. |
* 'value' of the FormElement. |
25 |
* NOTE: This can be a 'complex' FormElement |
* NOTE: This can be a 'complex' FormElement |
26 |
* in which it may return an array of |
* in which it may return an array of |
27 |
* values. |
* values. |
28 |
* |
* |
29 |
* set_value() - Set the current value |
* set_value() - Set the current value |
31 |
* |
* |
32 |
* get_label() - returns the label for this |
* get_label() - returns the label for this |
33 |
* FormElement. |
* FormElement. |
34 |
* |
* |
35 |
* |
* |
36 |
* @package phpHtmlLib |
* @package phpHtmlLib |
37 |
* @subpackage FormProcessing |
* @subpackage FormProcessing |
58 |
var $_is_required = TRUE; |
var $_is_required = TRUE; |
59 |
|
|
60 |
/** |
/** |
61 |
|
* holds the array of errors |
62 |
|
* for this element. |
63 |
|
*/ |
64 |
|
var $_errors = array(); |
65 |
|
|
66 |
|
/** |
67 |
* Holds the error message text |
* Holds the error message text |
68 |
* for validation errors, if any |
* for validation errors, if any |
69 |
* |
* |
104 |
*/ |
*/ |
105 |
var $_is_disabled = FALSE; |
var $_is_disabled = FALSE; |
106 |
|
|
107 |
|
/** |
108 |
|
* automatically strip slashes from |
109 |
|
* form values? |
110 |
|
*/ |
111 |
|
var $_stripslashes = FALSE; |
112 |
|
|
113 |
|
|
114 |
|
/** |
115 |
|
* This holds the name of the form |
116 |
|
* for js that needs it |
117 |
|
*/ |
118 |
|
var $_form_name; |
119 |
|
|
120 |
|
/** |
121 |
|
* This Form Element needs to |
122 |
|
* propogate some js to the |
123 |
|
* Form tag's onsubmit attribute |
124 |
|
* |
125 |
|
* @var string |
126 |
|
*/ |
127 |
|
var $_has_form_on_submit = false; |
128 |
|
|
129 |
|
/** |
130 |
|
* This holds an array of FormElement objects |
131 |
|
* that are slaves of this element |
132 |
|
*/ |
133 |
|
var $_slave_elements; |
134 |
|
|
135 |
|
/** |
136 |
|
* The required field string |
137 |
|
* |
138 |
|
*/ |
139 |
|
var $_required_field_marker = "*"; |
140 |
|
|
141 |
|
|
142 |
/** |
/** |
143 |
* The constructor |
* The constructor |
157 |
$this->set_value( $this->get_init_value() ); |
$this->set_value( $this->get_init_value() ); |
158 |
} |
} |
159 |
|
|
160 |
|
/** |
161 |
|
* This function will return the |
162 |
|
* slaves of this element |
163 |
|
* |
164 |
|
* @return array of slave elements |
165 |
|
*/ |
166 |
|
function &get_slave_elements() { |
167 |
|
return $this->_slave_elements; |
168 |
|
} |
169 |
|
|
170 |
|
/** |
171 |
|
* This function will set the |
172 |
|
* slaves of this element |
173 |
|
* |
174 |
|
* @param array of slave elements (element references &) |
175 |
|
*/ |
176 |
|
function set_slave_elements($slaveElements) { |
177 |
|
$this->_slave_elements = $slaveElements; |
178 |
|
$this->set_data_all_slaves( $this->get_value() ); |
179 |
|
} |
180 |
|
|
181 |
|
|
182 |
|
/** |
183 |
|
* This function will set the |
184 |
|
* data the parent wants to set for the slave |
185 |
|
* |
186 |
|
* @param the slave |
187 |
|
* @param the value being set for the parent |
188 |
|
*/ |
189 |
|
function set_slave_data(&$slave, $parentValue) { |
190 |
|
; //override to set the data for the slave |
191 |
|
} |
192 |
|
|
193 |
/** |
/** |
194 |
* This function will return the |
* This function will return the |
217 |
* @return mixed |
* @return mixed |
218 |
*/ |
*/ |
219 |
function get_value() { |
function get_value() { |
220 |
return $this->_value; |
if ($this->_stripslashes) { |
221 |
|
return stripslashes($this->_value); |
222 |
|
} else { |
223 |
|
return $this->_value; |
224 |
|
} |
225 |
} |
} |
226 |
|
|
227 |
|
|
228 |
/** |
/** |
229 |
* This function will set the |
* This function will set the |
230 |
* initial value for the element |
* initial value for the element |
233 |
*/ |
*/ |
234 |
function set_value($value) { |
function set_value($value) { |
235 |
$this->_value = $value; |
$this->_value = $value; |
236 |
|
$this->set_data_all_slaves($value); |
237 |
|
} |
238 |
|
|
239 |
|
/** |
240 |
|
* This function will call set_slave_data for all slaves |
241 |
|
* should be called when value of parent changes |
242 |
|
* |
243 |
|
* @param string - the current value of parent |
244 |
|
*/ |
245 |
|
function set_data_all_slaves($parentValue){ |
246 |
|
$slaves = &$this->get_slave_elements(); |
247 |
|
$num_slaves = count($slaves); |
248 |
|
if ($num_slaves) { |
249 |
|
for($i=0; $i < $num_slaves; $i++){ |
250 |
|
$this->set_slave_data($slaves[$i], $parentValue); |
251 |
|
} |
252 |
|
|
253 |
|
} |
254 |
|
} |
255 |
|
|
256 |
|
/** |
257 |
|
* in case anyone in JS land |
258 |
|
* needs the name of the form that |
259 |
|
* this element lives in |
260 |
|
* |
261 |
|
* @param string - the form name |
262 |
|
*/ |
263 |
|
function set_form_name($name) { |
264 |
|
$this->_form_name = $name; |
265 |
} |
} |
266 |
|
|
267 |
/** |
/** |
268 |
* This provides a method |
* This provides a method |
269 |
* for the FormContent |
* for the FormContent |
270 |
* to get access to the |
* to get access to the |
271 |
* text associated with a |
* text associated with a |
272 |
* field. This is only available |
* field. This is only available |
273 |
* on FormElements that have text |
* on FormElements that have text |
274 |
* associated with a field. |
* associated with a field. |
275 |
* It is used during Confirmation |
* It is used during Confirmation |
276 |
* |
* |
277 |
* @param mixed - the value to look up |
* @param mixed the value to look up |
278 |
* @return string - the text associated |
* @return string - the text associated |
279 |
*/ |
*/ |
280 |
function get_value_text($value) { |
function get_value_text() { |
281 |
return $this->get_value(); |
return $this->get_value(); |
282 |
} |
} |
283 |
|
|
310 |
* @return mixed |
* @return mixed |
311 |
*/ |
*/ |
312 |
function get_init_value() { |
function get_init_value() { |
313 |
return $_REQUEST[str_replace("[]","",$this->get_element_name())]; |
return @$_REQUEST[str_replace("[]","",$this->get_element_name())]; |
314 |
} |
} |
315 |
|
|
316 |
|
|
333 |
return $this->_is_disabled; |
return $this->_is_disabled; |
334 |
} |
} |
335 |
|
|
336 |
|
/** |
337 |
|
* This sets the stripslashes flag for |
338 |
|
* this object. |
339 |
|
* |
340 |
|
* @param boolean |
341 |
|
*/ |
342 |
|
function set_stripslashes( $flag = TRUE ) { |
343 |
|
$this->_stripslashes = $flag; |
344 |
|
} |
345 |
|
|
346 |
|
|
347 |
/********* Tag attributes methods *********/ |
/********* Tag attributes methods *********/ |
348 |
|
|
393 |
$this->_style_attributes[$name] = $value; |
$this->_style_attributes[$name] = $value; |
394 |
} |
} |
395 |
|
|
396 |
|
/** |
397 |
|
* Sets the element's tab index |
398 |
|
* |
399 |
|
* @param int the # for the tabindex |
400 |
|
*/ |
401 |
|
function set_tabindex($index) { |
402 |
|
$this->set_attribute('tabindex', $index); |
403 |
|
} |
404 |
|
|
405 |
|
/** |
406 |
|
* Gets the current tab index if any |
407 |
|
* |
408 |
|
* @return int returns 0 if there is none set. |
409 |
|
*/ |
410 |
|
function get_tabindex() { |
411 |
|
if (isset($this->_attributes['tabindex'])) { |
412 |
|
return $this->_attributes['tabindex']; |
413 |
|
} else { |
414 |
|
return 0; |
415 |
|
} |
416 |
|
} |
417 |
|
|
418 |
|
|
419 |
/********* Error handling methods *********/ |
/********* Error handling methods *********/ |
420 |
|
|
423 |
* sets the error flag to true |
* sets the error flag to true |
424 |
* |
* |
425 |
* @param mesage text - error message |
* @param mesage text - error message |
426 |
*/ |
* @param label text - a label to provide |
427 |
function set_error_message($message) { |
* for the error. This is |
428 |
$this->_error_message = $message; |
* only needed for a complex |
429 |
|
* element that has multiple |
430 |
|
* 'hidden/magic' fields. |
431 |
|
*/ |
432 |
|
function set_error_message($message, $label=NULL) { |
433 |
|
if ($label == NULL) { |
434 |
|
$label = $this->get_label_text(); |
435 |
|
} |
436 |
|
$this->_errors[] = array("label" => $label, |
437 |
|
"message" => $message); |
438 |
$this->_has_error = TRUE; |
$this->_has_error = TRUE; |
439 |
} |
} |
440 |
|
|
441 |
|
/** |
442 |
|
* This returns the array of errors |
443 |
|
* for this element |
444 |
|
* |
445 |
|
* @param array of errors |
446 |
|
*/ |
447 |
|
function get_errors() { |
448 |
|
return $this->_errors; |
449 |
|
} |
450 |
|
|
451 |
|
|
452 |
/** |
/** |
453 |
* Returns the current error message |
* Returns the current error message |
465 |
* |
* |
466 |
* @return bool error state |
* @return bool error state |
467 |
*/ |
*/ |
468 |
function has_error() { |
function has_error($label=NULL) { |
469 |
return $this->_has_error; |
if ($label==NULL) { |
470 |
|
return $this->_has_error; |
471 |
|
} else { |
472 |
|
//they are looking for a specific error |
473 |
|
foreach( $this->_errors as $error) { |
474 |
|
|
475 |
|
if ($error['label'] == $label) { |
476 |
|
return TRUE; |
477 |
|
} |
478 |
|
} |
479 |
|
return FALSE; |
480 |
|
} |
481 |
} |
} |
482 |
|
|
483 |
|
|
499 |
$name[$i] = "_"; |
$name[$i] = "_"; |
500 |
} |
} |
501 |
|
|
502 |
|
$this->set_element_name($name); |
503 |
|
} |
504 |
|
|
505 |
|
/** |
506 |
|
* This allows you to force the element |
507 |
|
* name to whatever you like, so you |
508 |
|
* don't have to use the default name, which is |
509 |
|
* generated based on the label. |
510 |
|
* |
511 |
|
* @param string the form element's name |
512 |
|
* @return none |
513 |
|
*/ |
514 |
|
function set_element_name($name) { |
515 |
$this->_element_name = $name; |
$this->_element_name = $name; |
516 |
} |
} |
517 |
|
|
538 |
* validation and setting the appropriate error message |
* validation and setting the appropriate error message |
539 |
* in case of a failed validation |
* in case of a failed validation |
540 |
* |
* |
541 |
|
* @param FormValidation object. |
542 |
*/ |
*/ |
543 |
function validate() { |
function validate(&$_FormValidation) { |
544 |
return TRUE; |
return TRUE; |
545 |
} |
} |
546 |
|
|
549 |
* This function checks if the validation |
* This function checks if the validation |
550 |
* is nesseccary and calls the validate method |
* is nesseccary and calls the validate method |
551 |
* |
* |
552 |
|
* @param FormValidation object. |
553 |
*/ |
*/ |
554 |
function _do_validation() { |
function _do_validation(&$_FormValidation) { |
555 |
if ($this->get_value() != NULL) { |
$value = $this->get_value(); |
556 |
return $this->validate(); |
$has_value = FALSE; |
557 |
|
if (is_array($value)) { |
558 |
|
foreach($value as $entry) { |
559 |
|
if ($entry != NULL) { |
560 |
|
$has_value = TRUE; |
561 |
|
} |
562 |
|
} |
563 |
|
} else { |
564 |
|
if ($value != NULL) { |
565 |
|
$has_value = TRUE; |
566 |
|
} |
567 |
|
} |
568 |
|
|
569 |
|
if ($has_value && !$this->is_disabled()) { |
570 |
|
return $this->validate($_FormValidation); |
571 |
} else if ($this->is_required()) { |
} else if ($this->is_required()) { |
572 |
$this->set_error_message("This field cannot be empty"); |
$this->set_error_message("This field cannot be empty"); |
573 |
return FALSE; |
return FALSE; |
579 |
|
|
580 |
/********* JavaScript event methods *********/ |
/********* JavaScript event methods *********/ |
581 |
|
|
582 |
|
|
583 |
|
/** |
584 |
|
* This method is used for adding any javascript |
585 |
|
* that is used by this element. This will automatically |
586 |
|
* get called and added to the page by the FormProcessor |
587 |
|
* |
588 |
|
* @return string - raw js |
589 |
|
*/ |
590 |
|
function javascript() { |
591 |
|
return NULL; |
592 |
|
} |
593 |
|
|
594 |
/** |
/** |
595 |
* This function return the javaScript code for |
* This function return the javaScript code for |
596 |
* an onClick event |
* an onClick event |
653 |
$javascript = array(); |
$javascript = array(); |
654 |
|
|
655 |
if (($js=$this->onClick()) != NULL) { |
if (($js=$this->onClick()) != NULL) { |
656 |
$javascript[] = "onClick=\"" . $js . "\""; |
$javascript[] = "onclick=\"" . $js . "\""; |
657 |
} |
} |
658 |
|
|
659 |
if (($js=$this->onFocus()) != NULL) { |
if (($js=$this->onFocus()) != NULL) { |
660 |
$javascript[] = "onFocus=\"" . $js . "\""; |
$javascript[] = "onfocus=\"" . $js . "\""; |
661 |
} |
} |
662 |
|
|
663 |
if (($js=$this->onSubmit()) != NULL) { |
if (($js=$this->onSubmit()) != NULL) { |
664 |
$javascript[] = "onSubmit=\"" . $js . "\""; |
$javascript[] = "onsubmit=\"" . $js . "\""; |
665 |
} |
} |
666 |
|
|
667 |
if (($js=$this->onBlur()) != NULL) { |
if (($js=$this->onBlur()) != NULL) { |
668 |
$javascript[] = "onBlur=\"" . $js . "\""; |
$javascript[] = "onblur=\"" . $js . "\""; |
669 |
} |
} |
670 |
|
|
671 |
if (($js=$this->onChange()) != NULL) { |
if (($js=$this->onChange()) != NULL) { |
672 |
$javascript[] = "onChange=\"" . $js . "\""; |
$javascript[] = "onchange=\"" . $js . "\""; |
673 |
} |
} |
674 |
|
|
675 |
if (count($javascript)>0) { |
if (count($javascript)>0) { |
681 |
} |
} |
682 |
|
|
683 |
|
|
684 |
|
/** |
685 |
|
* This is a method for getting the JS needed |
686 |
|
* for the form tag's onsubmit attribute. |
687 |
|
* |
688 |
|
* @return string |
689 |
|
*/ |
690 |
|
function form_tag_onsubmit() { |
691 |
|
return ''; |
692 |
|
} |
693 |
|
|
694 |
|
|
695 |
/********* rendering methods *********/ |
/********* rendering methods *********/ |
696 |
|
|
697 |
|
|
702 |
* @return string - required symbol |
* @return string - required symbol |
703 |
*/ |
*/ |
704 |
function get_required_symbol() { |
function get_required_symbol() { |
705 |
return '*'; |
return $this->_required_field_marker; |
706 |
|
} |
707 |
|
|
708 |
|
/** |
709 |
|
* This allows you to customize the |
710 |
|
* require string marker |
711 |
|
* |
712 |
|
* @param string |
713 |
|
*/ |
714 |
|
function set_required_symbol($symbol) { |
715 |
|
$this->_required_field_marker; |
716 |
} |
} |
717 |
|
|
718 |
/** |
/** |
754 |
* label object based on the label text |
* label object based on the label text |
755 |
* and error conditions |
* and error conditions |
756 |
* |
* |
757 |
|
* @param FormContent object that holds the |
758 |
|
* required field marker |
759 |
* @return object SPANtag |
* @return object SPANtag |
760 |
*/ |
*/ |
761 |
function get_label() { |
function get_label($form_content=NULL) { |
762 |
|
|
763 |
$label = $this->get_label_text(); |
$label = $this->get_label_text(); |
764 |
|
$text = $label; |
765 |
|
|
766 |
if ($this->is_required()) $label .= ' ' . $this->get_required_symbol(); |
if ($this->is_required()) { |
767 |
|
$text .= ' ' . ($form_content ? $form_content->_required_field_marker : |
768 |
|
$this->get_required_symbol()); |
769 |
|
} |
770 |
|
|
771 |
$span = html_span("formlabel", $label); |
$span = html_span("formlabel", $text); |
772 |
|
|
773 |
if ($this->has_error()) { |
if ($this->has_error()) { |
774 |
$span->set_tag_attribute("style","color:red;"); |
$span->set_tag_attribute("style","color:red;"); |
791 |
return $span; |
return $span; |
792 |
} |
} |
793 |
|
|
794 |
|
|
795 |
|
/** |
796 |
|
* This method returns the hidden version of this |
797 |
|
* element for a confirmation page. |
798 |
|
* |
799 |
|
* NOTE: This is called by the FormProcessor only. |
800 |
|
* It shouldn't be called manually. |
801 |
|
* |
802 |
|
* @return INPUTtag of type hidden |
803 |
|
*/ |
804 |
|
function get_confirm_element() { |
805 |
|
$values = $this->get_value(); |
806 |
|
$name = $this->get_element_name(); |
807 |
|
|
808 |
|
if (is_array($values)) { |
809 |
|
$c = container(); |
810 |
|
foreach ( $values as $value ) { |
811 |
|
$c->add( form_hidden($name, $value ) ); |
812 |
|
} |
813 |
|
return $c; |
814 |
|
} else { |
815 |
|
return form_hidden($name, $this->get_value() ); |
816 |
|
} |
817 |
|
} |
818 |
|
|
819 |
} // FormElement |
} // FormElement |
820 |
?> |
?> |