root/trunk/patForms/Element/Text.php

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