Skip to content

Commit

Permalink
Merge pull request #147 from muskie9/enhancement/requiredFields#144
Browse files Browse the repository at this point in the history
ENHANCEMENT LocatorForm required fields
  • Loading branch information
muskie9 authored Mar 17, 2017
2 parents 2dd09aa + 82e0d7c commit 6162546
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
16 changes: 15 additions & 1 deletion code/form/LocatorForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,23 @@ public function __construct(Controller $controller, $name)
->setTitle('Search')
);

$validator = RequiredFields::create();
$validator = $this->getValidator();

parent::__construct($controller, $name, $fields, $actions, $validator);
}

/**
* @return Validator
*/
public function getValidator()
{
$validator = parent::getValidator();
if (empty($validator)) {
if (!$this->validator instanceof RequiredFields) $this->setValidator(RequiredFields::create('Address'));
$validator = $this->validator;
}
$this->extend('updateRequiredFields', $validator);
return $validator;
}

}
21 changes: 21 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<phpunit bootstrap="../framework/tests/bootstrap.php" colors="true">

<testsuite name="Default">
<directory>tests</directory>
</testsuite>

<listeners>
<listener class="SS_TestListener" file="../framework/dev/TestListener.php"/>
</listeners>

<groups>
<exclude>
<group>sanitychecks</group>
</exclude>
</groups>
<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">code</directory>
</whitelist>
</filter>
</phpunit>
39 changes: 39 additions & 0 deletions tests/LocatorFormTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

class LocatorFormTest extends FunctionalTest
{

public function testLocatorFormBase()
{
$form = LocatorForm::create(Locator_Controller::create(Locator::get()->first()), 'LocatorForm');

$this->assertInstanceOf('FieldList', $form->Fields());
$this->assertInstanceOf('RequiredFields', $form->getValidator());
}

public function testUpdateRequiredFields()
{
$form = LocatorFormExtendedForm::create(Locator_Controller::create(Locator::get()->first()), 'LocatorForm');

$this->assertEquals(['Foo'], $form->getValidator()->getRequired());
}

}

class LocatorFormTestFormExtension extends Extension implements TestOnly
{
public function updateRequiredFields(RequiredFields $validator)
{
$validator->removeRequiredField('Address');
$validator->addRequiredField('Foo');
}
}

class LocatorFormExtendedForm extends LocatorForm implements TestOnly
{

private static $extensions = [
'LocatorFormTestFormExtension',
];

}

0 comments on commit 6162546

Please sign in to comment.