root/trunk/patForms/Element/String.php

Revision 396, 11.1 kB (checked in by axel, 1 year ago)

Changed the way to calcualte the strlen. If the mb_strlen function is available, this will be used to calcuatle the length.

Added minlength and maxlength in string element example.

Now the patForms_Element_String and patForms_Element_Text classes are using the new way to calcualte the string length.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2 /**
3  * simple textfield patForms element that builds and validates text input fields.
4  *
5  * $Id$
6  *
7  * @access        protected
8  * @package        patForms
9  * @subpackage    Element
10  * @author        Sebastian Mordziol <argh@php-tools.net>
11  */
12
13 /**
14  * simple textfield patForms element that builds and validates text input fields.
15  *
16  * $Id$
17  *
18  * @access        protected
19  * @package        patForms
20  * @subpackage    Element
21  * @author        Sebastian Mordziol <argh@php-tools.net>
22  * @license        LGPL, see license.txt for details
23  */
24 class patForms_Element_String extends patForms_Element
25 {
26    /**
27     * javascript that will be displayed only once
28     *
29     * @access    private
30     * @var        array
31     */
32     var $globalJavascript    =    array(
33                                         'html'    =>    'Html/Element/String.js'
34                                     );
35
36    /**
37     * javascript that will be displayed once per instance
38     *
39     * @access    private
40     * @var        array
41     */
42     var $instanceJavascript    =    array(
43                                         'html'    =>    "var pfe_[ELEMENT::NAME] = new pFEC_String( '[ELEMENT::ID]' );\n"
44                                     );
45
46    /**
47     * Stores the name of the element - this is used mainly by the patForms
48     * error management and should be set in every element class.
49     * @access    public
50     */
51     var $elementName    =    'String';
52
53    /**
54     * the type of the element - set this to the type of element you are creating
55     * if you want to use the {@link patForms_Element::element2html()} method to
56     * create the final HTML tag for your element.
57     *
58     * @access    public
59     * @see        patForms_Element::element2html()
60     */
61     var $elementType    =    array(    "html"    =>    "input" );
62
63    /**
64     * set here which attributes you want to include in the element if you want to use
65     * the {@link patForms_Element::convertDefinition2Attributes()} method to automatically
66     * convert the values from your element definition into element attributes.
67     *
68     * @access    protected
69     * @see        patForms_Element::convertDefinition2Attribute()
70     */
71     var    $attributeDefinition    =    array(
72
73             'id' =>    array(
74                 'required'        =>    false,
75                 'format'        =>    'string',
76                 'outputFormats'    =>    array( 'html' ),
77             ),
78             'name'            =>    array(
79                 'required'        =>    true,
80                 'format'        =>    'string',
81                 'outputFormats'    =>    array( 'html' ),
82                 'modifiers'        =>    array( 'insertSpecials' => array() ),
83             ),
84             'type'            =>    array(
85                 'required'        =>    false,
86                 'format'        =>    'string',
87                 'default'        =>    'text',
88                 'outputFormats'    =>    array( 'html' ),
89             ),
90             'title'            =>    array(
91                 'required'        =>    false,
92                 'format'        =>    'string',
93                 'outputFormats'    =>    array( 'html' ),
94                 'modifiers'        =>    array( 'insertSpecials' => array() ),
95             ),
96             'description'    =>    array(
97                 'required'        =>    false,
98                 'format'        =>    'string',
99                 'outputFormats'    =>    array(),
100                 'modifiers'        =>    array( 'insertSpecials' => array() ),
101             ),
102             'default'        =>    array(
103                 'required'        =>    false,
104                 'format'        =>    'string',
105                 'outputFormats'    =>    array(),
106             ),
107             'label'            =>    array(
108                 'required'        =>    false,
109                 'format'        =>    'string',
110                 'outputFormats'    =>    array(),
111             ),
112             'display'        =>    array(
113                 'required'        =>    false,
114                 'format'        =>    'string',
115                 'default'        =>    'yes',
116                 'outputFormats'    =>    array(),
117             ),
118             'edit'            =>    array(
119                 'required'        =>    false,
120                 'format'        =>    'string',
121                 'default'        =>    'yes',
122                 'outputFormats'    =>    array(),
123             ),
124             'required'        =>    array(
125                 'required'        =>    false,
126                 'format'        =>    'string',
127                 'default'        =>    'yes',
128                 'outputFormats'    =>    array(),
129             ),
130             'value'            =>    array(
131                 'required'        =>    false,
132                 'format'        =>    'string',
133                 'outputFormats'    =>    array( 'html' ),
134             ),
135             'style'            =>    array(
136                 'required'        =>    false,
137                 'outputFormats'    =>    array( 'html' ),
138                 'format'        =>    'string',
139             ),
140             'class'            =>    array(
141                 'required'        =>    false,
142                 'outputFormats'    =>    array( 'html' ),
143                 'format'        =>    'string',
144             ),
145             'onchange'        =>    array(
146                 'required'        =>    false,
147                 'format'        =>    'string',
148                 'outputFormats'    =>    array( 'html' ),
149                 'modifiers'        =>    array( 'insertSpecials' => array() ),
150             ),
151             'onclick'        =>    array(
152                 'required'        =>    false,
153                 'format'        =>    'string',
154                 'outputFormats'    =>    array( 'html' ),
155                 'modifiers'        =>    array( 'insertSpecials' => array() ),
156             ),
157             'onfocus'        =>    array(
158                 'required'        =>    false,
159                 'format'        =>    'string',
160                 'outputFormats'    =>    array( 'html' ),
161                 'modifiers'        =>    array( 'insertSpecials' => array() ),
162             ),
163             'onkeydown'        =>    array(
164                 'required'        =>    false,
165                 'format'        =>    'string',
166                 'outputFormats'    =>    array( 'html' ),
167                 'modifiers'        =>    array( 'insertSpecials' => array() ),
168             ),
169             'onkeyup'        =>    array(
170                 'required'        =>    false,
171                 'format'        =>    'string',
172                 'outputFormats'    =>    array( 'html' ),
173                 'modifiers'        =>    array( 'insertSpecials' => array() ),
174             ),
175             'onkeypress'        =>    array(
176                 'required'        =>    false,
177                 'format'        =>    'string',
178                 'outputFormats'    =>    array( 'html' ),
179                 'modifiers'        =>    array( 'insertSpecials' => array() ),
180             ),
181             'onmouseover'    =>    array(
182                 'required'        =>    false,
183                 'format'        =>    'string',
184                 'outputFormats'    =>    array( 'html' ),
185                 'modifiers'        =>    array( 'insertSpecials' => array() ),
186             ),
187             'onmouseout'    =>    array(
188                 'required'        =>    false,
189                 'format'        =>    'string',
190                 'outputFormats'    =>    array( 'html' ),
191                 'modifiers'        =>    array( 'insertSpecials' => array() ),
192             ),
193             'onblur'        =>    array(
194                 'required'        =>    false,
195                 'format'        =>    'string',
196                 'outputFormats'    =>    array( 'html' ),
197                 'modifiers'        =>    array( 'insertSpecials' => array() ),
198             ),
199             'accesskey'        =>    array(
200                 'required'        =>    false,
201                 'format'        =>    'string',
202                 'outputFormats'    =>    array( 'html' ),
203             ),
204             'position'        =>    array(
205                 'required'        =>    false,
206                 'format'        =>    'int',
207                 'outputFormats'    =>    array(),
208             ),
209             'tabindex'        =>    array(
210                 'required'        =>    false,
211                 'format'        =>    'int',
212                 'outputFormats'    =>    array( 'html' ),
213             ),
214             'maxlength'        =>    array(
215                 'required'        =>    false,
216                 'format'        =>    'int',
217                 'outputFormats'    =>    array( 'html' ),
218             ),
219             'minlength'        =>    array(
220                 'required'        =>    false,
221                 'format'        =>    'int',
222                 'outputFormats'    =>    array(),
223             ),
224             'format'        =>    array(
225                 'required'        =>    false,
226                 'format'        =>    'string',
227                 'outputFormats'    =>    array(),
228                 'setter'        =>  'setValueFormat'
229             ),
230             'disabled'        =>    array(
231                 'required'        =>    false,
232                 'format'        =>    'string',
233                 'default'        =>    'no',
234                 'outputFormats'    =>    array( 'html' ),
235             ),
236             'size' => array(
237                 'required'        =>    false,
238                 'format'        =>    'int',
239                 'outputFormats'    =>    array( 'html' ),
240             ),
241             'allowedtags'    =>    array(
242                 'required'        =>    false,
243                 'format'        =>    'string',
244                 'default'        =>    '',
245                 'outputFormats'    =>    array(),
246             ),
247             'resendpasswd'    =>    array(
248                 'required'        =>    false,
249                 'format'        =>    'string',
250                 'default'        =>    'no',
251                 'outputFormats'    =>    array(),
252             ),
253         );
254
255     /**
256      *    define error codes an messages for each form element
257      *
258      *  @access private
259      *  @var    array    $validatorErrorCodes
260      */
261     var    $validatorErrorCodes  =   array(
262         "C"    =>    array(
263             1    =>    "This field is required, please complete it.",
264             2    =>    "Value is not a string.",
265             3    =>    "The value is shorter than the minimum length of [MINLENGTH].",
266             4    =>    "The value is longer than the maximum length of [MAXLENGTH].",
267             5    =>    "The value does not match the required input format.",
268             6    =>    "The value contains some tags that are not allowed - only [ALLOWEDTAGS] are allowed here",
269             7    =>    "Markup tags are not allowed.",
270         ),
271         "de" =>    array(
272             1    =>    "Pflichtfeld. Bitte vervollständigen Sie Ihre Angabe.",
273             2    =>    "Wert ist keine Zeichenkette.",
274             3    =>    "Eingabe zu kurz, bitte geben Sie mindestens [MINLENGTH] Zeichen ein.",
275             4    =>    "Eingabe zu lang, bitte geben Sie maximal [MAXLENGTH] Zeichen ein.",
276             5    =>    "Der angegebene Wert entspricht nicht dem gewünschten Eingabeformat.",
277             6    =>    "Der Text enthält unbekannte Tags; es sind nur '[ALLOWEDTAGS]' erlaubt.",
278             7    =>    "Tags sind nicht erlaubt.",
279         ),
280         "fr" =>    array(
281             1    =>    "Ce champ est obligatoire.",
282             2    =>    "Pas une chaîne de caractères valide.",
283             3    =>    "Valeur trop courte. Longueur minimum: [MINLENGTH] caractères.",
284             4    =>    "Valeur trop longue. Longueur maximum: [MAXLENGTH] caractères.",
285             5    =>    "La valeur ne correspond pas au format souhaité.",
286             6    =>    "Le texte contient des balises non autorisées - les balises autorisées sont '[ALLOWEDTAGS]'.",
287             7    =>    "Les balises ne sont pas autorisées.",
288         )
289     );
290
291    /**
292     * element creation method for the 'HTML' format in the 'default' form mode.
293     *
294     * @access    public
295     * @param    mixed    value of the element
296     * @return    mixed    $element    The element, or false if failed.
297     */
298     function serializeHtmlDefault( $value )
299     {
300         if ($this->attributes['type'] !== 'password' ||
301             $this->attributes['resendpasswd'] === 'yes') {
302             $this->attributes['value'] = $value;
303         }
304
305         // handle display attribute
306         if ($this->attributes['display'] == 'no') {
307             return $this->createDisplaylessTag( $value );
308         }
309
310         if ($this->attributes['edit'] == 'no') {
311             $this->attributes['disabled']    =    'yes';
312         }
313
314         // create element
315         return $this->toHtml();
316     }
317
318    /**
319     * element creation method for the 'HTML' format in the 'readonly' form mode.
320     * Very simple; just returns the stored element value.
321     *
322     * @access    public
323     * @return    string    $value    The element's value
324     */
325     function serializeHtmlReadonly( $value )
326     {
327         $tag = $this->createDisplaylessTag( $value );
328
329         if( $this->attributes['display'] == 'no' )
330         {
331             return $tag;
332         }
333
334         $display = $value;
335
336         // password: we don't want to display this as plain text...
337         if( $this->attributes["type"] == "password" )
338         {
339             $display = str_repeat( "*", $this->strlen( $value ) );
340         }
341
342         return $display.$tag;
343     }
344
345    /**
346     * validates the element.
347     *
348     * @access    public
349     * @param    mixed    value of the element
350     * @return    bool    $isValid    True if element could be validated, false otherwise.
351     */
352     function validateElement( $value )
353     {
354         $required    =    false;
355         $empty        =    false;
356
357         // store the required flag for easy access
358         if( isset( $this->attributes["required"] ) && $this->attributes["required"] == "yes" )
359             $required    =    true;
360
361         if( $this->strlen( $value ) == 0 )
362             $empty    =    true;
363
364         if( $empty && $required )
365         {
366             $this->addValidationError( 1 );
367             return false;
368         }
369
370         if( $empty && !$required )
371             return true;
372
373         // is it a string?
374         if( !is_string( $value ) )
375         {
376             $this->addValidationError( 2 );
377             return false;
378         }
379
380         // check for tags
381         if( $this->strlen( $this->attributes["allowedtags"] ) )
382         {
383             $allowed    =    explode( ',', $this->attributes["allowedtags"] );
384             for( $i = 0; $i < count( $allowed ); ++$i )
385             {
386                 $allowed[$i]    =    '<' . $allowed[$i] . '>';
387             }
388             $allowed    =    implode( '', $allowed );
389             $newValue    =    strip_tags( $value, $allowed );
390             if( $this->strlen( $newValue ) != $this->strlen( $value ) )
391             {
392                 $this->addValidationError( 6, array( 'allowedtags' => htmlspecialchars( $allowed ) ) );
393                 return false;
394             }
395         }
396         else
397         {
398             $newValue    =    strip_tags( $value );
399             if( $this->strlen( $newValue ) != $this->strlen( $value ) )
400             {
401                 $this->addValidationError( 7 );
402                 return false;
403             }
404         }
405
406         // minlength
407         if( isset( $this->attributes["minlength"] ) && strlen( $value ) < $this->attributes["minlength"] )
408         {
409             $this->addValidationError( 3, array( "minlength" => $this->attributes["minlength"] ) );
410             return false;
411         }
412
413         // maxlength
414         if( isset( $this->attributes["maxlength"] ) && $this->strlen( $value ) > $this->attributes["maxlength"] )
415         {
416             $this->addValidationError( 4, array( "maxlength" => $this->attributes["maxlength"] ) );
417             return false;
418         }
419         return true;
420     }
421 }
422 ?>
423
Note: See TracBrowser for help on using the browser.