/** * Returns whether the field can be left blank according to `allowEmpty` * * @param ValidationSet $field the set of rules for a field * @param array $context a key value list of data containing the validation context. * @return bool */ 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 || $allowed === 'update' && !$newRecord; } return $allowed; }
/** * Sets the rule set for a field * * @param string $field name of the field to set * @param array|\Cake\Validation\ValidationSet $rules set of rules to apply to field * @return void */ public function offsetSet($field, $rules) { if (!$rules instanceof ValidationSet) { $set = new ValidationSet(); foreach ((array) $rules as $name => $rule) { $set->add($name, $rule); } } $this->_fields[$field] = $rules; }
/** * Test removeRule method * * @return void */ public function testRemoveRule() { $set = new ValidationSet('title', array('_validatePresent' => true, 'notEmpty' => array('rule' => 'notEmpty'), 'numeric' => array('rule' => 'numeric'), 'other' => array('rule' => array('other', 1)))); $set->remove('notEmpty'); $this->assertFalse(isset($set['notEmpty'])); $set->remove('numeric'); $this->assertFalse(isset($set['numeric'])); $set->remove('other'); $this->assertFalse(isset($set['other'])); }
/** * Test removeRule method * * @return void */ public function testRemoveRule() { $set = new ValidationSet('title', ['_validatePresent' => true, 'notBlank' => ['rule' => 'notBlank'], 'numeric' => ['rule' => 'numeric'], 'other' => ['rule' => ['other', 1]]]); $set->remove('notBlank'); $this->assertFalse(isset($set['notBlank'])); $set->remove('numeric'); $this->assertFalse(isset($set['numeric'])); $set->remove('other'); $this->assertFalse(isset($set['other'])); }
/** * 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. * @return bool */ protected function _canBeEmpty($field, $newRecord) { $allowed = $field->isEmptyAllowed(); if (in_array($allowed, array('create', 'update'), true)) { $allowed = $allowed === 'create' && $newRecord || $allowed === 'update' && !$newRecord; } return $allowed; }