Skip to content

Commit

Permalink
add Exception section to README
Browse files Browse the repository at this point in the history
  • Loading branch information
koriym committed Sep 4, 2015
1 parent 578b11c commit b96cf8b
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 2 deletions.
49 changes: 48 additions & 1 deletion README.JA.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,54 @@ class MyForm extends AbstractAuraForm
セキュリティレベルを高めるためにはユーザーの認証を含んだカスタムCsrfクラスを作成してフォームクラスにセットします。
詳しくはAura.Inputの[Applying CSRF Protections](https://github.com/auraphp/Aura.Input#applying-csrf-protections)をご覧ください。

## Validation Exception

以下のように `Ray\WebFormModule\FormVndErrorModule`をインストールするとフォームのバリデーションが失敗したときに`Ray\WebFormModule\Exception\ValidationException`例外が投げられるよになります。

```php
use Ray\Di\AbstractModule;

class FakeVndErrorModule extends AbstractModule
{
protected function configure()
{
$this->install(new WebFormModule);
$this->override(new FormVndErrorModule);
}
```

キャッチした例外の`error`プロパティを`echo`すると[application/vnd.error+json](https://tools.ietf.org/html/rfc6906)メディアタイプの表現が出力されます。

```php
http_response_code(400);
echo $e->error;

//{
// "message": "Validation failed",
// "path": "/path/to/error",
// "validation_messages": {
// "name": [
// "Name must be alphabetic only."
// ]
// }
//}
```

`@VndError`アノテーションで`vnd.error+json`に必要な情報を加えることができます。

```php
/**
* @FormValidation(form="contactForm")
* @VndError(
* message="foo validation failed",
* logref="a1000", path="/path/to/error",
* href={"_self"="/path/to/error", "help"="/path/to/help"}
* )
*/
```

このオプションのモジュールはAPIアプリケーションの時に有用です。

## Demo

$ php -S docs/demo/1.csrf/web.php
Expand All @@ -149,4 +197,3 @@ class MyForm extends AbstractAuraForm

* PHP 5.5+
* hhvm

48 changes: 47 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,53 @@ class MyController
```
You can provide your custom `AntiCsrf` class. See more detail at [Aura.Input](https://github.com/auraphp/Aura.Input#applying-csrf-protections)

## Validation Exception

When we install `Ray\WebFormModule\FormVndErrorModule` as following,

```php
use Ray\Di\AbstractModule;

class FakeVndErrorModule extends AbstractModule
{
protected function configure()
{
$this->install(new WebFormModule);
$this->override(new FormVndErrorModule);
}
```
A `Ray\WebFormModule\Exception\ValidationException` will be thrown.
We can echo catched exception to get [application/vnd.error+json](https://tools.ietf.org/html/rfc6906) media type.

```php
echo $e->error;

//{
// "message": "Validation failed",
// "path": "/path/to/error",
// "validation_messages": {
// "name": [
// "Name must be alphabetic only."
// ]
// }
//}
```

More detail for `vnd.error+json`can be add with `@VndError` annotation.

```php
/**
* @FormValidation(form="contactForm")
* @VndError(
* message="foo validation failed",
* logref="a1000", path="/path/to/error",
* href={"_self"="/path/to/error", "help"="/path/to/help"}
* )
*/
```

This optional module is handy for API application.

### Demo

$ php -S docs/demo/1.csrf/web.php
Expand All @@ -166,4 +213,3 @@ You can provide your custom `AntiCsrf` class. See more detail at [Aura.Input](ht

* PHP 5.5+
* hhvm

0 comments on commit b96cf8b

Please sign in to comment.