Fixing the ability to use regular expressions for validation rules. Fixes #453

This commit is contained in:
Mark Story 2010-03-13 19:44:20 -05:00
parent 688daf2c35
commit 49c60658b9
2 changed files with 25 additions and 3 deletions

View file

@ -410,7 +410,7 @@ class ModelTask extends BakeTask {
$this->out(__("You have already chosen that validation rule,\nplease choose again", true));
continue;
}
if (!isset($this->_validations[$choice])) {
if (!isset($this->_validations[$choice]) && is_numeric($choice)) {
$this->out(__('Please make a valid selection.', true));
continue;
}
@ -418,7 +418,13 @@ class ModelTask extends BakeTask {
} else {
$choice = $guess;
}
$validatorName = $this->_validations[$choice];
if (isset($this->_validations[$choice])) {
$validatorName = $this->_validations[$choice];
} else {
$validatorName = Inflector::slug($choice);
}
if ($choice != $defaultChoice) {
if (is_numeric($choice) && isset($this->_validations[$choice])) {
$validate[$validatorName] = $this->_validations[$choice];

View file

@ -241,7 +241,7 @@ class ModelTaskTest extends CakeTestCase {
function testInteractiveFieldValidationWithBogusResponse() {
$this->Task->initValidations();
$this->Task->interactive = true;
$this->Task->setReturnValueAt(0, 'in', '--bogus--');
$this->Task->setReturnValueAt(0, 'in', '999999');
$this->Task->setReturnValueAt(1, 'in', '19');
$this->Task->setReturnValueAt(2, 'in', 'n');
$this->Task->expectAt(4, 'out', array(new PatternExpectation('/make a valid/')));
@ -251,6 +251,22 @@ class ModelTaskTest extends CakeTestCase {
$this->assertEqual($result, $expected);
}
/**
* test that a regular expression can be used for validation.
*
* @return void
*/
function testInteractiveFieldValidationWithRegexp() {
$this->Task->initValidations();
$this->Task->interactive = true;
$this->Task->setReturnValueAt(0, 'in', '/^[a-z]{0,9}$/');
$this->Task->setReturnValueAt(1, 'in', 'n');
$result = $this->Task->fieldValidation('text', array('type' => 'string', 'length' => 10, 'null' => false));
$expected = array('a_z_0_9' => '/^[a-z]{0,9}$/');
$this->assertEqual($result, $expected);
}
/**
* test the validation Generation routine
*