Skip to content

Commit

Permalink
Merge pull request #13904 from cakephp/4.x-allow-empty-null
Browse files Browse the repository at this point in the history
Make Validator::allowEmptyFor() only allow null as empty when no flags are specified.
  • Loading branch information
markstory committed Nov 21, 2019
2 parents 8ae327b + 617a0e4 commit 9b6b0f8
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
22 changes: 19 additions & 3 deletions src/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ class Validator implements ArrayAccess, IteratorAggregate, Countable
*/
public const NESTED = '_nested';

/**
* A flag for allowEmptyFor()
*
* When `null` is given, it will be recognized as empty.
*
* @var int
*/
public const EMPTY_NULL = 0;

/**
* A flag for allowEmptyFor()
*
Expand Down Expand Up @@ -202,7 +211,7 @@ public function errors(array $data, bool $newRecord = true): array

$canBeEmpty = $this->_canBeEmpty($field, $context);

$flags = static::EMPTY_ALL;
$flags = static::EMPTY_NULL;
if (isset($this->_allowEmptyFlags[$name])) {
$flags = $this->_allowEmptyFlags[$name];
}
Expand Down Expand Up @@ -705,7 +714,12 @@ public function allowEmpty($field, $when = true, $message = null)
foreach ($field as $fieldName => $setting) {
$settings = $this->_convertValidatorToArray($fieldName, $defaults, $setting);
$fieldName = array_keys($settings)[0];
$this->allowEmptyFor($fieldName, null, $settings[$fieldName]['when'], $settings[$fieldName]['message']);
$this->allowEmptyFor(
$fieldName,
static::EMPTY_ALL,
$settings[$fieldName]['when'],
$settings[$fieldName]['message']
);
}

return $this;
Expand Down Expand Up @@ -771,7 +785,8 @@ public function allowEmpty($field, $when = true, $message = null)
* ```
*
* @param string $field The name of the field.
* @param int|null $flags A bitmask of EMPTY_* flags which specify what is empty
* @param int|null $flags A bitmask of EMPTY_* flags which specify what is empty.
* If no flags/bitmask is provided only `null` will be allowed as empty value.
* @param bool|string|callable $when Indicates when the field is allowed to be empty
* Valid values are true, false, 'create', 'update'. If a callable is passed then
* the field will allowed to be empty only when the callback returns true.
Expand Down Expand Up @@ -1161,6 +1176,7 @@ public function notEmpty($field, ?string $message = null, $when = false)
$whenSetting = $this->invertWhenClause($settings[$fieldName]['when']);

$this->field($fieldName)->allowEmpty($whenSetting);
$this->_allowEmptyFlags[$fieldName] = static::EMPTY_ALL;
if ($settings[$fieldName]['message']) {
$this->_allowEmptyMessages[$fieldName] = $settings[$fieldName]['message'];
}
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/ORM/Behavior/TranslateBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1714,7 +1714,7 @@ public function testBuildMarshalMapBuildEntitiesValidationErrors()
'fields' => ['title', 'body'],
'validator' => 'custom',
]);
$validator = (new Validator())->add('title', 'notBlank', ['rule' => 'notBlank']);
$validator = (new Validator())->notEmptyString('title');
$table->setValidator('custom', $validator);
$translate = $table->behaviors()->get('Translate');

Expand Down Expand Up @@ -1797,7 +1797,7 @@ public function testBuildMarshalMapUpdateEntitiesValidationErrors()
'fields' => ['title', 'body'],
'validator' => 'custom',
]);
$validator = (new Validator())->add('title', 'notBlank', ['rule' => 'notBlank']);
$validator = (new Validator())->notEmptyString('title');
$table->setValidator('custom', $validator);
$translate = $table->behaviors()->get('Translate');

Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/ORM/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5932,7 +5932,7 @@ public function testFindOrCreateWithInvalidEntity()

$articles = $this->getTableLocator()->get('Articles');
$validator = new Validator();
$validator->notBlank('title');
$validator->notEmptyString('title');
$articles->setValidator('default', $validator);

$articles->findOrCreate(['title' => '']);
Expand Down
4 changes: 2 additions & 2 deletions tests/TestCase/Validation/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -616,13 +616,13 @@ public function testAllowEmptyFor()
$this->assertSame([], $results);

$results = $validator->errors(['title' => '']);
$this->assertSame([], $results);
$this->assertSame(['title' => ['minLength' => 'Min. length 5 chars']], $results);

$results = $validator->errors(['title' => 0]);
$this->assertSame(['title' => ['minLength' => 'Min. length 5 chars']], $results);

$results = $validator->errors(['title' => []]);
$this->assertSame([], $results);
$this->assertSame(['title' => ['minLength' => 'Min. length 5 chars']], $results);

$validator
->allowEmptyFor('name', Validator::EMPTY_STRING)
Expand Down

0 comments on commit 9b6b0f8

Please sign in to comment.