/[cvs]/nfo/php/libs/com.newsblob.phphtmllib/form/FormElement.inc
ViewVC logotype

Annotation of /nfo/php/libs/com.newsblob.phphtmllib/form/FormElement.inc

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations)
Sat Feb 22 21:07:42 2003 UTC (21 years, 4 months ago) by jonen
Branch: MAIN
+ updated whole lib to version 2.2.1 (new FormProcessing since 2.2.0!)

1 jonen 1.1 <?php
2     /**
3     * This file contains the base FormElement class.
4     *
5     * $Id: FormElement.inc,v 1.21 2003/02/21 05:47:37 hemna Exp $
6     *
7     * @author Walter A. Boring IV <waboring@buildabetterweb.com>
8     * @author Suren Markosyan <suren@bcsweb.com>
9     * @package phpHtmlLib
10     * @subpackage FormProcessing
11     *
12     * @copyright LGPL - See LICENCE
13     *
14     */
15    
16     /**
17     * This is the base FormElement object. It can be
18     * single form field such as a text input field,
19     * or a complex object.
20     *
21     * Usefull Functions
22     *
23     * get_value() - This gets the current
24     * 'value' of the FormElement.
25     * NOTE: This can be a 'complex' FormElement
26     * in which it may return an array of
27     * values.
28     *
29     * set_value() - Set the current value
30     * for this FormElement.
31     *
32     * get_label() - returns the label for this
33     * FormElement.
34     *
35     *
36     * @package phpHtmlLib
37     * @subpackage FormProcessing
38     */
39     class FormElement {
40    
41     /**
42     * Holds the elements label text
43     *
44     */
45     var $_label_text = NULL;
46    
47     /**
48     * Holds the elements initial value
49     *
50     */
51     var $_value = NULL;
52    
53     /**
54     * Indicates whether this elements
55     * final value is required and cannot
56     * be empty
57     */
58     var $_is_required = TRUE;
59    
60     /**
61     * Holds the error message text
62     * for validation errors, if any
63     *
64     */
65     var $_error_message = NULL;
66    
67     /**
68     * Holds the state of the last validation
69     * Sets to true in case of a validation error
70     *
71     */
72     var $_has_error = FALSE;
73    
74     /**
75     * Holds the name of the element
76     * as it appears in the form html tag
77     *
78     */
79     var $_element_name = NULL;
80    
81     /**
82     * Holds additional attributes for
83     * the elements html tag
84     *
85     */
86     var $_attributes;
87    
88     /**
89     * Holds additional style attributes for
90     * the elements html tag
91     *
92     */
93     var $_style_attributes;
94    
95     /**
96     * Indicates a disabled element
97     *
98     */
99     var $_is_disabled = FALSE;
100    
101    
102     /**
103     * The constructor
104     *
105     * @param label string - text label for the element
106     * @param bool required - is this a required element
107     */
108     function FormElement($label, $required = TRUE) {
109    
110     $this->set_label_text($label);
111     $this->set_required($required);
112    
113     // create a unique name to be used in the html form
114     $this->create_element_name();
115    
116     //This sets the initial value of the element
117     $this->set_value( $this->get_init_value() );
118     }
119    
120    
121     /**
122     * This function will return the
123     * elements label text
124     *
125     * @return string
126     */
127     function get_label_text() {
128     return $this->_label_text;
129     }
130    
131     /**
132     * This function will set the
133     * label for the element
134     *
135     * @param label string
136     */
137     function set_label_text($label) {
138     $this->_label_text = $label;
139     }
140    
141     /**
142     * This function will return the
143     * elements value
144     *
145     * @return mixed
146     */
147     function get_value() {
148     return $this->_value;
149     }
150    
151     /**
152     * This function will set the
153     * initial value for the element
154     *
155     * @param value mixed
156     */
157     function set_value($value) {
158     $this->_value = $value;
159     }
160    
161     /**
162     * This provides a method
163     * for the FormContent
164     * to get access to the
165     * text associated with a
166     * field. This is only available
167     * on FormElements that have text
168     * associated with a field.
169     * It is used during Confirmation
170     *
171     * @param mixed - the value to look up
172     * @return string - the text associated
173     */
174     function get_value_text($value) {
175     return $this->get_value();
176     }
177    
178    
179     /**
180     * This function set the elements
181     * required state
182     *
183     * @param bool required
184     */
185     function set_required($required) {
186     $this->_is_required = $required;
187     }
188    
189     /**
190     * Returns whether this elements
191     * final value cannot be empty
192     *
193     * @return bool requried
194     */
195     function is_required() {
196     return $this->_is_required;
197     }
198    
199    
200     /**
201     * This returns the initial value of
202     * the element
203     *
204     * @return mixed
205     */
206     function get_init_value() {
207     return $_REQUEST[str_replace("[]","",$this->get_element_name())];
208     }
209    
210    
211     /**
212     * Sets the disabled element flag
213     *
214     * @param bool disabled
215     */
216     function set_disabled($flag) {
217     $this->_is_disabled = $flag;
218     }
219    
220     /**
221     * Returns the elements disabled
222     * state
223     *
224     * @return bool disabled
225     */
226     function is_disabled() {
227     return $this->_is_disabled;
228     }
229    
230    
231     /********* Tag attributes methods *********/
232    
233    
234     /**
235     * add a single attribute (name="value")
236     *
237     * @param string $name attribute name
238     * @param mixed $value the value
239     *
240     */
241     function set_attribute($name, $value = NULL) {
242     if ($value == NULL) {
243     $this->_attributes[] = $name;
244     }
245     else {
246     $this->_attributes[$name] = $value;
247     }
248     }
249    
250     /**
251     * return a single attribute
252     *
253     * @param string $name attribute name
254     * @return mixed $value the value
255     *
256     */
257     function get_attribute($name) {
258     return $this->_attributes[$name];
259     }
260    
261     /**
262     * Sets elements title text
263     *
264     * @param string title
265     */
266     function set_title($title) {
267     $this->set_attribute("title", $title);
268     }
269    
270     /**
271     * Sets elements css attribute
272     *
273     * @param string $name attribute name
274     * @param mixed $value the value
275     */
276     function set_style_attribute($name, $value) {
277     $this->_style_attributes[$name] = $value;
278     }
279    
280    
281     /********* Error handling methods *********/
282    
283     /**
284     * Defines error message text and
285     * sets the error flag to true
286     *
287     * @param mesage text - error message
288     */
289     function set_error_message($message) {
290     $this->_error_message = $message;
291     $this->_has_error = TRUE;
292     }
293    
294    
295     /**
296     * Returns the current error message
297     * if any
298     *
299     * @return mesage text - error message
300     */
301     function get_error_message() {
302     return $this->_error_message;
303     }
304    
305     /**
306     * Returns the current error state
307     *
308     *
309     * @return bool error state
310     */
311     function has_error() {
312     return $this->_has_error;
313     }
314    
315    
316     /********* Element name handling methods *********/
317    
318     /**
319     * This function creates element name
320     * used in the form based on the text label
321     * or any other parameters
322     *
323     */
324     function create_element_name() {
325    
326     $name = strtolower($this->get_label_text());
327     $len = strlen($name);
328    
329     for ($i=0; $i<$len;$i++) {
330     if ((ord($name[$i])<97 || ord($name[$i])>122) && (ord($name[$i])<48 || ord($name[$i])>57))
331     $name[$i] = "_";
332     }
333    
334     $this->_element_name = $name;
335     }
336    
337     /**
338     * Returns the element name
339     * to be used in the form
340     *
341     * @return string form element name
342     */
343     function get_element_name() {
344     return $this->_element_name;
345     }
346    
347    
348     /********* Validation methods *********/
349    
350    
351     /**
352     * This function performs the actual validation
353     * It is called only if the validation is required by
354     * this element
355     *
356     * This function is responsible for performing complete
357     * validation and setting the appropriate error message
358     * in case of a failed validation
359     *
360     */
361     function validate() {
362     return TRUE;
363     }
364    
365    
366     /**
367     * This function checks if the validation
368     * is nesseccary and calls the validate method
369     *
370     */
371     function _do_validation() {
372     if ($this->get_value() != NULL) {
373     return $this->validate();
374     } else if ($this->is_required()) {
375     $this->set_error_message("This field cannot be empty");
376     return FALSE;
377     } else {
378     return TRUE;
379     }
380     }
381    
382    
383     /********* JavaScript event methods *********/
384    
385     /**
386     * This function return the javaScript code for
387     * an onClick event
388     *
389     * @return string - javascript code
390     */
391     function onClick() {
392     return NULL;
393     }
394    
395     /**
396     * This function return the javaScript code for
397     * an onFocus event
398     *
399     * @return string - javascript code
400     */
401     function onFocus() {
402     return NULL;
403     }
404    
405     /**
406     * This function return the javaScript code for
407     * an onSubmit event
408     *
409     * @return string - javascript code
410     */
411     function onSubmit() {
412     return NULL;
413     }
414    
415     /**
416     * This function return the javaScript code for
417     * an onBlur event
418     *
419     * @return string - javascript code
420     */
421     function onBlur() {
422     return NULL;
423     }
424    
425    
426     /**
427     * this function retuns the javaScript code for
428     * an onChange event
429     *
430     * @return string - javascript code
431     */
432     function onChange() {
433     return NULL;
434     }
435    
436     /**
437     * This function builds the complete javaScript events code
438     * for the element
439     *
440     * @return array - attributes
441     */
442     function _build_javascript() {
443    
444     $javascript = array();
445    
446     if (($js=$this->onClick()) != NULL) {
447     $javascript[] = "onClick=\"" . $js . "\"";
448     }
449    
450     if (($js=$this->onFocus()) != NULL) {
451     $javascript[] = "onFocus=\"" . $js . "\"";
452     }
453    
454     if (($js=$this->onSubmit()) != NULL) {
455     $javascript[] = "onSubmit=\"" . $js . "\"";
456     }
457    
458     if (($js=$this->onBlur()) != NULL) {
459     $javascript[] = "onBlur=\"" . $js . "\"";
460     }
461    
462     if (($js=$this->onChange()) != NULL) {
463     $javascript[] = "onChange=\"" . $js . "\"";
464     }
465    
466     if (count($javascript)>0) {
467     return $javascript;
468     }
469     else {
470     return NULL;
471     }
472     }
473    
474    
475     /********* rendering methods *********/
476    
477    
478     /**
479     * This function return the symbol used to
480     * denote a required field
481     *
482     * @return string - required symbol
483     */
484     function get_required_symbol() {
485     return '*';
486     }
487    
488     /**
489     * This function builds the element form attributes
490     *
491     * @return array attributes
492     */
493     function _build_element_attributes() {
494    
495     $attributes = $this->_attributes;
496    
497     if ($this->_style_attributes) {
498     // build the css styles
499     foreach ($this->_style_attributes as $name=>$value) {
500     $style[] = $name . ":" . $value . ";";
501     }
502     $attributes["style"] = implode("", $style);
503     }
504    
505     // set the element tag name
506     $attributes["name"] = $this->get_element_name();
507    
508     if (($js = $this->_build_javascript()) != NULL) {
509     // build javaScript attributes
510     $attributes = array_merge($attributes, $js);
511     }
512    
513     //see if its disabled
514     if ($this->is_disabled()) {
515     $attributes[] = "disabled";
516     }
517    
518     return $attributes;
519     }
520    
521    
522     /**
523     * This function builds and returns a
524     * label object based on the label text
525     * and error conditions
526     *
527     * @return object SPANtag
528     */
529     function get_label() {
530    
531     $label = $this->get_label_text();
532    
533     if ($this->is_required()) $label .= ' ' . $this->get_required_symbol();
534    
535     $span = html_span("formlabel", $label);
536    
537     if ($this->has_error()) {
538     $span->set_tag_attribute("style","color:red;");
539     }
540    
541     return $span;
542     }
543    
544    
545     /**
546     * This function builds and returns the
547     * form element object
548     *
549     * @return object
550     */
551     function get_element() {
552    
553     $span = new SPANtag($this->_build_element_attributes(), $this->get_value());
554    
555     return $span;
556     }
557    
558     } // FormElement
559     ?>

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed