diff --git a/example/main.dart b/example/main.dart index 3d747d7a..8941432c 100644 --- a/example/main.dart +++ b/example/main.dart @@ -12,11 +12,38 @@ main() async { var app = Angel(logger: Logger('angel_validate')); var http = AngelHttp(app); + var todos = []; + /// We can combine fields into a form; this is most + /// useful when we immediately deserialize the form into + /// something else. var todoForm = Form(fields: [ - + TextField('text'), + BoolField('is_complete'), ]); + /// We can directly use a `Form` to deserialize a + /// request body into a `Map`. + /// + /// By calling `deserialize` or `decode`, we can populate + /// concrete Dart objects. + app.post('/', (req, res) async { + var todo = await todoForm.deserialize(req, Todo.fromMap); + todos.add(todo); + await res.redirect('/'); + }); + + /// You can also use `Field`s to read directly from the + /// request, without `as` casts. + /// + /// In this handler, we read the value of `name` from the query. + app.get('/hello', (req, res) async { + var nameField = TextField('name'); + var name = await nameField.getValue(req, query: true); + return 'Hello, $name!'; + }); + + /// Simple page displaying a form and some state. app.get('/', (req, res) { res ..contentType = MediaType('text', 'html') @@ -24,6 +51,12 @@ main() async { +

angel_validate

+
@@ -31,7 +64,7 @@ main() async {
- +
@@ -48,6 +81,7 @@ main() async { }; await http.startServer('127.0.0.1', 3000); + print('Listening at ${http.uri}'); } class Todo { diff --git a/lib/src/common_fields.dart b/lib/src/common_fields.dart index 867466bf..b9c47c27 100644 --- a/lib/src/common_fields.dart +++ b/lib/src/common_fields.dart @@ -5,11 +5,17 @@ import 'form_renderer.dart'; /// A [Field] that accepts plain text. class TextField extends Field { - /// If `true`, then renderers will produce a `