Skip to content

Commit

Permalink
another batch of revese changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Graziel committed May 11, 2016
1 parent 9e96ecc commit 27f52f2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 64 deletions.
9 changes: 3 additions & 6 deletions src/Validation/Validator.php
Expand Up @@ -158,7 +158,6 @@ public function field($name, ValidationSet $set = null)
$set = $set ?: new ValidationSet;
$this->_fields[$name] = $set;
}

return $this->_fields[$name];
}

Expand Down Expand Up @@ -318,6 +317,7 @@ public function add($field, $name, $rule = [])
foreach ($rules as $name => $rule) {
$field->add($name, $rule);
}

return $this;
}

Expand Down Expand Up @@ -349,7 +349,6 @@ public function addNested($field, Validator $validator)
$validator->provider($provider, $this->provider($provider));
}
$errors = $validator->errors($value, $context['newRecord']);

return empty($errors) ? true : $errors;
}]);
return $this;
Expand Down Expand Up @@ -392,7 +391,6 @@ public function addNestedMany($field, Validator $validator)
$errors[$i] = $check;
}
}

return empty($errors) ? true : $errors;
}]);
return $this;
Expand Down Expand Up @@ -1444,7 +1442,6 @@ public function multipleOptions($field, array $options = [], $message = null, $w
public function hasAtLeast($field, $count, $message = null, $when = null)
{
$extra = array_filter(['on' => $when, 'message' => $message]);

return $this->add($field, 'hasAtLeast', $extra + [
'rule' => function ($value) use ($count) {
if (is_array($value) && isset($value['_ids'])) {
Expand All @@ -1470,7 +1467,6 @@ public function hasAtLeast($field, $count, $message = null, $when = null)
public function hasAtMost($field, $count, $message = null, $when = null)
{
$extra = array_filter(['on' => $when, 'message' => $message]);

return $this->add($field, 'hasAtMost', $extra + [
'rule' => function ($value) use ($count) {
if (is_array($value) && isset($value['_ids'])) {
Expand Down Expand Up @@ -1536,6 +1532,7 @@ protected function _checkPresence($field, $context)
($required === 'update' && $newRecord)
);
}

return !$required;
}

Expand All @@ -1561,6 +1558,7 @@ protected function _canBeEmpty($field, $context)
($allowed === 'update' && !$newRecord)
);
}

return $allowed;
}

Expand All @@ -1578,7 +1576,6 @@ protected function _fieldIsEmpty($data)
$isArray = is_array($data);
if ($isArray && (isset($data['year']) || isset($data['hour']))) {
$value = implode('', $data);

return strlen($value) === 0;
}
if ($isArray && isset($data['name'], $data['type'], $data['tmp_name'], $data['error'])) {
Expand Down
100 changes: 42 additions & 58 deletions tests/TestCase/Validation/ValidatorTest.php
Expand Up @@ -203,18 +203,15 @@ public function testRequirePresenceAsArray()
$this->assertTrue($validator->field('title')->isPresenceRequired());
$this->assertTrue($validator->field('created')->isPresenceRequired());

$validator->requirePresence(
[
'title' => [
'mode' => false
],
'content' => [
'mode' => 'update'
],
'subject'
$validator->requirePresence([
'title' => [
'mode' => false
],
true
);
'content' => [
'mode' => 'update'
],
'subject'
], true);
$this->assertFalse($validator->field('title')->isPresenceRequired());
$this->assertEquals('update', $validator->field('content')->isPresenceRequired());
$this->assertTrue($validator->field('subject')->isPresenceRequired());
Expand Down Expand Up @@ -561,22 +558,19 @@ public function testAllowEmptyAsArray()
{
$validator = new Validator;

$validator->allowEmpty(
[
'title',
'subject',
'posted_at' => [
'when' => false
],
'updated_at' => [
'when' => true
],
'show_at' => [
'when' => 'update'
]
$validator->allowEmpty([
'title',
'subject',
'posted_at' => [
'when' => false
],
'create'
);
'updated_at' => [
'when' => true
],
'show_at' => [
'when' => 'update'
]
], 'create');
$this->assertEquals('create', $validator->field('title')->isEmptyAllowed());
$this->assertEquals('create', $validator->field('subject')->isEmptyAllowed());
$this->assertFalse($validator->field('posted_at')->isEmptyAllowed());
Expand Down Expand Up @@ -623,26 +617,22 @@ public function testNotEmptyAsArray()
$this->assertFalse($validator->field('title')->isEmptyAllowed());
$this->assertFalse($validator->field('created')->isEmptyAllowed());

$validator->notEmpty(
[
'title' => [
'when' => false
],
'content' => [
'when' => 'update'
],
'posted_at' => [
'when' => 'create'
],
'show_at' => [
'message' => 'Show date cannot be empty',
'when' => false
],
'subject'
$validator->notEmpty([
'title' => [
'when' => false
],
'Not empty',
true
);
'content' => [
'when' => 'update'
],
'posted_at' => [
'when' => 'create'
],
'show_at' => [
'message' => 'Show date cannot be empty',
'when' => false
],
'subject'
], 'Not empty', true);

$this->assertFalse($validator->field('title')->isEmptyAllowed());
$this->assertTrue($validator->isEmptyAllowed('content', true));
Expand All @@ -651,16 +641,13 @@ public function testNotEmptyAsArray()
$this->assertTrue($validator->isEmptyAllowed('posted_at', false));
$this->assertTrue($validator->field('subject')->isEmptyAllowed());

$errors = $validator->errors(
[
'title' => '',
'content' => '',
'posted_at' => null,
'show_at' => null,
'subject' => ''
],
false
);
$errors = $validator->errors([
'title' => '',
'content' => '',
'posted_at' => null,
'show_at' => null,
'subject' => ''
], false);

$expected = [
'title' => ['_empty' => 'Not empty'],
Expand Down Expand Up @@ -948,7 +935,6 @@ public function testErrorsFromCustomProvider()
'field' => 'title'
];
$this->assertEquals($expected, $context);

return "That ain't cool, yo";
}));

Expand Down Expand Up @@ -994,7 +980,6 @@ public function testMethodsWithExtraArguments()
'field' => 'title'
];
$this->assertEquals($expected, $context);

return "That ain't cool, yo";
}));
$validator->provider('thing', $thing);
Expand All @@ -1016,7 +1001,6 @@ public function testUsingClosureAsRule()
$validator->add('name', 'myRule', [
'rule' => function ($data, $provider) {
$this->assertEquals('foo', $data);

return 'You fail';
}
]);
Expand Down

0 comments on commit 27f52f2

Please sign in to comment.