Bwf/ GuideFrançais
Bwf Guide

HTML forms and CSRF

When you click Save in the Journal editor, the browser sends the field names along with what you typed. We’ll connect those names to the operation that handles the form.

Use the field names from the contract

Start with the body schema in the OpenAPI file. For each value you want to send, give the input in your BSX view the corresponding name. Now the build can check your form against the operation that receives it.

Django forms

Connect the form to a typed operation

Look at the input called title in the example. Follow the form’s POST destination to the OpenAPI operation and declare the field in its application/x-www-form-urlencoded body. With a closed schema, a misspelling in the view is a build error. Leave the method literal and build the address with url(…). When a reader submits the form, the action receives the decoded record.

<form method="post" action={url("/items/{id}")}>
  <label>Title <input name="title" required /></label>
  <button type="submit">Save</button>
</form>

A form using GET sends its named fields in the URL query.

Look for the hidden input

Sign in, open the editor and inspect the form in your browser. There’s an input called _csrf that you didn’t write in the template. Bwf added it when it rendered this local POST form. On submission it checks that value against the session identified by the cookie. A mismatch gives you 403 before your action runs.

This is the input the browser sees. Bwf fills in the value when it renders the form.

<input type="hidden" name="_csrf" value="session-generated-token">

Structure and business validation

A schema mismatch can return 422 before the action. In the action, validate the decoded values against your application’s rules. For example, the Journal’s article editor uses Draft.prepare to trim the title before checking that it is nonempty.

To render messages beside their fields, follow the registration example in Validation and error responses.

In the Journal example

Register two readers