Permalink
Browse files

Fixing the ability to use regular expressions for validation rules. F…

…ixes #453
  • Loading branch information...
1 parent 688daf2 commit 49c60658b965da4193d653547535b37746a35f2e @markstory markstory committed Mar 14, 2010
Showing with 25 additions and 3 deletions.
  1. +8 −2 cake/console/libs/tasks/model.php
  2. +17 −1 cake/tests/cases/console/libs/tasks/model.test.php
@@ -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];
@@ -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/')));
@@ -252,6 +252,22 @@ function testInteractiveFieldValidationWithBogusResponse() {
}
/**
+ * 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
*
* @return void

0 comments on commit 49c6065

Please sign in to comment.