Changeset 396 for trunk/patForms

Show
Ignore:
Timestamp:
09/27/07 20:01:56 (1 year ago)
Author:
axel
Message:

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.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/patForms/Element.php

    r350 r396  
    407407    */ 
    408408    var $containerElement = false; 
    409      
     409 
    410410   /** 
    411411    * id of the last rule that has been added using the format attribute 
     
    415415    */ 
    416416    var $_formatRuleId = null; 
    417      
     417 
     418   /** 
     419    * contains the function name for calculating strlen of a string. 
     420    * Maybe mb_strlen is available so this will be used instead. 
     421    * 
     422    * @access protected 
     423    * @var    string 
     424    */ 
     425    var $_lenFunc = 'strlen'; 
     426 
    418427   /** 
    419428    * constructor - extend this in your class if you need to do specific operations 
     
    431440        if ($format !== false) { 
    432441            $this->format = $format; 
     442        } 
     443 
     444        if (function_exists('mb_strlen')){ 
     445            $this->_lenFunc = 'mb_strlen'; 
    433446        } 
    434447 
     
    700713            $this->removeRule($this->_formatRuleId); 
    701714        } 
    702          
     715 
    703716        switch (strtolower($format)) { 
    704717            case 'email': 
     
    10571070    * @access public 
    10581071    * @return boolean 
    1059     */  
     1072    */ 
    10601073    function isContainerElement() 
    10611074    { 
    10621075        return $this->containerElement; 
    10631076    } 
    1064      
     1077 
    10651078   /** 
    10661079    * add a custom validation rule 
     
    10941107            $ruleId = $rule->getId(); 
    10951108        } 
    1096         
     1109 
    10971110        for ($i = 0; $i < count($this->_rules); $i++) { 
    10981111            if ($ruleId != $this->_rules[$i]['rule']->getId()) { 
     
    11061119                    break; 
    11071120                } 
    1108                 $this->_rules[$i] = $this->_rules[$next];               
     1121                $this->_rules[$i] = $this->_rules[$next]; 
    11091122                $i++; 
    11101123            } 
    11111124        } 
    11121125    } 
    1113      
     1126 
    11141127   /** 
    11151128    * adds an observer to the element 
     
    18141827           $encoding = 'ISO-8859-1'; 
    18151828        } 
    1816          
     1829 
    18171830        switch( $type ) 
    18181831        { 
     
    19631976   /** 
    19641977    * Apply a simple filter 
    1965     *  
     1978    * 
    19661979    * To create a simple filter you can supply to 
    19671980    * functions, one to filter the data you receive 
     
    22752288        return $value; 
    22762289    } 
     2290 
     2291   /** 
     2292    * Calcualte string length 
     2293    * 
     2294    * This method return's the string length of given value. 
     2295    * 
     2296    * @access   public 
     2297    * @param    string      value 
     2298    * @return   integer     string length of given value 
     2299    */ 
     2300    function strlen( &$value ) 
     2301    { 
     2302        $func = $this->_lenFunc; 
     2303        return $func( $value ); 
     2304    } 
    22772305} 
    22782306?> 
  • trunk/patForms/Element/String.php

    r361 r396  
    337337        if( $this->attributes["type"] == "password" ) 
    338338        { 
    339             $display = str_repeat( "*", strlen( $value ) ); 
     339            $display = str_repeat( "*", $this->strlen( $value ) ); 
    340340        } 
    341341 
     
    359359            $required   =   true; 
    360360 
    361         if( strlen( $value ) == 0 ) 
     361        if( $this->strlen( $value ) == 0 ) 
    362362            $empty  =   true; 
    363363 
     
    379379 
    380380        // check for tags 
    381         if( strlen( $this->attributes["allowedtags"] ) ) 
     381        if( $this->strlen( $this->attributes["allowedtags"] ) ) 
    382382        { 
    383383            $allowed    =   explode( ',', $this->attributes["allowedtags"] ); 
     
    388388            $allowed    =   implode( '', $allowed ); 
    389389            $newValue   =   strip_tags( $value, $allowed ); 
    390             if( strlen( $newValue ) != strlen( $value ) ) 
     390            if( $this->strlen( $newValue ) != $this->strlen( $value ) ) 
    391391            { 
    392392                $this->addValidationError( 6, array( 'allowedtags' => htmlspecialchars( $allowed ) ) ); 
     
    397397        { 
    398398            $newValue   =   strip_tags( $value ); 
    399             if( strlen( $newValue ) != strlen( $value ) ) 
     399            if( $this->strlen( $newValue ) != $this->strlen( $value ) ) 
    400400            { 
    401401                $this->addValidationError( 7 ); 
     
    412412 
    413413        // maxlength 
    414         if( isset( $this->attributes["maxlength"] ) && strlen( $value ) > $this->attributes["maxlength"] ) 
     414        if( isset( $this->attributes["maxlength"] ) && $this->strlen( $value ) > $this->attributes["maxlength"] ) 
    415415        { 
    416416            $this->addValidationError( 4, array( "maxlength" => $this->attributes["maxlength"] ) ); 
  • trunk/patForms/Element/Text.php

    r336 r396  
    22/** 
    33 * simple textfield patForms element that builds and validates text input fields. 
    4  *  
     4 * 
    55 * @access      protected 
    66 * @package     patForms 
     
    1111/** 
    1212 * simple textfield patForms element that builds and validates text input fields. 
    13  *  
     13 * 
    1414 * @access      protected 
    1515 * @package     patForms 
     
    2929   /** 
    3030    * 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  
     31    * if you want to use the {@link patForms_Element::element2html()} method to 
    3232    * create the final HTML tag for your element. 
    3333    * 
     
    3535    * @see      patForms_Element::element2html() 
    3636    */ 
    37     var $elementType    =   array(  
     37    var $elementType    =   array( 
    3838        'html'  =>  'input', 
    3939    ); 
    40      
     40 
    4141   /** 
    4242    * set here which attributes you want to include in the element if you want to use 
     
    4747    * @see      patForms_Element::convertDefinition2Attribute() 
    4848    */ 
    49     var $attributeDefinition = array(   
    50              
    51         'id' => array(  
    52             'required'      =>  false, 
    53             'format'        =>  'string', 
    54             'outputFormats' =>  array( 'html' ), 
    55         ), 
    56         'name' => array(    
     49    var $attributeDefinition = array( 
     50 
     51        'id' => array( 
     52            'required'      =>  false, 
     53            'format'        =>  'string', 
     54            'outputFormats' =>  array( 'html' ), 
     55        ), 
     56        'name' => array( 
    5757            'required'      =>  true, 
    5858            'format'        =>  'string', 
     
    6060            'modifiers'     =>  array( 'insertSpecials' => array() ), 
    6161        ), 
    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(    
     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( 
    8585            'required'      =>  false, 
    8686            'format'        =>  'string', 
     
    8888            'outputFormats' =>  array(), 
    8989        ), 
    90         'display' => array(     
     90        'display' => array( 
    9191            'required'      =>  false, 
    9292            'format'        =>  'string', 
     
    9494            'outputFormats' =>  array(), 
    9595        ), 
    96         'required' => array(    
     96        'required' => array( 
    9797            'required'      =>  false, 
    9898            'format'        =>  'string', 
     
    100100            'outputFormats' =>  array(), 
    101101        ), 
    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(    
     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( 
    164164            'required'      =>  false, 
    165165            'format'        =>  'int', 
     
    205205        ), 
    206206    ); 
    207          
     207 
    208208    /** 
    209209     *  define error codes and messages for each form element 
     
    242242    ); 
    243243 
    244      
     244 
    245245   /** 
    246246    * element creation method for the 'HTML' format in the 'default' form mode. 
     
    255255            return $this->createDisplaylessTag( $value ); 
    256256        } 
    257          
     257 
    258258        if( $this->attributes['edit'] == 'no' ) { 
    259259            $this->attributes['disabled']   =   'yes'; 
    260260        } 
    261          
     261 
    262262        return $this->createTag( 'textarea', 'full', $this->getAttributesFor( $this->getFormat() ), $value ); 
    263263    } 
    264      
     264 
    265265   /** 
    266266    * element creation method for the 'HTML' format in the 'readonly' form mode. 
     
    274274    { 
    275275        $tag = $this->createDisplaylessTag( $value ); 
    276          
     276 
    277277        if( $this->attributes['display'] == 'no' ) { 
    278278            return $tag; 
    279279        } 
    280280 
    281         return  $value.$tag;        
     281        return  $value.$tag; 
    282282    } 
    283      
     283 
    284284   /** 
    285285    * validates the element. 
     
    293293        $required   =   false; 
    294294        $empty      =   false; 
    295          
     295 
    296296        // store the required flag for easy access 
    297297        if( isset( $this->attributes['required'] ) && $this->attributes['required'] == 'yes' ) { 
    298298            $required   =   true; 
    299299        } 
    300          
    301         if( strlen( $value ) == 0 ) { 
     300 
     301        if( $this->strlen( $value ) == 0 ) { 
    302302            $empty  =   true; 
    303303        } 
    304              
     304 
    305305        if( $empty && $required ) { 
    306306            $this->addValidationError( 1 ); 
    307307            return false; 
    308308        } 
    309              
     309 
    310310        if( $empty && !$required ) { 
    311311            return true; 
    312312        } 
    313              
     313 
    314314        // is it a string? 
    315315        if( !is_string( $value ) ) { 
     
    317317            return false; 
    318318        } 
    319          
     319 
    320320        // check for allowed tags 
    321321        if( $this->attributes['allowedtags'] && $this->attributes['allowedtags'] != '*' ) { 
     
    326326            $allowed    =   implode( '', $allowed ); 
    327327            $newValue   =   strip_tags( $value, $allowed ); 
    328             if( strlen( $newValue ) != strlen( $value ) ) { 
     328            if( $this->strlen( $newValue ) != $this->strlen( $value ) ) { 
    329329                $this->addValidationError( 6, array( 'allowedtags' => $this->attributes['allowedtags'] ) ); 
    330330                return false; 
     
    334334        // check for denied tags 
    335335        if( $this->attributes['deniedtags'] ) { 
    336          
     336 
    337337            // must prohibit any tag 
    338338            if( $this->attributes['deniedtags'] == '*' )  { 
    339339                $newValue   =   strip_tags( $value ); 
    340                 if( strlen( $newValue ) != strlen( $value ) ) { 
     340                if( $this->strlen( $newValue ) != $this->strlen( $value ) ) { 
    341341                    $this->addValidationError( 5 ); 
    342342                    return false; 
     
    351351                } 
    352352            } 
    353              
     353 
    354354        } 
    355          
    356          
     355 
     356 
    357357        // minlength 
    358         if( isset( $this->attributes['minlength'] ) && strlen( $value ) < $this->attributes['minlength'] ) { 
     358        if( isset( $this->attributes['minlength'] ) && $this->strlen( $value ) < $this->attributes['minlength'] ) { 
    359359            $this->addValidationError( 3, array( 'minlength' => $this->attributes['minlength'] ) ); 
    360360            return false; 
    361361        } 
    362          
     362 
    363363        // maxlength 
    364         if( isset( $this->attributes['maxlength'] ) && strlen( $value ) > $this->attributes['maxlength'] ) { 
     364        if( isset( $this->attributes['maxlength'] ) && $this->strlen( $value ) > $this->attributes['maxlength'] ) { 
    365365            $this->addValidationError( 4, array( 'maxlength' => $this->attributes['maxlength'] ) ); 
    366366            return false;