Skip to content

Commit

Permalink
Merge pull request #128 from creative-commoners/pulls/5/form-field-valid
Browse files Browse the repository at this point in the history
API Use updated validation API
  • Loading branch information
GuySartorelli authored Dec 2, 2024
2 parents 16a447e + 53aeaff commit 1538b6f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 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
14 changes: 9 additions & 5 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,13 +59,13 @@ public function testValidateFieldAddsErrorFromField()
$formMock = $this->getFormMock();
$formFieldMock = $this->getEditableFormFieldMock();

$result = new ValidationResult;
$result->addFieldError('MyField', 'some field message');
$formFieldMock
->getFormField() // mock
->expects($this->once())
->method('validate')
->willReturn(false);

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

$formMock
->expects($this->once())
Expand All @@ -78,11 +80,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 1538b6f

Please sign in to comment.