Skip to content

Commit

Permalink
Merge branch '5' into 6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Dec 18, 2024
2 parents 69cb5ce + 754ac17 commit 313893b
Show file tree
Hide file tree
Showing 16 changed files with 220 additions and 51 deletions.
21 changes: 15 additions & 6 deletions src/Forms/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
use SilverStripe\View\AttributesHTML;
use SilverStripe\View\SSViewer;
use SilverStripe\Model\ModelData;
use SilverStripe\Forms\Validation\RequiredFieldsValidator;
use SilverStripe\Forms\Validation\Validator;
use SilverStripe\Dev\Deprecation;

/**
* Base class for all forms.
Expand Down Expand Up @@ -1247,6 +1247,18 @@ public function getLegend()
return $this->legend;
}

/**
* Alias of validate() for backwards compatibility.
*
* @return ValidationResult
* @deprecated 5.4.0 Use validate() instead
*/
public function validationResult()
{
Deprecation::notice('5.4.0', 'Use validate() instead');
return $this->validate();
}

/**
* Processing that occurs before a form is executed.
*
Expand All @@ -1258,13 +1270,10 @@ public function getLegend()
*
* Triggered through {@link httpSubmission()}.
*
*
* Note that CSRF protection takes place in {@link httpSubmission()},
* if it fails the form data will never reach this method.
*
* @return ValidationResult
*/
public function validationResult()
*/
public function validate(): ValidationResult
{
$result = ValidationResult::create();
// Automatically pass if the clicked button is exempt
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/FormRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ public function httpSubmission($request)
// Action handlers may throw ValidationExceptions.
try {
// Or we can use the Validator attached to the form
$result = $this->form->validationResult();
$result = $this->form->validate();
if (!$result->isValid()) {
return $this->getValidationErrorResponse($result);
}
Expand Down
14 changes: 14 additions & 0 deletions src/ORM/DataList.php
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,14 @@ private function fetchEagerLoadRelations(Query $query): void
$parentIDs = $topLevelIDs;
$parentRelationData = $query;
$chainToDate = [];
$polymorphicEncountered = false;
foreach (explode('.', $relationChain) as $relationName) {
if ($polymorphicEncountered) {
$polymorphicRelation = $chainToDate[array_key_last($chainToDate)];
throw new InvalidArgumentException(
"Invalid relation passed to eagerLoad() - $relationChain. Further nested relations are not supported after polymorphic has_one relation $polymorphicRelation."
);
}
/** @var Query|array<DataObject|EagerLoadedList> $parentRelationData */
$chainToDate[] = $relationName;
list(
Expand All @@ -1059,6 +1066,9 @@ private function fetchEagerLoadRelations(Query $query): void
$relationName,
$relationType
);
if ($relationComponent['joinClass']) {
$polymorphicEncountered = true;
}
break;
case 'belongs_to':
list($parentRelationData, $parentIDs) = $this->fetchEagerLoadBelongsTo(
Expand Down Expand Up @@ -1190,6 +1200,10 @@ private function fetchEagerLoadHasOne(
// into the has_one components - DataObject does that for us in getComponent() without any extra
// db calls.

// fetchEagerLoadRelations expects these to be flat arrays if the relation is not polymorphic
if (!$hasOneClassField) {
return [$fetchedRecords, $fetchedIDs[$relationDataClass] ?? []];
}
return [$fetchedRecords, $fetchedIDs];
}

Expand Down
6 changes: 3 additions & 3 deletions tests/php/Forms/CompositeValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public function testValidate(): void
// Put data into the form so the validator can pull it back out again
$form->loadDataFrom($data);

$result = $form->validationResult();
$result = $form->validate();
$this->assertFalse($result->isValid());
$this->assertCount(2, $result->getMessages());
}
Expand All @@ -187,14 +187,14 @@ public function testRemoveValidation(): void
// Put data into the form so the validator can pull it back out again
$form->loadDataFrom($data);

$result = $form->validationResult();
$result = $form->validate();
$this->assertFalse($result->isValid());
$this->assertCount(1, $result->getMessages());

// Make sure it doesn't fail after removing validation AND has no errors (since calling validate should
// reset errors)
$compositeValidator->removeValidation();
$result = $form->validationResult();
$result = $form->validate();
$this->assertTrue($result->isValid());
$this->assertEmpty($result->getMessages());
}
Expand Down
8 changes: 4 additions & 4 deletions tests/php/Forms/FileFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function testUploadRequiredFile()
];
$fileField->setValue($fileFieldValue);

$this->assertTrue($form->validationResult()->isValid());
$this->assertTrue($form->validate()->isValid());
}

/**
Expand Down Expand Up @@ -139,7 +139,7 @@ public function testUploadMissingRequiredFile()
$fileField->setValue($fileFieldValue);

$this->assertFalse(
$form->validationResult()->isValid(),
$form->validate()->isValid(),
'An error occurred when uploading a file, but the validator returned true'
);

Expand All @@ -148,7 +148,7 @@ public function testUploadMissingRequiredFile()
$fileField->setValue($fileFieldValue);

$this->assertFalse(
$form->validationResult()->isValid(),
$form->validate()->isValid(),
'An empty array was passed as parameter for an uploaded file, but the validator returned true'
);

Expand All @@ -157,7 +157,7 @@ public function testUploadMissingRequiredFile()
$fileField->setValue($fileFieldValue);

$this->assertFalse(
$form->validationResult()->isValid(),
$form->validate()->isValid(),
'A null value was passed as parameter for an uploaded file, but the validator returned true'
);
}
Expand Down
13 changes: 8 additions & 5 deletions tests/php/Forms/FormFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ public function testGetSchemaStateWithFormValidation()
$field = new FormField('MyField', 'My Field');
$validator = new RequiredFieldsValidator('MyField');
$form = new Form(null, 'TestForm', new FieldList($field), new FieldList(), $validator);
$form->validationResult();
$form->validate();
$schema = $field->getSchemaState();
$this->assertEquals(
'"My Field" is required',
Expand Down Expand Up @@ -566,10 +566,13 @@ public function testValidationExtensionHooks()
$result = $field->validate();
$this->assertFalse($result->isValid());

// Ensure messages set via updateValidate() propagate through after form validation
$result = $form->validationResult();
$state = $field->getSchemaState();
$this->assertEquals('A test error message', $state['message']['value']);
// Ensure messages set via updateValidationResult() propagate through to form fields after validation
$form->validate();
$schema = $field->getSchemaState();
$this->assertEquals(
'A test error message',
$schema['message']['value']
);
}

public function testValidationExtensionHooksAreCalledOnFormFieldSubclasses()
Expand Down
2 changes: 1 addition & 1 deletion tests/php/Forms/FormSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public function testGetStateWithFieldValidationErrors()
'Title' => null,
]
);
$this->assertFalse($form->validationResult()->isValid());
$this->assertFalse($form->validate()->isValid());
$formSchema = new FormSchema();
$expected = [
'id' => 'Form_TestForm',
Expand Down
2 changes: 1 addition & 1 deletion tests/php/Forms/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ public function testLookupFieldDisabledSaving()
$form->saveInto($object);
$playersIds = $object->Players()->getIDList();

$this->assertTrue($form->validationResult()->isValid());
$this->assertTrue($form->validate()->isValid());
$this->assertEquals(
$playersIds,
[],
Expand Down
4 changes: 2 additions & 2 deletions tests/php/Forms/GridField/GridFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ public function testValidationMessageInOutput()
$form = new Form(null, "testForm", $fieldList, new FieldList(), $validator);

// A form that fails validation should display the validation error in the FieldHolder output.
$form->validationResult();
$form->validate();
$gridfieldOutput = $gridField->FieldHolder();
$this->assertStringContainsString('<p class="message ' . ValidationResult::TYPE_ERROR . '">error</p>', $gridfieldOutput);

Expand All @@ -552,7 +552,7 @@ public function testValidationMessageInOutput()

// A form that passes validation should not display a validation error in the FieldHolder output.
$form->setValidator(null);
$form->validationResult();
$form->validate();
$gridfieldOutput = $gridField->FieldHolder();
$this->assertStringNotContainsString('<p class="message ' . ValidationResult::TYPE_ERROR . '">', $gridfieldOutput);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/php/Forms/OptionsetFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public function testValidation()
$field->setValue('');
$this->assertFalse($field->validate()->isValid());

// ... and should not pass "RequiredFieldsValidator" validation
$this->assertFalse($form->validationResult()->isValid());
// ... but should not pass "RequiredFields" validation
$this->assertFalse($form->validate()->isValid());

// null value should pass field-level validation...
$field->setValue(null);
Expand Down
4 changes: 2 additions & 2 deletions tests/php/Forms/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ public function testRemoveValidation()
$form->setValidator($validator); // Setup validator now that we've got our form.
$form->loadDataFrom($data); // Put data into the form so the validator can pull it back out again.

$result = $form->validationResult();
$result = $form->validate();
$this->assertFalse($result->isValid());
$this->assertCount(1, $result->getMessages());

// Make sure it doesn't fail after removing validation AND has no errors (since calling validate should reset errors).
$validator->removeValidation();
$result = $form->validationResult();
$result = $form->validate();
$this->assertTrue($result->isValid());
$this->assertEmpty($result->getMessages());
}
Expand Down
Loading

0 comments on commit 313893b

Please sign in to comment.