From a6e26a5a903ad9075721098cf4156a6589ba3fbb Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Tue, 17 Dec 2024 10:28:32 +1300 Subject: [PATCH] API Use validate method instead of validationResult --- src/Forms/Form.php | 21 ++++++++++++++++----- src/Forms/FormRequestHandler.php | 2 +- tests/php/Forms/CompositeValidatorTest.php | 6 +++--- tests/php/Forms/FieldsValidatorTest.php | 2 +- tests/php/Forms/FileFieldTest.php | 8 ++++---- tests/php/Forms/FormSchemaTest.php | 2 +- tests/php/Forms/FormTest.php | 2 +- tests/php/Forms/GridField/GridFieldTest.php | 4 ++-- tests/php/Forms/OptionsetFieldTest.php | 2 +- tests/php/Forms/ValidatorTest.php | 4 ++-- 10 files changed, 32 insertions(+), 21 deletions(-) diff --git a/src/Forms/Form.php b/src/Forms/Form.php index 69151c12145..282ca32c168 100644 --- a/src/Forms/Form.php +++ b/src/Forms/Form.php @@ -20,6 +20,7 @@ use SilverStripe\View\AttributesHTML; use SilverStripe\View\SSViewer; use SilverStripe\View\ViewableData; +use SilverStripe\Dev\Deprecation; /** * Base class for all forms. @@ -1243,6 +1244,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. * @@ -1254,14 +1267,12 @@ 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 { + // Automatically pass if there is no validator, or the clicked button is exempt // Note: Soft support here for validation with absent request handler $handler = $this->getRequestHandler(); diff --git a/src/Forms/FormRequestHandler.php b/src/Forms/FormRequestHandler.php index 1071f92aba8..69deb991bf2 100644 --- a/src/Forms/FormRequestHandler.php +++ b/src/Forms/FormRequestHandler.php @@ -217,7 +217,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); } diff --git a/tests/php/Forms/CompositeValidatorTest.php b/tests/php/Forms/CompositeValidatorTest.php index bcd79a41caf..422935a7156 100644 --- a/tests/php/Forms/CompositeValidatorTest.php +++ b/tests/php/Forms/CompositeValidatorTest.php @@ -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()); } @@ -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()); } diff --git a/tests/php/Forms/FieldsValidatorTest.php b/tests/php/Forms/FieldsValidatorTest.php index 8c958bcd348..b24005e7448 100644 --- a/tests/php/Forms/FieldsValidatorTest.php +++ b/tests/php/Forms/FieldsValidatorTest.php @@ -67,7 +67,7 @@ public function testValidation(array $values, bool $isValid) } $form = new Form(null, 'testForm', $fieldList, new FieldList([/* no actions */]), new FieldsValidator()); - $result = $form->validationResult(); + $result = $form->validate(); $this->assertSame($isValid, $result->isValid()); $messages = $result->getMessages(); if ($isValid) { diff --git a/tests/php/Forms/FileFieldTest.php b/tests/php/Forms/FileFieldTest.php index 18e397d30f5..2bf65a5e5d0 100644 --- a/tests/php/Forms/FileFieldTest.php +++ b/tests/php/Forms/FileFieldTest.php @@ -37,7 +37,7 @@ public function testUploadRequiredFile() ]; $fileField->setValue($fileFieldValue); - $this->assertTrue($form->validationResult()->isValid()); + $this->assertTrue($form->validate()->isValid()); } /** @@ -141,7 +141,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' ); @@ -150,7 +150,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' ); @@ -159,7 +159,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' ); } diff --git a/tests/php/Forms/FormSchemaTest.php b/tests/php/Forms/FormSchemaTest.php index c032f3a5372..d4f05bcc3f2 100644 --- a/tests/php/Forms/FormSchemaTest.php +++ b/tests/php/Forms/FormSchemaTest.php @@ -150,7 +150,7 @@ public function testGetStateWithFieldValidationErrors() 'Title' => null, ] ); - $this->assertFalse($form->validationResult()->isValid()); + $this->assertFalse($form->validate()->isValid()); $formSchema = new FormSchema(); $expected = [ 'id' => 'Form_TestForm', diff --git a/tests/php/Forms/FormTest.php b/tests/php/Forms/FormTest.php index 8b97293443a..05ad1f2055f 100644 --- a/tests/php/Forms/FormTest.php +++ b/tests/php/Forms/FormTest.php @@ -404,7 +404,7 @@ public function testLookupFieldDisabledSaving() $form->saveInto($object); $playersIds = $object->Players()->getIDList(); - $this->assertTrue($form->validationResult()->isValid()); + $this->assertTrue($form->validate()->isValid()); $this->assertEquals( $playersIds, [], diff --git a/tests/php/Forms/GridField/GridFieldTest.php b/tests/php/Forms/GridField/GridFieldTest.php index 642460aa5ac..859e9b54432 100644 --- a/tests/php/Forms/GridField/GridFieldTest.php +++ b/tests/php/Forms/GridField/GridFieldTest.php @@ -637,7 +637,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('

error

', $gridfieldOutput); @@ -647,7 +647,7 @@ public function testValidationMessageInOutput() // A form that passes validation should not display a validation error in the FieldHolder output. $form->setValidator(new RequiredFields()); - $form->validationResult(); + $form->validate(); $gridfieldOutput = $gridField->FieldHolder(); $this->assertStringNotContainsString('

', $gridfieldOutput); } diff --git a/tests/php/Forms/OptionsetFieldTest.php b/tests/php/Forms/OptionsetFieldTest.php index 34142ec2973..e49b2020712 100644 --- a/tests/php/Forms/OptionsetFieldTest.php +++ b/tests/php/Forms/OptionsetFieldTest.php @@ -61,7 +61,7 @@ public function testValidation() $this->assertTrue($field->validate($validator)); // ... but should not pass "RequiredFields" validation - $this->assertFalse($form->validationResult()->isValid()); + $this->assertFalse($form->validate()->isValid()); //disabled items shouldn't validate $field->setDisabledItems(['Five']); diff --git a/tests/php/Forms/ValidatorTest.php b/tests/php/Forms/ValidatorTest.php index 941094b438f..56659a07797 100644 --- a/tests/php/Forms/ValidatorTest.php +++ b/tests/php/Forms/ValidatorTest.php @@ -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()); }