Skip to content

Commit b2372f6

Browse files
author
Nils Henning
committed
[TASK] extend basic building blocks guide
1 parent e0da571 commit b2372f6

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

docs/guides/100-tutorial/04_forms_edit_new_create_update_delete.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ Within our response method we have at first a transition to the person index pag
165165

166166
Let's take a closer look. Like in Rails with `form_for` you can create a form in matestack with `form`. It takes a hash as first argument for configuration. In this case we defined `new_person_form_config` to return the config hash for our form. In the config hash you can set the http request method, a path, success and failure configs and a for key, which will be explained soon. This form gets submitted as a POST request to the `persons_path`.
167167

168-
The `for` key let's us define for what this form is. In this case we pass a empty model to the form component, which will therefore submitt the form inputs wrapped by the model name following the Rails behavior and conventions.
168+
The `for` key let's us define for what this form is. In this case we pass a empty model to the form component, which will therefore submit the form inputs wrapped by the model name following the Rails behavior and conventions.
169169

170170
The 'success' key let's us define a behavior when the form was submitted successful, which means the server returned a status code of 2XX. In this case we tell the form that if successful it should follow the transition path we return in our controller action. So a page transition will happen to the detail page of our newly created model. We could also configure a failure behavior by specifying the `failure` key.
171171

docs/guides/200-basic_building_blocks/README.md

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,49 @@ Read more about the [action component](/docs/api/100-components/action.md).
479479

480480
**forms**
481481

482-
Read more about the [forms component](/docs/api/100-components/forms.md).
482+
Like in Rails with `form_for` you can create a form in matestack with `form`. It takes a hash as parameter with which you can configure your form. In the config hash you can set the HTTP request method, a path, success and failure behavior. You also need to specify a model, string or symbol for what the form is for. All form params will then be submitted nested in this namespace, following Rails behavior and conventions.
483+
484+
```ruby
485+
def response
486+
form form_config do
487+
form_input key: :name, type: :text, label: 'Name'
488+
form_input key: :age, type: :number, label: 'Name'
489+
textarea key: :description, label: 'Description'
490+
form_select key: :experience, options: ['Junior', 'Senior']
491+
form_radio key: :newsletter, options: { 'Yeah, a newsletter.': 1, 'Oh no. Not again.': 0 }, label: 'Name'
492+
form_checkbox key: :conditions, label: "I've read the terms and conditions"
493+
form_submit do
494+
button text: 'Save'
495+
end
496+
end
497+
end
498+
499+
def form_config
500+
{
501+
for: User.new
502+
path: users_path,
503+
method: :post,
504+
success: {
505+
transition: {
506+
follow_redirect: true
507+
}
508+
},
509+
failure: {
510+
emit: 'user_form_failure'
511+
}
512+
}
513+
end
514+
```
515+
516+
Inside a form you can use our form input components `form_input`, `textarea`, `form_select`, `form_radio` and `form_checkbox`. Each input component requires a `:key` which represents the params name as which this inputs value get's submitted. It is also possible to specify `:label` in order to create labels for the input on the fly. Some form components can take additional `:options` as a array or hash, which will render a the passed options. For inputs with possible multiple values, like checkboxes or multisecelects, the selected values are submitted in an array, following again Rails behavior. To learn more about the details of each input component take a look at the [form components api](/docs/api/100-components/form.md)
517+
518+
Wrap a button or any markup which should submit the form when clicked in `form_submit`.
519+
520+
Each form requires a few keys for configuration: `:for`, `:path`, `:method`. Like said above, `:for` can reference a active record object or a string/symbol which will be used to nest params in it. `:path` specifies the target path, the form is submitted to with the configured request method in `:method`.
521+
522+
Forms will be submitted asynchronously and in case of errors dynamically extended to show errors belonging to inputs fields, but it is possible to set custom form behavior in success or failure cases. You could transition to another page, follow the redirect from the server as a transition or normal redirect, or emit events to leverage the above mentioned event hub.
523+
524+
To learn more, check out the [complete form documentation](/docs/api/100-components/form.md).
483525

484526
**async**
485527

0 commit comments

Comments
 (0)