Skip to content

Commit

Permalink
NEW Allow skipping validation on write (#11202)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli authored Apr 18, 2024
1 parent 4be3dd5 commit dcc6863
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
15 changes: 10 additions & 5 deletions src/ORM/DataObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -1345,7 +1345,7 @@ public function populateDefaults()
*
* @return ValidationException Exception generated by this write, or null if valid
*/
protected function validateWrite()
protected function validateWrite(bool $skipValidation = false)
{
if ($this->ObsoleteClassName) {
return new ValidationException(
Expand All @@ -1354,6 +1354,10 @@ protected function validateWrite()
);
}

if ($skipValidation) {
return null;
}

// Note: Validation can only be disabled at the global level, not per-model
if (DataObject::config()->uninherited('validation_enabled')) {
$result = $this->validate();
Expand All @@ -1369,10 +1373,10 @@ protected function validateWrite()
*
* @throws ValidationException
*/
protected function preWrite()
protected function preWrite(bool $skipValidation = false)
{
// Validate this object
if ($writeException = $this->validateWrite()) {
if ($writeException = $this->validateWrite($skipValidation)) {
// Used by DODs to clean up after themselves, eg, Versioned
$this->invokeWithExtensions('onAfterSkippedWrite');
throw $writeException;
Expand Down Expand Up @@ -1560,15 +1564,16 @@ protected function writeManipulation($baseTable, $now, $isNewRecord)
* {@link getManyManyComponents()}. Default to `false`. The parameter can also be provided in
* the form of an array: `['recursive' => true, skip => ['Page'=>[1,2,3]]`. This avoid infinite
* loops when one DataObject are components of each other.
* @param boolean $skipValidation Skip validation of data
* @return int The ID of the record
* @throws ValidationException Exception that can be caught and handled by the calling function
*/
public function write($showDebug = false, $forceInsert = false, $forceWrite = false, $writeComponents = false)
public function write($showDebug = false, $forceInsert = false, $forceWrite = false, $writeComponents = false, bool $skipValidation = false)
{
$now = DBDatetime::now()->Rfc2822();

// Execute pre-write tasks
$this->preWrite();
$this->preWrite($skipValidation);

// Check if we are doing an update or an insert
$isNewRecord = !$this->isInDB() || $forceInsert;
Expand Down
7 changes: 7 additions & 0 deletions tests/php/ORM/DataObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1519,6 +1519,13 @@ public function testWritingValidDataObjectDoesntThrowException()
$this->assertTrue($validatedObject->isInDB(), "Validated object was not saved to database");
}

public function testWriteSkipValidation(): void
{
$validatedObject = new DataObjectTest\ValidatedObject();
$validatedObject->write(skipValidation: true);
$this->assertTrue($validatedObject->isInDB(), "Validated object was not saved to database");
}

public function testSubclassCreation()
{
/* Creating a new object of a subclass should set the ClassName field correctly */
Expand Down
4 changes: 2 additions & 2 deletions tests/php/ORM/DataObjectTest/TreeNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class TreeNode extends DataObject implements TestOnly
'Children' => self::class,
];

public function write($showDebug = false, $forceInsert = false, $forceWrite = false, $writeComponents = false)
public function write($showDebug = false, $forceInsert = false, $forceWrite = false, $writeComponents = false, bool $skipValidation = false)
{
// Force the component to fetch its Parent and Cycle relation so we have components to recursively write
$this->Parent;
Expand All @@ -42,7 +42,7 @@ public function write($showDebug = false, $forceInsert = false, $forceWrite = fa
// Count a write attempts
$this->WriteCount++;

return parent::write($showDebug, $forceInsert, $forceWrite, $writeComponents);
return parent::write($showDebug, $forceInsert, $forceWrite, $writeComponents, $skipValidation);
}

/**
Expand Down

0 comments on commit dcc6863

Please sign in to comment.