root

Changeset 787

Show
Ignore:
Timestamp:
03/17/10 23:59:37 (4 months ago)
Author:
wbond
Message:

Fixed ticket #177 - added support for schema-based numeric range checking to fORMValidation

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • fORMValidation.php

    r742 r787 Hide Line Numbers
    33 * Handles validation for fActiveRecord classes 
    44 *  
    5  * @copyright  Copyright (c) 2007-2009 Will Bond, others 
     5 * @copyright  Copyright (c) 2007-2010 Will Bond, others 
    66 * @author     Will Bond [wb] <will@flourishlib.com> 
    77 * @author     Jeff Turcotte [jt] <jeff.turcotte@gmail.com> 
     
    1111 * @link       http://flourishlib.com/fORMValidation 
    1212 *  
    13  * @version    1.0.0b22 
     13 * @version    1.0.0b23 
     14 * @changes    1.0.0b23  Added support for checking integers and floats to ensure they fit within the range imposed by the database schema [wb, 2010-03-17] 
    1415 * @changes    1.0.0b22  Made the value checking for one-or-more and only-one rules more robust when detecting the absence of a value [wb, 2009-12-17] 
    1516 * @changes    1.0.0b21  Fixed a bug affecting where conditions with columns that are not null but have a default value [wb, 2009-11-03] 
     
    374375        $table = fORM::tablize($class); 
    375376         
    376         $schema      = fORMSchema::retrieve($class); 
    377         $column_info = $schema->getColumnInfo($table, $column); 
     377        $schema = fORMSchema::retrieve($class); 
     378        $info  = $schema->getColumnInfo($table, $column); 
    378379        // Make sure a value is provided for required columns 
    379         if ($values[$column] === NULL && $column_info['not_null'] && $column_info['default'] === NULL && $column_info['auto_increment'] === FALSE) { 
     380        if ($values[$column] === NULL && $info['not_null'] && $info['default'] === NULL && $info['auto_increment'] === FALSE) { 
    380381            return self::compose( 
    381382                '%sPlease enter a value', 
     
    388389         
    389390        // Make sure a valid value is chosen 
    390         if (isset($column_info['valid_values']) && $values[$column] !== NULL && !in_array($values[$column], $column_info['valid_values'])) { 
     391        if (isset($info['valid_values']) && $values[$column] !== NULL && !in_array($values[$column], $info['valid_values'])) { 
    391392            return self::compose( 
    392393                '%1$sPlease choose from one of the following: %2$s', 
    393394                fValidationException::formatField(fORM::getColumnName($class, $column)), 
    394                 join(', ', $column_info['valid_values']) 
     395                join(', ', $info['valid_values']) 
    395396            ); 
    396397        } 
    397398         
    398399        // Make sure the value isn't too long 
    399         if ($column_info['type'] == 'varchar' && isset($column_info['max_length']) && $values[$column] !== NULL && is_string($values[$column]) && fUTF8::len($values[$column]) > $column_info['max_length']) { 
     400        if ($info['type'] == 'varchar' && isset($info['max_length']) && $values[$column] !== NULL && is_string($values[$column]) && fUTF8::len($values[$column]) > $info['max_length']) { 
    400401            return self::compose( 
    401402                '%1$sPlease enter a value no longer than %2$s characters', 
    402403                fValidationException::formatField(fORM::getColumnName($class, $column)), 
    403                 $column_info['max_length'] 
     404                $info['max_length'] 
    404405            ); 
    405406        } 
    406407         
    407408        // Make sure the value is the proper length 
    408         if ($column_info['type'] == 'char' && isset($column_info['max_length']) && $values[$column] !== NULL && is_string($values[$column]) && fUTF8::len($values[$column]) != $column_info['max_length']) { 
     409        if ($info['type'] == 'char' && isset($info['max_length']) && $values[$column] !== NULL && is_string($values[$column]) && fUTF8::len($values[$column]) != $info['max_length']) { 
    409410            return self::compose( 
    410411                '%1$sPlease enter exactly %2$s characters', 
    411412                fValidationException::formatField(fORM::getColumnName($class, $column)), 
    412                 $column_info['max_length'] 
     413                $info['max_length'] 
     414            ); 
     415        } 
     416         
     417        // Make sure the value fits in the numeric range 
     418        if (self::stringlike($values[$column]) && in_array($info['type'], array('integer', 'float')) && $info['min_value'] && $info['max_value'] && ($info['min_value']->gt($values[$column]) || $info['max_value']->lt($values[$column]))) { 
     419            return self::compose( 
     420                '%1$sPlease enter a number between %2$s and %3$s', 
     421                fValidationException::formatField(fORM::getColumnName($class, $column)), 
     422                $info['min_value']->__toString(), 
     423                $info['max_value']->__toString() 
    413424            ); 
    414425        } 
     
    14191430 
    14201431/** 
    1421  * Copyright (c) 2007-2009 Will Bond <will@flourishlib.com>, others 
     1432 * Copyright (c) 2007-2010 Will Bond <will@flourishlib.com>, others 
    14221433 *  
    14231434 * Permission is hereby granted, free of charge, to any person obtaining a copy