Merge pull request #3478 from Schlaefer/#3303-lengthBetween

closes #3303 RFC: Rename Validator::between() into Validator::length()
This commit is contained in:
Mark Story 2014-05-24 16:48:05 -04:00
commit 9c9d45596f
9 changed files with 48 additions and 34 deletions

View file

@ -142,8 +142,8 @@ class Model extends Object implements CakeEventListener {
*
* {{{
* public $validate = array(
* 'age' => array(
* 'rule' => array('between', 5, 25)
* 'length' => array(
* 'rule' => array('lengthBetween', 5, 25)
* )
* );
* }}}
@ -171,9 +171,9 @@ class Model extends Object implements CakeEventListener {
*
* {{{
* public $validate = array(
* 'age' => array(
* 'rule' => array('between', 5, 25),
* 'message' => array('The age must be between %d and %d.')
* 'length' => array(
* 'rule' => array('lengthBetween', 5, 15),
* 'message' => array('Between %d to %d characters')
* )
* );
* }}}

View file

@ -539,7 +539,7 @@ class ModelValidator implements ArrayAccess, IteratorAggregate, Countable {
* ->add('user_id', 'valid', array('rule' => 'numeric', 'message' => 'Invalid User'))
*
* $validator->add('password', array(
* 'size' => array('rule' => array('between', 8, 20)),
* 'size' => array('rule' => array('lengthBetween', 8, 20)),
* 'hasSpecialCharacter' => array('rule' => 'validateSpecialchar', 'message' => 'not valid')
* ));
* }}}

View file

@ -186,7 +186,7 @@ class CakeValidationSet implements ArrayAccess, IteratorAggregate, Countable {
* {{{
* $set
* ->setRule('required', array('rule' => 'notEmpty', 'required' => true))
* ->setRule('inRange', array('rule' => array('between', 4, 10))
* ->setRule('between', array('rule' => array('lengthBetween', 4, 10))
* }}}
*
* @param string $name The name under which the rule should be set

View file

@ -315,7 +315,7 @@ class ModelTaskTest extends CakeTestCase {
$this->Task->initValidations();
$this->Task->interactive = true;
$this->Task->expects($this->any())->method('in')
->will($this->onConsecutiveCalls('24', 'y', '18', 'n'));
->will($this->onConsecutiveCalls('25', 'y', '19', 'n'));
$result = $this->Task->fieldValidation('text', array('type' => 'string', 'length' => 10, 'null' => false));
$expected = array('notEmpty' => 'notEmpty', 'maxLength' => 'maxLength');
@ -333,7 +333,7 @@ class ModelTaskTest extends CakeTestCase {
$this->Task->interactive = true;
$this->Task->expects($this->any())->method('in')
->will($this->onConsecutiveCalls('999999', '24', 'n'));
->will($this->onConsecutiveCalls('999999', '25', 'n'));
$this->Task->expects($this->at(10))->method('out')
->with($this->stringContains('make a valid'));
@ -368,7 +368,7 @@ class ModelTaskTest extends CakeTestCase {
$this->Task->initValidations();
$this->Task->interactive = true;
$this->Task->expects($this->any())->method('in')
->will($this->onConsecutiveCalls('24', 'y', 's'));
->will($this->onConsecutiveCalls('25', 'y', 's'));
$result = $this->Task->fieldValidation('text', array('type' => 'string', 'length' => 10, 'null' => false));
$expected = array('notEmpty' => 'notEmpty', '_skipFields' => true);
@ -384,7 +384,7 @@ class ModelTaskTest extends CakeTestCase {
$this->Task->initValidations();
$this->Task->interactive = true;
$this->Task->expects($this->any())->method('in')
->will($this->onConsecutiveCalls('24', 's'));
->will($this->onConsecutiveCalls('25', 's'));
$result = $this->Task->fieldValidation('text', array('type' => 'string', 'length' => 10, 'null' => false));
$expected = array('notEmpty' => 'notEmpty', '_skipFields' => true);
@ -400,7 +400,7 @@ class ModelTaskTest extends CakeTestCase {
public function testInteractiveDoValidationWithSkipping() {
$this->Task->expects($this->any())
->method('in')
->will($this->onConsecutiveCalls('35', '24', 'n', '11', 's'));
->will($this->onConsecutiveCalls('36', '25', 'n', '11', 's'));
$this->Task->interactive = true;
$Model = $this->getMock('Model');
$Model->primaryKey = 'id';

View file

@ -767,7 +767,7 @@ class ModelValidationTest extends BaseModelTest {
'last' => false
),
'between' => array(
'rule' => array('between', 5, 15),
'rule' => array('lengthBetween', 5, 15),
'message' => array('You may enter up to %s chars (minimum is %s chars)', 14, 6)
)
)
@ -1844,7 +1844,7 @@ class ModelValidationTest extends BaseModelTest {
$set = array(
'numeric' => array('rule' => 'numeric', 'allowEmpty' => false),
'range' => array('rule' => array('between', 1, 5), 'allowEmpty' => false),
'between' => array('rule' => array('lengthBetween', 1, 5), 'allowEmpty' => false),
);
$Validator['other'] = $set;
$rules = $Validator['other'];
@ -1853,7 +1853,7 @@ class ModelValidationTest extends BaseModelTest {
$validators = $rules->getRules();
$this->assertCount(2, $validators);
$this->assertEquals('numeric', $validators['numeric']->rule);
$this->assertEquals(array('between', 1, 5), $validators['range']->rule);
$this->assertEquals(array('lengthBetween', 1, 5), $validators['between']->rule);
$Validator['new'] = new CakeValidationSet('new', $set, array());
$rules = $Validator['new'];
@ -1862,7 +1862,7 @@ class ModelValidationTest extends BaseModelTest {
$validators = $rules->getRules();
$this->assertCount(2, $validators);
$this->assertEquals('numeric', $validators['numeric']->rule);
$this->assertEquals(array('between', 1, 5), $validators['range']->rule);
$this->assertEquals(array('lengthBetween', 1, 5), $validators['between']->rule);
}
/**
@ -1917,7 +1917,7 @@ class ModelValidationTest extends BaseModelTest {
$set = array(
'numeric' => array('rule' => 'numeric', 'allowEmpty' => false),
'range' => array('rule' => array('between', 1, 5), 'allowEmpty' => false),
'range' => array('rule' => array('lengthBetween', 1, 5), 'allowEmpty' => false),
);
$Validator['other'] = $set;
$this->assertCount(4, $Validator);
@ -1938,14 +1938,14 @@ class ModelValidationTest extends BaseModelTest {
$Validator = $TestModel->validator();
$Validator->add('other', 'numeric', array('rule' => 'numeric', 'allowEmpty' => false));
$Validator->add('other', 'range', array('rule' => array('between', 1, 5), 'allowEmpty' => false));
$Validator->add('other', 'between', array('rule' => array('lengthBetween', 1, 5), 'allowEmpty' => false));
$rules = $Validator['other'];
$this->assertEquals('other', $rules->field);
$validators = $rules->getRules();
$this->assertCount(2, $validators);
$this->assertEquals('numeric', $validators['numeric']->rule);
$this->assertEquals(array('between', 1, 5), $validators['range']->rule);
$this->assertEquals(array('lengthBetween', 1, 5), $validators['between']->rule);
}
/**
@ -1962,13 +1962,13 @@ class ModelValidationTest extends BaseModelTest {
$this->assertFalse(isset($Validator['title']));
$Validator->add('other', 'numeric', array('rule' => 'numeric', 'allowEmpty' => false));
$Validator->add('other', 'range', array('rule' => array('between', 1, 5), 'allowEmpty' => false));
$Validator->add('other', 'between', array('rule' => array('lengthBetween', 1, 5), 'allowEmpty' => false));
$this->assertTrue(isset($Validator['other']));
$Validator->remove('other', 'numeric');
$this->assertTrue(isset($Validator['other']));
$this->assertFalse(isset($Validator['other']['numeric']));
$this->assertTrue(isset($Validator['other']['range']));
$this->assertTrue(isset($Validator['other']['between']));
}
/**
@ -2126,7 +2126,7 @@ class ModelValidationTest extends BaseModelTest {
$set = array(
'numeric' => array('rule' => 'numeric', 'allowEmpty' => false),
'range' => array('rule' => array('between', 1, 5), 'allowEmpty' => false),
'between' => array('rule' => array('lengthBetween', 1, 5), 'allowEmpty' => false),
);
$Validator->add('other', $set);
@ -2136,11 +2136,11 @@ class ModelValidationTest extends BaseModelTest {
$validators = $rules->getRules();
$this->assertCount(2, $validators);
$this->assertEquals('numeric', $validators['numeric']->rule);
$this->assertEquals(array('between', 1, 5), $validators['range']->rule);
$this->assertEquals(array('lengthBetween', 1, 5), $validators['between']->rule);
$set = new CakeValidationSet('other', array(
'a' => array('rule' => 'numeric', 'allowEmpty' => false),
'b' => array('rule' => array('between', 1, 5), 'allowEmpty' => false),
'b' => array('rule' => array('lengthBetween', 1, 5), 'allowEmpty' => false),
));
$Validator->add('other', $set);

View file

@ -203,17 +203,17 @@ class ValidationTest extends CakeTestCase {
}
/**
* testBetween method
* testLengthBetween method
*
* @return void
*/
public function testBetween() {
$this->assertTrue(Validation::between('abcdefg', 1, 7));
$this->assertTrue(Validation::between('', 0, 7));
$this->assertTrue(Validation::between('אกあアꀀ豈', 1, 7));
public function testLengthBetween() {
$this->assertTrue(Validation::lengthBetween('abcdefg', 1, 7));
$this->assertTrue(Validation::lengthBetween('', 0, 7));
$this->assertTrue(Validation::lengthBetween('אกあアꀀ豈', 1, 7));
$this->assertFalse(Validation::between('abcdefg', 1, 6));
$this->assertFalse(Validation::between('ÆΔΩЖÇ', 1, 3));
$this->assertFalse(Validation::lengthBetween('abcdefg', 1, 6));
$this->assertFalse(Validation::lengthBetween('ÆΔΩЖÇ', 1, 3));
}
/**

View file

@ -36,7 +36,7 @@ class Extract extends AppModel {
'message' => 'double "quoted" validation'
),
'between' => array(
'rule' => array('between', 5, 15),
'rule' => array('lengthBetween', 5, 15),
'message' => "single 'quoted' validation"
)
),

View file

@ -38,7 +38,7 @@ class PersisterOne extends AppModel {
'message' => 'Post title is required'
),
'between' => array(
'rule' => array('between', 5, 15),
'rule' => array('lengthBetween', 5, 15),
'message' => array('You may enter up to %s chars (minimum is %s chars)', 14, 6)
)
),

View file

@ -104,11 +104,25 @@ class Validation {
* @param integer $max Maximum value in range (inclusive)
* @return boolean Success
*/
public static function between($check, $min, $max) {
public static function lengthBetween($check, $min, $max) {
$length = mb_strlen($check);
return ($length >= $min && $length <= $max);
}
/**
* Alias of Validator::lengthBetween() for backwards compatibility.
*
* @see Validator::lengthBetween()
* @deprecated Deprecated since 2.6, use Validator::lengthBetween() instead.
* @param string $check Value to check for length
* @param integer $min Minimum value in range (inclusive)
* @param integer $max Maximum value in range (inclusive)
* @return boolean Success
*/
public static function between($check, $min, $max) {
return self::lengthBetween($check, $min, $max);
}
/**
* Returns true if field is left blank -OR- only whitespace characters are present in its value
* Whitespace characters include Space, Tab, Carriage Return, Newline