Development

Changeset 1436

You must first sign up to be able to contribute.

Changeset 1436

Show
Ignore:
Timestamp:
06/13/06 14:31:11 (2 years ago)
Author:
fabien
Message:
  • Added Integer support to sfNumberValidator (Fixes Ticket #494)
  • Also made a few adjustments to the data flow to allow for easy future expansion (i.e. check for even/odd)

(closes #494 - patch from slickrick)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lib/validator/sfNumberValidator.class.php

    r1415 r1436  
    5656    $type = strtolower($this->getParameterHolder()->get('type')); 
    5757 
    58     if ($type == 'float'
     58    switch ($type
    5959    { 
    60       if (substr_count($value, '.') != 1) 
     60      case "decimal": 
     61      case "float": 
    6162      { 
    62         // value isn't a float, shazbot! 
    63         $error = $this->getParameterHolder()->get('type_error'); 
     63        if (substr_count($value, '.') != 1) 
     64        { 
     65          // value isn't a float, shazbot! 
     66          $error = $this->getParameterHolder()->get('type_error'); 
     67          return false; 
     68        } 
    6469 
    65         return false; 
     70        // cast our value to a float 
     71        $value = (float) $value; 
     72 
     73        break; 
    6674      } 
    6775 
    68       // cast our value to a float 
    69       $value = (float) $value; 
     76      case "int": 
     77      case "integer": 
     78      { 
     79        // Note: (Both 3 AND 3.0 are BOTH considered integers and 3.1 is not) 
     80        if ((float) $value != (int) $value) 
     81        { 
     82          // is not an integer. 
     83          $error = $this->getParameterHolder()->get('type_error'); 
     84          return false; 
     85        } 
     86 
     87        // cast our value to an integer 
     88        $value = (int) $value; 
     89 
     90        break; 
     91      } 
     92 
    7093    } 
    7194 
     
    120143    $type = strtolower($this->getParameterHolder()->get('type')); 
    121144 
    122     if ($type != 'any' && $type != 'float') 
     145    // array of allowed types 
     146    $allowed_types = array('any', 'decimal', 'float', 'int', 'integer'); 
     147 
     148    if (!array_search($type, $allowed_types)) 
    123149    { 
    124150      // unknown type