Skip to content

Commit

Permalink
Adding method to fetch all errors from fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Danzabar committed Jul 7, 2015
1 parent f0152af commit 69d6a90
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Forms/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,4 +329,15 @@ public function createBox(Array $extras)
return $this->inputString($type, $extras, $this->values);
}

/**
* Returns the ID of the field
*
* @return String
* @author Dan Cox
*/
public function getID()
{
return $this->id;
}

} // END class Field
20 changes: 20 additions & 0 deletions src/Forms/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ class Form
*/
protected $url;

/**
* A collection of validation errors assigned to fields
*
* @var Wasp\Utils\Collection
*/
protected $errors;

/**
* The CSRF token
*
Expand Down Expand Up @@ -100,6 +107,7 @@ class Form
public function __construct()
{
$this->container = DI::getContainer();
$this->errors = new Collection;

$this->setup();
$this->configure();
Expand Down Expand Up @@ -260,6 +268,7 @@ public function validate()
{
if (!$field->validate())
{
$this->errors->add($field->getID(), $field->errors());
$passes = false;
}
}
Expand Down Expand Up @@ -335,6 +344,17 @@ public function getToken()
return $this->token;
}

/**
* Returns a collection of all errors from all fields
*
* @return Wasp\Utils\Collection
* @author Dan Cox
*/
public function getErrors()
{
return $this->errors;
}

/**
* Returns a closing form element
*
Expand Down
20 changes: 20 additions & 0 deletions tests/Forms/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,5 +213,25 @@ public function test_csrfFailure()
$form->validate();
}

/**
* Test getting errors from form level rather than individual fields
*
* @return void
* @author Dan Cox
*/
public function test_returnAllErrors()
{
$this->DI->get('request')->make('/form', 'POST', []);

$form = new Wasp\Test\Forms\Forms\TestForm();
$form->validate();

$errors = $form->getErrors();
$error = $errors->get('password');

$this->assertEquals(1, count($errors));
$this->assertContains('required', $error->get(0));
}


} // END class FormTest extends TestCase

0 comments on commit 69d6a90

Please sign in to comment.