Skip to content

Commit

Permalink
Merge pull request #56 from harikt/issue-55
Browse files Browse the repository at this point in the history
Add tests and fix issue #55 .
  • Loading branch information
harikt committed Oct 22, 2016
2 parents 1a01b43 + ff82142 commit 2a3b42b
Show file tree
Hide file tree
Showing 9 changed files with 249 additions and 29 deletions.
46 changes: 44 additions & 2 deletions docs/index.md
Expand Up @@ -432,9 +432,51 @@ class ContactForm extends Form

### Creating Reusable Fieldsets

TBD.
Consider we want to address to different forms.
Address have multiple fields, and it is not necessary to copy and paste the same.
With `Fieldsets` we can create a `AddressFieldset` as below

```php
<?php
use Aura\Input\Fieldset;

class AddressFieldset extends Fieldset
{
public function init()
{
$this->setField('street');
$this->setField('city');
$this->setField('state');
$this->setField('zip');
}
}
```

and map it to the `Builder`.

```
$builder = new Builder([
'address' => function () {
return new AddressFieldset(
new Builder,
new Filter
);
},
// other fieldset if any
]);
```

Now in our `ContactForm` `init()` method we can use `$this->setFieldset('address');`

> NB: Don't forget to pass the builder to the Form.
```
$form = new ContactForm($builder, new Filter);
```

### Using Fieldset Collections

TBD.
Consider we need multiple address like shipping and billing.
Now we can reuse the same `AddressFieldset` in the form by calling `$this->setCollection('address', 'address');`

Consider looking into the [examples](https://github.com/auraphp/Aura.Input/tree/3.x/tests/Example) for more information.
8 changes: 4 additions & 4 deletions src/Collection.php
Expand Up @@ -96,16 +96,16 @@ public function filter()
*
* Returns the failures for the fieldset filters.
*
* @return \ArrayObject
* @return array
*
*/
public function getFailures()
{
$messages = [];
$failures = [];
foreach ($this->fieldsets as $key => $fieldset) {
$messages[$key] = $fieldset->getFailures();
$failures[$key] = $fieldset->getFailures();
}
return new ArrayObject($messages);
return $failures;
}

/**
Expand Down
65 changes: 50 additions & 15 deletions src/Fieldset.php
Expand Up @@ -10,6 +10,7 @@
*/
namespace Aura\Input;

use ArrayObject;
use Aura\Filter_Interface\FilterInterface;

/**
Expand Down Expand Up @@ -68,6 +69,15 @@ class Fieldset extends AbstractInput
*/
protected $success;

/**
*
* Failures in the fieldset.
*
* @var ArrayObject|null
*
*/
protected $failures;

/**
*
* Constructor.
Expand Down Expand Up @@ -234,6 +244,7 @@ public function getOptions()
*/
public function fill(array $data)
{
$this->success = null;
foreach ($this->inputs as $key => $input) {
if (array_key_exists($key, $data)) {
$input->fill($data[$key]);
Expand Down Expand Up @@ -347,22 +358,46 @@ public function get($name = null)
* @return bool True if all the filter rules pass, false if not.
*
*/
public function filter()
{
return $this->success = $this->filter->apply($this);
}
public function filter()
{
$success = $this->filter->apply($this);
if (! $success) {
$this->success = $success;
$this->failures = $this->filter->getFailures();
}

/**
*
* Returns the failures.
*
* @return \ArrayObject
*
*/
public function getFailures()
{
return $this->filter->getFailures();
}
// Iterate on fieldset or collection and get failures
foreach ($this->inputs as $name => $input) {
if ($input instanceof Fieldset || $input instanceof Collection) {
if (! $input->filter()) {
$this->success = false;
if (! $this->failures instanceof ArrayObject) {
$this->failures = new ArrayObject();
}
$this->failures->offsetSet($name, $input->getFailures());
}
}
}

// If there is no failure, then it is a success on all Filters
if (! isset($this->success) && $success) {
$this->success = true;
}

return $this->success;
}

/**
*
* Returns the failures.
*
* @return ArrayObject
*
*/
public function getFailures()
{
return $this->failures;
}

/**
*
Expand Down
18 changes: 18 additions & 0 deletions src/Filter.php
Expand Up @@ -40,6 +40,24 @@ class Filter implements FilterInterface
*/
protected $messages = [];

/**
* Initialize filters
*/
public function __construct()
{
$this->init();
}

/**
*
* Does nothing
*
*/
protected function init()
{
# code...
}

/**
*
* Sets a filter rule on a field.
Expand Down
12 changes: 6 additions & 6 deletions tests/CollectionTest.php
Expand Up @@ -51,30 +51,30 @@ public function testFilterAndGetMessages()
$this->assertFalse($actual);

$actual = $collection->getFailures();
$expect = new ArrayObject([
0 => new ArrayObject([]),
$expect = [
0 => null,
1 => new ArrayObject([
'foo' => [
'Use alpha only!',
],
]),
2 => new ArrayObject([]),
2 => null,
3 => new ArrayObject([
'foo' => [
'Use alpha only!',
],
]),
]);
];

$this->assertEquals($expect, $actual);

$actual = $collection->getFailures()[1];
$actual = $collection->getFailures();
$expect = new ArrayObject([
'foo' => [
'Use alpha only!',
],
]);
$this->assertEquals($expect, $actual);
$this->assertEquals($expect, $actual[1]);
}

public function testArrayAccessCount()
Expand Down
34 changes: 34 additions & 0 deletions tests/Example/ContactFilter.php
@@ -0,0 +1,34 @@
<?php
namespace Aura\Input\Example;

use Aura\Input\Filter;

class ContactFilter extends Filter
{
protected function init()
{
$this->setRule(
'first_name',
'First name must be alphabetic only.',
function ($value) {
return ctype_alpha($value);
}
);

$this->setRule(
'last_name',
'Last name must be alphabetic only.',
function ($value) {
return ctype_alpha($value);
}
);

$this->setRule(
'email',
'Email not valid.',
function ($value) {
return filter_var($value, FILTER_VALIDATE_EMAIL);
}
);
}
}
2 changes: 0 additions & 2 deletions tests/Example/ExampleTest.php
Expand Up @@ -103,7 +103,5 @@ public function testAll()
'value' => '234-567-8901',
];
$this->assertSame($expect, $actual);


}
}
31 changes: 31 additions & 0 deletions tests/Example/PhoneFilter.php
@@ -0,0 +1,31 @@
<?php
namespace Aura\Input\Example;

use Aura\Input\Filter;

class PhoneFilter extends Filter
{
protected function init()
{
$this->setRule(
'type',
'Type is not valid.',
function ($value) {
$phoneTypes = [
'mobile',
'home',
'fax'
];
return in_array($value, $phoneTypes);
}
);

$this->setRule(
'number',
'Not a valid phone number.',
function ($value) {
return preg_match('/^[0-9-]+$/', $value);
}
);
}
}
62 changes: 62 additions & 0 deletions tests/FormTest.php
@@ -1,6 +1,12 @@
<?php
namespace Aura\Input;

use Aura\Input\Example\AddressFieldset;
use Aura\Input\Example\ContactFilter;
use Aura\Input\Example\ContactForm;
use Aura\Input\Example\PhoneFieldset;
use Aura\Input\Example\PhoneFilter;

class FormTest extends \PHPUnit_Framework_TestCase
{
public function testAddCsrf()
Expand Down Expand Up @@ -84,4 +90,60 @@ public function testGetIterator()
$keys = array_keys($iterator->getArrayCopy());
$this->assertSame(['foo', 'bar'], $keys);
}

public function testGetFailures()
{
$builder = new Builder([
'address' => function () {
return new AddressFieldset(
new Builder,
new Filter
);
},
'phone' => function () {
return new PhoneFieldset(
new Builder,
new PhoneFilter
);
},
]);

$filter = new ContactFilter();

$form = new ContactForm($builder, $filter);
// fill the form with data
$form->fill([
'first_name' => '',
'last_name' => 'KT',
'no_such_field' => 'nonesuch',
'email' => 'someon@example.com',
'website' => 'http://boshag.example.com',
'address' => [
'street' => '123 Main',
'city' => 'Beverly Hills',
'state' => 'CA',
'zip' => '90210',
],
'phone_numbers' => [
0 => [
'type' => 'mobile',
'number' => '123-456-7890',
],
1 => [
'type' => 'home',
'number' => '234-567-8901',
],
2 => [
'type' => 'fax',
'number' => '5345a34-8988',
],
],
]);

$this->assertFalse($form->filter());
$failures = $form->getFailures();
$this->assertSame(2, $failures->count());
$this->assertSame("First name must be alphabetic only.", $failures->offsetGet('first_name')[0]);
$this->assertSame("Not a valid phone number.", $failures['phone_numbers'][2]['number'][0]);
}
}

0 comments on commit 2a3b42b

Please sign in to comment.