Skip to content

Commit

Permalink
Fixing the ability to use regular expressions for validation rules. F…
Browse files Browse the repository at this point in the history
…ixes #453
  • Loading branch information
markstory committed Mar 14, 2010
1 parent 688daf2 commit 49c6065
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
10 changes: 8 additions & 2 deletions cake/console/libs/tasks/model.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,15 +410,21 @@ function fieldValidation($fieldName, $metaData, $primaryKey = 'id') {
$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;
}
$alreadyChosen[] = $choice;
} 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];
Expand Down
18 changes: 17 additions & 1 deletion cake/tests/cases/console/libs/tasks/model.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ function testInteractiveFieldValidation() {
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/')));
Expand All @@ -251,6 +251,22 @@ function testInteractiveFieldValidationWithBogusResponse() {
$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
*
Expand Down

0 comments on commit 49c6065

Please sign in to comment.