Skip to content

Commit 1490e7e

Browse files
committed
Implemented executing a callable for determine if field can be allowed to be empty
1 parent daa5c8f commit 1490e7e

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

src/Validation/ValidationSet.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class ValidationSet implements \ArrayAccess, \IteratorAggregate, \Countable {
3939
/**
4040
* Denotes if a field is allowed to be empty
4141
*
42-
* @var bool|string
42+
* @var bool|string|callable
4343
*/
4444
protected $_allowEmpty = false;
4545

@@ -63,8 +63,9 @@ public function isPresenceRequired($validatePresent = null) {
6363
*
6464
* If no argument is passed the currently set `allowEmpty` value will be returned.
6565
*
66-
* @param bool|string $allowEmpty Valid values are true, false, 'create', 'update'
67-
* @return bool|string
66+
* @param bool|string|callable $allowEmpty Valid values are true, false,
67+
* 'create', 'update'
68+
* @return bool|string|callable
6869
*/
6970
public function isEmptyAllowed($allowEmpty = null) {
7071
if ($allowEmpty === null) {

src/Validation/Validator.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ public function errors(array $data, $newRecord = true) {
8686
continue;
8787
}
8888

89-
$canBeEmpty = $this->_canBeEmpty($field, $newRecord);
89+
$providers = $this->_providers;
90+
$context = compact('data', 'newRecord', 'field', 'providers');
91+
$canBeEmpty = $this->_canBeEmpty($field, $context);
9092
$isEmpty = $this->_fieldIsEmpty($data[$name]);
9193

9294
if (!$canBeEmpty && $isEmpty) {
@@ -314,7 +316,7 @@ public function validatePresence($field, $mode = true, $message = null) {
314316
* This is the opposite of notEmpty() which requires a field to not be empty.
315317
* By using $mode equal to 'create' or 'update', you can allow fields to be empty
316318
* when records are first created, or when they are updated.
317-
*
319+
*
318320
* ### Example:
319321
*
320322
* {{{
@@ -387,7 +389,12 @@ public function allowEmpty($field, $when = true) {
387389
public function notEmpty($field, $message = null, $when = false) {
388390
if ($when === 'create' || $when === 'update') {
389391
$when = $when === 'create' ? 'update' : 'create';
392+
} elseif (is_callable($when)) {
393+
$when = function($context) use ($when) {
394+
return !$when($context);
395+
};
390396
}
397+
391398
$this->field($field)->isEmptyAllowed($when);
392399
if ($message) {
393400
$this->_allowEmptyMessages[$field] = $message;
@@ -443,11 +450,17 @@ protected function _checkPresence($field, $newRecord) {
443450
* Returns whether the field can be left blank according to `allowEmpty`
444451
*
445452
* @param ValidationSet $field the set of rules for a field
446-
* @param bool $newRecord whether the data to be validated is new or to be updated.
453+
* @param array $context a key value list of data containing the validation context.
447454
* @return bool
448455
*/
449-
protected function _canBeEmpty($field, $newRecord) {
456+
protected function _canBeEmpty($field, $context) {
450457
$allowed = $field->isEmptyAllowed();
458+
459+
if (!is_string($allowed) && is_callable($allowed)) {
460+
return $allowed($context);
461+
}
462+
463+
$newRecord = $context['newRecord'];
451464
if (in_array($allowed, array('create', 'update'), true)) {
452465
$allowed = (
453466
($allowed === 'create' && $newRecord) ||

0 commit comments

Comments
 (0)