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 updated validation API #128

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
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
Loading