Skip to content

Commit

Permalink
Ensuring that forms persist errors for the first request
Browse files Browse the repository at this point in the history
  • Loading branch information
Danzabar committed Sep 1, 2015
1 parent 02ea86a commit 8218ca7
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
34 changes: 33 additions & 1 deletion src/Forms/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,10 @@ public function __construct()
$this->name = base64_encode(get_called_class());
$this->errors = new Collection;

// Configure and set up form
$this->configure();

$this->setup();
$this->reinstateErrors();
}

/**
Expand Down Expand Up @@ -286,9 +287,40 @@ public function validate()
}
}

$this->saveErrors();

return $passes;
}

/**
* Saves errors in session to use on subsequent requests
*
* @return void
* @author Dan Cox
*/
public function saveErrors()
{
$this->container->get('session')
->set("form_errors_{$this->name}", $this->errors->all());
}

/**
* Adds errors from the session back into the error collection
* and removes the session entry
*
* @return void
* @author Dan Cox
*/
public function reinstateErrors()
{
$session = $this->container->get('session');

if ($session->has("form_errors_{$this->name}")) {
$this->errors->addAll($session->get("form_errors_{$this->name}"));
$session->remove("form_errors_{$this->name}");
}
}

/**
* Returns the form name
*
Expand Down
16 changes: 16 additions & 0 deletions src/Utils/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ public function add($key, $value)
return $this;
}

/**
* Adds all from an array into the collection
*
* @param Array $collection
* @return Collection
* @author Dan Cox
*/
public function addAll(Array $collection)
{
foreach ($collection as $key => $value) {
$this->add($key, $value);
}

return $this;
}

/**
* Returns the whole collection
*
Expand Down
25 changes: 25 additions & 0 deletions tests/Forms/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,5 +235,30 @@ public function test_returnAllErrors()
$this->assertContains('required', $error->get(0));
}

/**
* Test that errors on forms are persisted with each request
*
* @return void
* @author Dan Cox
*/
public function test_persistErrors()
{
// An intial form has errors
$this->DI->get('request')->make('/form', 'POST', []);
$form = new Wasp\Test\Forms\Forms\TestForm();
$form->validate();
$this->assertEquals(1, count($form->getErrors()));

// The errors should be seen in the second request
$this->DI->get('request')->make('/form', 'GET', []);
$form = new Wasp\Test\Forms\Forms\TestForm();
$this->assertEquals(1, count($form->getErrors()));

// No errors should be seen in the third request
$this->DI->get('request')->make('/form', 'GET', []);
$form = new Wasp\Test\Forms\Forms\TestForm();
$this->assertEquals(0, count($form->getErrors()));
}


} // END class FormTest extends TestCase

0 comments on commit 8218ca7

Please sign in to comment.