Skip to content

Commit

Permalink
API Use updated validation API
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Nov 11, 2024
1 parent 16a447e commit 166ec67
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/EditableSpamProtectionField.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,9 @@ public function validateField($data, $form)
$formField->setValue($data[$this->Name]);
}

$validator = $form->getValidator();
if (!$formField->validate($validator)) {
$errors = $validator->getErrors();
$result = $formField->validate();
if (!$result->isValid()) {
$errors = $result->getMessages();
$foundError = false;

// field validate implementation may not add error to validator
Expand Down
12 changes: 9 additions & 3 deletions tests/EditableSpamProtectionFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use SilverStripe\SpamProtection\EditableSpamProtectionField;
use SilverStripe\SpamProtection\Extension\FormSpamProtectionExtension;
use SilverStripe\SpamProtection\Tests\Stub\Protector;
use SilverStripe\Core\Validation\ValidationResult;

class EditableSpamProtectionFieldTest extends SapphireTest
{
Expand All @@ -39,11 +40,12 @@ public function testValidateFieldDoesntAddErrorOnSuccess()
$formMock = $this->getFormMock();
$formFieldMock = $this->getEditableFormFieldMock();

$result = new ValidationResult;
$formFieldMock
->getFormField() // mock
->expects($this->once())
->method('validate')
->willReturn(true);
->willReturn($result);

$formMock
->expects($this->never())
Expand All @@ -57,11 +59,13 @@ public function testValidateFieldAddsErrorFromField()
$formMock = $this->getFormMock();
$formFieldMock = $this->getEditableFormFieldMock();

$result = new ValidationResult;
$result->addError('fail');
$formFieldMock
->getFormField() // mock
->expects($this->once())
->method('validate')
->willReturn(false);
->willReturn($result);

$formMock->getValidator()->validationError('MyField', 'some field message', 'required');

Expand All @@ -78,11 +82,13 @@ public function testValidateFieldAddsDefaultError()
$formMock = $this->getFormMock();
$formFieldMock = $this->getEditableFormFieldMock();

$result = new ValidationResult;
$result->addError('fail');
$formFieldMock
->getFormField() // mock
->expects($this->once())
->method('validate')
->willReturn(false);
->willReturn($result);

// field doesn't set any validation errors here

Expand Down

0 comments on commit 166ec67

Please sign in to comment.