Skip to content

Commit 3d313b6

Browse files
author
Paul M. Jones
committed
Merge pull request #45 from stephen-hill/isSuccess
isSuccess
2 parents a70eb12 + 3731439 commit 3d313b6

2 files changed

Lines changed: 58 additions & 1 deletion

File tree

src/Aura/Input/Fieldset.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ class Fieldset extends AbstractInput
5757
*/
5858
protected $options;
5959

60+
/**
61+
*
62+
* Property for storing the result of the last filter() call.
63+
*
64+
* @var bool
65+
*
66+
*/
67+
protected $success;
68+
6069
/**
6170
*
6271
* Constructor.
@@ -241,6 +250,18 @@ public function init()
241250
{
242251
}
243252

253+
/**
254+
*
255+
* Did the input data pass the filter rules?
256+
*
257+
* @return null|bool
258+
*
259+
*/
260+
public function isSuccess()
261+
{
262+
return $this->success;
263+
}
264+
244265
/**
245266
*
246267
* Sets a new Field input.
@@ -326,7 +347,7 @@ public function get($name = null)
326347
*/
327348
public function filter()
328349
{
329-
return $this->filter->values($this);
350+
return $this->success = $this->filter->values($this);
330351
}
331352

332353
/**

tests/Aura/Input/FieldsetTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,42 @@ public function testFilterAll()
121121
$this->assertSame($expect, $actual);
122122
}
123123

124+
public function testIsSuccessAll()
125+
{
126+
// new fieldset
127+
$fieldset = $this->newFieldset();
128+
129+
// add fields
130+
$fieldset->setField('foo');
131+
$fieldset->setField('bar');
132+
133+
// get the filter and add a rule
134+
$filter = $fieldset->getFilter();
135+
$filter->setRule('foo', 'Foo should be alpha', function ($value) {
136+
return ctype_alpha($value);
137+
});
138+
139+
// set values on the fieldset
140+
$fieldset->fill(['foo' => 'foo_value', 'bar' => 'bar_value']);
141+
142+
// Filter has not been called, so isSuccess must return null
143+
$this->assertNull($fieldset->isSuccess());
144+
145+
// apply the filter
146+
$pass = $fieldset->filter();
147+
148+
// After calling filter, isSuccess should return a boolean
149+
// and filter() should also return a boolean
150+
$this->assertFalse($fieldset->isSuccess());
151+
$this->assertFalse($pass);
152+
153+
// Let's fix the data and test if isSuccess returns true
154+
$fieldset->fill(['foo' => 'fooValue', 'bar' => 'barValue']);
155+
$pass = $fieldset->filter();
156+
$this->assertTrue($fieldset->isSuccess());
157+
$this->assertTrue($pass);
158+
}
159+
124160
public function testSetFieldset()
125161
{
126162
// a map so the outer fieldset can create the inner fieldset

0 commit comments

Comments
 (0)