Applied patch by 'GreyCells', closes #4629

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6956 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
the_undefined 2008-05-19 07:10:06 +00:00
parent f6aa0e0a09
commit c3d266e27d
2 changed files with 63 additions and 2 deletions

View file

@ -412,6 +412,37 @@ class Validation extends Object {
}
return false;
}
/**
* Time validation, determines if the string passed is a valid time.
* Validates time as 24hr (HH:MM) or am/pm ([H]H:MM[a|p]m)
* Does not allow/validate seconds.
*
* @param string $check a valid time string
* @return boolean Success
* @access public
*/
function time($check) {
$_this =& Validation::getInstance();
$_this->__reset();
$_this->check = $check;
$_this->regex = '%^((0?[1-9]|1[012])(:[0-5]\d){0,2}([AP]M|[ap]m))$|^([01]\d|2[0-3])(:[0-5]\d){0,2}$%';
return $_this->_check();
}
/**
* Boolean validation, determines if value passed is a boolean integer or true/false.
*
* @param string $check a valid boolean
* @return boolean Success
* @access public
*/
function boolean($check) {
$booleanList = array(0,1,'0','1',true,false);
return in_array($check, $booleanList, true);
}
/**
* Checks that a value is a valid decimal. If $places is null, the $check is allowed to be a scientific float
* If no decimal point is found a false will be returned. Both the sign and exponent are optional.
@ -868,4 +899,4 @@ class Validation extends Object {
$_this->type = null;
}
}
?>
?>

View file

@ -1123,6 +1123,36 @@ class ValidationTestCase extends UnitTestCase {
$this->assertFalse(Validation::date('12 06', array('my')));
}
function testTime() {
$this->assertTrue(Validation::time('00:00'));
$this->assertTrue(Validation::time('23:59'));
$this->assertFalse(Validation::time('24:00'));
$this->assertTrue(Validation::time('12:00'));
$this->assertTrue(Validation::time('12:01'));
$this->assertTrue(Validation::time('12:01am'));
$this->assertTrue(Validation::time('12:01pm'));
$this->assertTrue(Validation::time('1pm'));
$this->assertTrue(Validation::time('01:00'));
$this->assertFalse(Validation::time('1:00'));
$this->assertTrue(Validation::time('1:00pm'));
$this->assertFalse(Validation::time('13:00pm'));
$this->assertFalse(Validation::time('9:00'));
}
function testBoolean() {
$this->assertTrue(Validation::boolean('0'));
$this->assertTrue(Validation::boolean('1'));
$this->assertTrue(Validation::boolean(0));
$this->assertTrue(Validation::boolean(1));
$this->assertTrue(Validation::boolean(true));
$this->assertTrue(Validation::boolean(false));
$this->assertFalse(Validation::boolean('true'));
$this->assertFalse(Validation::boolean('false'));
$this->assertFalse(Validation::boolean('-1'));
$this->assertFalse(Validation::boolean('2'));
$this->assertFalse(Validation::boolean('Boo!'));
}
function testDateCustomRegx() {
$this->assertTrue(Validation::date('2006-12-27', null, '%^(19|20)[0-9]{2}[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$%'));
$this->assertFalse(Validation::date('12-27-2006', null, '%^(19|20)[0-9]{2}[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$%'));
@ -1489,4 +1519,4 @@ class ValidationTestCase extends UnitTestCase {
// $this->assertTrue(Validation::file(TEST_CAKE_CORE_INCLUDE_PATH. 'VERSION.txt'));
// }
}
?>
?>