Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NEW Allow skipping validation on write #11202

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
GuySartorelli marked this conversation as resolved.
Show resolved Hide resolved
{
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)) {
GuySartorelli marked this conversation as resolved.
Show resolved Hide resolved
// 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
Loading