Skip to content

Commit

Permalink
Implemented executing a callable for determine if field can be allowe…
Browse files Browse the repository at this point in the history
…d to be empty
  • Loading branch information
lorenzo committed Jun 21, 2014
1 parent daa5c8f commit 1490e7e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
7 changes: 4 additions & 3 deletions src/Validation/ValidationSet.php
Expand Up @@ -39,7 +39,7 @@ class ValidationSet implements \ArrayAccess, \IteratorAggregate, \Countable {
/**
* Denotes if a field is allowed to be empty
*
* @var bool|string
* @var bool|string|callable
*/
protected $_allowEmpty = false;

Expand All @@ -63,8 +63,9 @@ public function isPresenceRequired($validatePresent = null) {
*
* If no argument is passed the currently set `allowEmpty` value will be returned.
*
* @param bool|string $allowEmpty Valid values are true, false, 'create', 'update'
* @return bool|string
* @param bool|string|callable $allowEmpty Valid values are true, false,
* 'create', 'update'
* @return bool|string|callable
*/
public function isEmptyAllowed($allowEmpty = null) {
if ($allowEmpty === null) {
Expand Down
21 changes: 17 additions & 4 deletions src/Validation/Validator.php
Expand Up @@ -86,7 +86,9 @@ public function errors(array $data, $newRecord = true) {
continue;
}

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

if (!$canBeEmpty && $isEmpty) {
Expand Down Expand Up @@ -314,7 +316,7 @@ public function validatePresence($field, $mode = true, $message = null) {
* This is the opposite of notEmpty() which requires a field to not be empty.
* By using $mode equal to 'create' or 'update', you can allow fields to be empty
* when records are first created, or when they are updated.
*
*
* ### Example:
*
* {{{
Expand Down Expand Up @@ -387,7 +389,12 @@ public function allowEmpty($field, $when = true) {
public function notEmpty($field, $message = null, $when = false) {
if ($when === 'create' || $when === 'update') {
$when = $when === 'create' ? 'update' : 'create';
} elseif (is_callable($when)) {
$when = function($context) use ($when) {
return !$when($context);
};
}

$this->field($field)->isEmptyAllowed($when);
if ($message) {
$this->_allowEmptyMessages[$field] = $message;
Expand Down Expand Up @@ -443,11 +450,17 @@ protected function _checkPresence($field, $newRecord) {
* Returns whether the field can be left blank according to `allowEmpty`
*
* @param ValidationSet $field the set of rules for a field
* @param bool $newRecord whether the data to be validated is new or to be updated.
* @param array $context a key value list of data containing the validation context.
* @return bool
*/
protected function _canBeEmpty($field, $newRecord) {
protected function _canBeEmpty($field, $context) {
$allowed = $field->isEmptyAllowed();

if (!is_string($allowed) && is_callable($allowed)) {
return $allowed($context);
}

$newRecord = $context['newRecord'];
if (in_array($allowed, array('create', 'update'), true)) {
$allowed = (
($allowed === 'create' && $newRecord) ||
Expand Down

0 comments on commit 1490e7e

Please sign in to comment.