Commit 56b17f79 authored by Ad Schellevis's avatar Ad Schellevis

(mvc) change base validation behavior to default only validate changes

parent 7f0709df
...@@ -290,8 +290,10 @@ abstract class BaseModel ...@@ -290,8 +290,10 @@ abstract class BaseModel
/** /**
* validate full model using all fields and data in a single (1 deep) array * validate full model using all fields and data in a single (1 deep) array
* @param bool $validateFullModel validate full model or only changed fields
* @return \Phalcon\Validation\Message\Group
*/ */
public function performValidation() public function performValidation($validateFullModel = false)
{ {
// create a Phalcon validator and collect all model validations // create a Phalcon validator and collect all model validations
$validation = new \Phalcon\Validation(); $validation = new \Phalcon\Validation();
...@@ -299,12 +301,14 @@ abstract class BaseModel ...@@ -299,12 +301,14 @@ abstract class BaseModel
$all_nodes = $this->internalData->getFlatNodes(); $all_nodes = $this->internalData->getFlatNodes();
foreach ($all_nodes as $key => $node) { foreach ($all_nodes as $key => $node) {
$node_validators = $node->getValidators(); if ($validateFullModel || $node->isFieldChanged()) {
foreach ($node_validators as $item_validator) { $node_validators = $node->getValidators();
$validation->add($key, $item_validator); foreach ($node_validators as $item_validator) {
} $validation->add($key, $item_validator);
if (count($node_validators) > 0) { }
$validation_data[$key] = $node->__toString(); if (count($node_validators) > 0) {
$validation_data[$key] = $node->__toString();
}
} }
} }
...@@ -390,10 +394,11 @@ abstract class BaseModel ...@@ -390,10 +394,11 @@ abstract class BaseModel
/** /**
* validate model and serialize data to config singleton object. * validate model and serialize data to config singleton object.
* *
* @param bool $validateFullModel by default we only validate the fields we have changed
* @param bool $disable_validation skip validation, be careful to use this! * @param bool $disable_validation skip validation, be careful to use this!
* @throws \Phalcon\Validation\Exception validation errors * @throws \Phalcon\Validation\Exception validation errors
*/ */
public function serializeToConfig($disable_validation = false) public function serializeToConfig($validateFullModel = false, $disable_validation = false)
{ {
// create logger to save possible consistency issues to // create logger to save possible consistency issues to
$logger = new Syslog("config", array( $logger = new Syslog("config", array(
...@@ -404,7 +409,7 @@ abstract class BaseModel ...@@ -404,7 +409,7 @@ abstract class BaseModel
// Perform validation, collect all messages and raise exception if validation is not disabled. // Perform validation, collect all messages and raise exception if validation is not disabled.
// If for some reason the developer chooses to ignore the errors, let's at least log there something // If for some reason the developer chooses to ignore the errors, let's at least log there something
// wrong in this model. // wrong in this model.
$messages = $this->performValidation(); $messages = $this->performValidation($validateFullModel);
if ($messages->count() > 0) { if ($messages->count() > 0) {
$exception_msg = ""; $exception_msg = "";
foreach ($messages as $msg) { foreach ($messages as $msg) {
......
...@@ -60,6 +60,11 @@ abstract class BaseField ...@@ -60,6 +60,11 @@ abstract class BaseField
*/ */
protected $internalDefaultValue = ""; protected $internalDefaultValue = "";
/**
* @var null|string initial value of this field (first set)
*/
protected $internalInitialValue = null;
/** /**
* @var string direct reference to this field in the model object * @var string direct reference to this field in the model object
*/ */
...@@ -244,9 +249,26 @@ abstract class BaseField ...@@ -244,9 +249,26 @@ abstract class BaseField
*/ */
public function setValue($value) public function setValue($value)
{ {
// if first set, store initial value
if ($this->internalInitialValue == null) {
$this->internalInitialValue = $value;
}
$this->internalValue = $value; $this->internalValue = $value;
} }
/**
* check if field content has changed
* @return bool change indicator
*/
public function isFieldChanged()
{
if ($this->internalInitialValue !== $this->internalValue) {
return true;
} else {
return false;
}
}
/** /**
* Set attribute on Field object * Set attribute on Field object
* @param $key attribute key * @param $key attribute key
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment