Removing the need for first param in CakeRule constructor

This commit is contained in:
Jose Lorenzo Rodriguez 2012-05-05 18:46:58 -04:30
parent 3199b9029b
commit 877e6c0f66
4 changed files with 18 additions and 30 deletions

View file

@ -37,13 +37,6 @@ class CakeRule {
*/
protected $_valid = true;
/**
* Holds the index under which the Validator was attached
*
* @var mixed
*/
protected $_index = null;
/**
* Create or Update transaction?
*
@ -118,10 +111,8 @@ class CakeRule {
* Constructor
*
* @param array $validator [optional] The validator properties
* @param mixed $index [optional]
*/
public function __construct($index = null, $validator = array()) {
$this->_index = $index;
public function __construct($validator = array()) {
$this->_addValidatorProps($validator);
}
@ -286,10 +277,6 @@ class CakeRule {
return $this->_passedOptions[$key];
}
public function getName() {
return $this->_index;
}
/**
* Sets the rule properties from the rule entry in validate
*

View file

@ -85,7 +85,7 @@ class CakeValidationSet {
}
foreach ($ruleSet as $index => $validateProp) {
$this->_rules[$index] = new CakeRule($index, $validateProp);
$this->_rules[$index] = new CakeRule($validateProp);
}
$this->ruleSet = $ruleSet;
}
@ -116,7 +116,7 @@ class CakeValidationSet {
*/
public function validate($data, $isUpdate = false) {
$errors = array();
foreach ($this->getRules() as $rule) {
foreach ($this->getRules() as $name => $rule) {
$rule->isUpdate($isUpdate);
if ($rule->skip()) {
continue;
@ -131,7 +131,7 @@ class CakeValidationSet {
}
if ($checkRequired || !$rule->isValid()) {
$errors[] = $this->_processValidationResponse($rule);
$errors[] = $this->_processValidationResponse($name, $rule);
if ($rule->isLast()) {
break;
}
@ -193,11 +193,12 @@ class CakeValidationSet {
/**
* Fetches the correct error message for a failed validation
*
* @param string $name the name of the rule as it was configured
* @param CakeRule $rule the object containing validation information
* @return string
*/
protected function _processValidationResponse($rule) {
protected function _processValidationResponse($name, $rule) {
$message = $rule->getValidationResult();
$name = $rule->getName();
if (is_string($message)) {
return $message;
}

View file

@ -74,7 +74,7 @@ class CakeRuleTest extends CakeTestCase {
);
$methods = array();
$Rule = new CakeRule('notEmpty', $def);
$Rule = new CakeRule($def);
$Rule->process('fieldName', $data, $methods);
$this->assertFalse($Rule->isValid());
@ -94,7 +94,7 @@ class CakeRuleTest extends CakeTestCase {
);
$methods = array('mytestrule' => array($this, 'myTestRule'));
$Rule = new CakeRule('custom', $def);
$Rule = new CakeRule($def);
$Rule->process('fieldName', $data, $methods);
$this->assertFalse($Rule->isValid());
@ -114,19 +114,19 @@ class CakeRuleTest extends CakeTestCase {
*/
public function testIsRequired() {
$def = array('rule' => 'notEmpty', 'required' => true);
$Rule = new CakeRule('required', $def);
$Rule = new CakeRule($def);
$this->assertTrue($Rule->isRequired());
$def = array('rule' => 'notEmpty', 'required' => false);
$Rule = new CakeRule('required', $def);
$Rule = new CakeRule($def);
$this->assertFalse($Rule->isRequired());
$def = array('rule' => 'notEmpty', 'required' => 'create');
$Rule = new CakeRule('required', $def);
$Rule = new CakeRule($def);
$this->assertTrue($Rule->isRequired());
$def = array('rule' => 'notEmpty', 'required' => 'update');
$Rule = new CakeRule('required', $def);
$Rule = new CakeRule($def);
$this->assertFalse($Rule->isRequired());
$Rule->isUpdate(true);

View file

@ -108,18 +108,18 @@ class CakeValidationSetTest extends CakeTestModel {
public function testSetRule() {
$rules = array('notEmpty' => array('rule' => 'notEmpty', 'message' => 'Can not be empty'));
$Field = new CakeValidationSet('title', $rules);
$Rule = new CakeRule('notEmpty', $rules['notEmpty']);
$Rule = new CakeRule($rules['notEmpty']);
$this->assertEquals($Rule, $Field->getRule('notEmpty'));
$rules = array('validEmail' => array('rule' => 'email', 'message' => 'Invalid email'));
$Rule = new CakeRule('validEmail', $rules['validEmail']);
$Rule = new CakeRule($rules['validEmail']);
$Field->setRule('validEmail', $Rule);
$result = $Field->getRules();
$this->assertEquals(array('notEmpty', 'validEmail'), array_keys($result));
$rules = array('validEmail' => array('rule' => 'email', 'message' => 'Other message'));
$Rule = new CakeRule('validEmail', $rules['validEmail']);
$Rule = new CakeRule($rules['validEmail']);
$Field->setRule('validEmail', $Rule);
$result = $Field->getRules();
$this->assertEquals(array('notEmpty', 'validEmail'), array_keys($result));
@ -141,10 +141,10 @@ class CakeValidationSetTest extends CakeTestModel {
public function testSetRules() {
$rule = array('notEmpty' => array('rule' => 'notEmpty', 'message' => 'Can not be empty'));
$Field = new CakeValidationSet('title', $rule);
$RuleEmpty = new CakeRule('title', $rule['notEmpty'], 'notEmpty');
$RuleEmpty = new CakeRule($rule['notEmpty']);
$rule = array('validEmail' => array('rule' => 'email', 'message' => 'Invalid email'));
$RuleEmail = new CakeRule('email', $rule['validEmail'], 'validEmail');
$RuleEmail = new CakeRule($rule['validEmail']);
$rules = array('validEmail' => $RuleEmail);
$Field->setRules($rules, false);