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

API Use validate method instead of validationResult #11515

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
20 changes: 15 additions & 5 deletions src/Forms/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
*
Expand All @@ -1254,13 +1267,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
{
// Automatically pass if there is no validator, or the clicked button is exempt
// Note: Soft support here for validation with absent request handler
Expand Down
2 changes: 1 addition & 1 deletion src/Forms/FormRequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
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
2 changes: 1 addition & 1 deletion tests/php/Forms/FieldsValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
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 @@ -37,7 +37,7 @@ public function testUploadRequiredFile()
];
$fileField->setValue($fileFieldValue);

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

/**
Expand Down Expand Up @@ -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'
);

Expand All @@ -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'
);

Expand All @@ -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'
);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/php/Forms/FormFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ public function testGetSchemaStateWithFormValidation()
$field = new FormField('MyField', 'My Field');
$validator = new RequiredFields('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 @@ -498,7 +498,7 @@ public function testValidationExtensionHooks()

// Ensure messages set via updateValidationResult() propagate through to form fields after validation
$form = new Form(null, 'TestForm', new FieldList($field), new FieldList(), new RequiredFields());
$form->validationResult();
$form->validate();
$schema = $field->getSchemaState();
$this->assertEquals(
'A test error message',
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 @@ -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',
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 @@ -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,
[],
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 @@ -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('<p class="message ' . ValidationResult::TYPE_ERROR . '">error</p>', $gridfieldOutput);

Expand All @@ -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('<p class="message ' . ValidationResult::TYPE_ERROR . '">', $gridfieldOutput);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/php/Forms/OptionsetFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
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