Skip to content

Commit

Permalink
getErrors
Browse files Browse the repository at this point in the history
  • Loading branch information
inoas committed Sep 27, 2018
1 parent edc1934 commit 45e50d0
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 21 deletions.
6 changes: 3 additions & 3 deletions src/Datasource/EntityTrait.php
Expand Up @@ -1032,13 +1032,13 @@ public function setError($field, $errors, $overwrite = false)
* $entity->errors('salary', ['must be numeric', 'must be a positive number']);
*
* // Returns the error messages for a single field
* $entity->errors('salary');
* $entity->getErrors('salary');
*
* // Returns all error messages indexed by field name
* $entity->errors();
* $entity->getErrors();
*
* // Sets the error messages for multiple fields at once
* $entity->errors(['salary' => ['message'], 'name' => ['another message']);
* $entity->getErrors(['salary' => ['message'], 'name' => ['another message']);
* ```
*
* When used as a setter, this method will return this entity instance for method
Expand Down
21 changes: 20 additions & 1 deletion src/Form/Form.php
Expand Up @@ -251,8 +251,27 @@ public function validate(array $data)
* to `validate()` or `execute()`.
*
* @return array Last set validation errors.
* @deprecated 3.7.0 Use Form::getErrors() instead.
*/
public function errors()
{
deprecationWarning(
'Form::errors() is deprecated. ' .
'Use Form::getErrors() instead.'
);

return $this->getErrors();
}

/**
* Get the errors in the form
*
* Will return the errors from the last call
* to `validate()` or `execute()`.
*
* @return array Last set validation errors.
*/
public function getErrors()
{
return $this->_errors;
}
Expand Down Expand Up @@ -353,7 +372,7 @@ public function __debugInfo()
{
$special = [
'_schema' => $this->schema()->__debugInfo(),
'_errors' => $this->errors(),
'_errors' => $this->getErrors(),
'_validator' => $this->getValidator()->__debugInfo()
];

Expand Down
2 changes: 1 addition & 1 deletion src/Form/README.md
Expand Up @@ -55,7 +55,7 @@ You can always define additional public methods as you need as well.
```php
$contact = new ContactForm();
$success = $contact->execute($data);
$errors = $contact->errors();
$errors = $contact->getErrors();
```

## Documentation
Expand Down
2 changes: 1 addition & 1 deletion src/View/Form/FormContext.php
Expand Up @@ -209,6 +209,6 @@ public function hasError($field)
*/
public function error($field)
{
return array_values((array)Hash::get($this->_form->errors(), $field, []));
return array_values((array)Hash::get($this->_form->getErrors(), $field, []));
}
}
22 changes: 17 additions & 5 deletions tests/TestCase/Form/FormTest.php
Expand Up @@ -117,14 +117,14 @@ public function testValidate()
'body' => 'too short'
];
$this->assertFalse($form->validate($data));
$this->assertCount(2, $form->errors());
$this->assertCount(2, $form->getErrors());

$data = [
'email' => 'test@example.com',
'body' => 'Some content goes here'
];
$this->assertTrue($form->validate($data));
$this->assertCount(0, $form->errors());
$this->assertCount(0, $form->getErrors());
}

/**
Expand All @@ -140,7 +140,7 @@ public function testValidateDeprected()

$data = [];
$this->assertFalse($form->validate($data));
$this->assertCount(1, $form->errors());
$this->assertCount(1, $form->getErrors());
});
}

Expand All @@ -150,6 +150,18 @@ public function testValidateDeprected()
* @return void
*/
public function testErrors()
{
$this->deprecated(function () {
$this->testGetErrors();
});
}

/**
* Test the get errors methods.
*
* @return void
*/
public function testGetErrors()
{
$form = new Form();
$form->getValidator()
Expand All @@ -167,7 +179,7 @@ public function testErrors()
'body' => 'too short'
];
$form->validate($data);
$errors = $form->errors();
$errors = $form->getErrors();
$this->assertCount(2, $errors);
$this->assertEquals('Must be a valid email', $errors['email']['format']);
$this->assertEquals('Must be so long', $errors['body']['length']);
Expand All @@ -186,7 +198,7 @@ public function testSetErrors()
];

$form->setErrors($expected);
$this->assertSame($expected, $form->errors());
$this->assertSame($expected, $form->getErrors());
}

/**
Expand Down
20 changes: 10 additions & 10 deletions tests/TestCase/ORM/EntityTest.php
Expand Up @@ -1183,30 +1183,30 @@ public function testErrors()
{
$this->deprecated(function () {
$entity = new Entity();
$this->assertEmpty($entity->errors());
$this->assertSame($entity, $entity->errors('foo', 'bar'));
$this->assertEquals(['bar'], $entity->errors('foo'));
$this->assertEmpty($entity->getErrors());
$this->assertSame($entity, $entity->getErrors('foo', 'bar'));
$this->assertEquals(['bar'], $entity->getErrors('foo'));

$this->assertEquals([], $entity->errors('boo'));
$this->assertEquals([], $entity->getErrors('boo'));
$entity['boo'] = [
'something' => 'stupid',
'and' => false
];
$this->assertEquals([], $entity->errors('boo'));
$this->assertEquals([], $entity->getErrors('boo'));

$entity->errors('foo', 'other error');
$this->assertEquals(['bar', 'other error'], $entity->errors('foo'));
$this->assertEquals(['bar', 'other error'], $entity->getErrors('foo'));

$entity->errors('bar', ['something', 'bad']);
$this->assertEquals(['something', 'bad'], $entity->errors('bar'));
$this->assertEquals(['something', 'bad'], $entity->getErrors('bar'));

$expected = ['foo' => ['bar', 'other error'], 'bar' => ['something', 'bad']];
$this->assertEquals($expected, $entity->errors());
$this->assertEquals($expected, $entity->getErrors());

$errors = ['foo' => ['something'], 'bar' => 'else', 'baz' => ['error']];
$this->assertSame($entity, $entity->errors($errors, null, true));
$this->assertSame($entity, $entity->getErrors($errors, null, true));
$errors['bar'] = ['else'];
$this->assertEquals($errors, $entity->errors());
$this->assertEquals($errors, $entity->getErrors());
});
}

Expand Down

0 comments on commit 45e50d0

Please sign in to comment.