liveBook · Manning

Web form

As explained in Rust Servers, Services, and Apps

A web form is a user-interface mechanism for collecting and submitting structured input through a web page.

A web form collects input in an HTML page and sends it to a server route. In a server-rendered Rust application, an Actix Web handler receives the request, processes the fields, and returns a page rendered with Tera. Forms support tasks such as registration, sign-in, and creating or updating records. section 7.4 section 7.0

<!doctype html>
<html>
 
<head>
    <meta charset=utf-8>
    <title>Forms with Actix & Rust</title>
</head>
 
<body>
    <h3>Enter name of tutor</h3>
    <form action=/tutors method=POST>
        <label>
            Tutor name:
            <input name="name">
        </label>
        <button type=submit>Submit form</button>
    </form>
 
    <hr>
</html>

The minimal form has an input named `name`, a submit button, and `action=/tutors method=POST`. The browser sends the entered value to that route. section 7.4

On the server, `web::Form<T>` makes submitted fields available as a Rust value. A handler can put a field into a Tera `Context`, render a response template, and return the result as HTML. For the tutor-name form, the response displays the submitted name. section 7.4

A registration form adds a fuller request-and-feedback cycle:

  • Render fields for username, password, password confirmation, tutor name, image URL, and profile. Tera variables can supply current field values and an error message. section 8.0 section 8.2
  • Use HTML checks for required fields and field lengths. After a POST to `/register`, check matching passwords in the Actix Web handler; redisplay the form with an error when validation fails. section 8.2
  • On success, process the registration data and report the result. User-management data is stored locally; passwords are stored as hashes rather than clear text. section 8.0 section 8.4
Listing 8.4 Registration template
<!doctype html>
<html>
 
<head>
    <meta charset=utf-8>
    <title>Tutor registration</title>
    <link rel="stylesheet" href="/static/tutor-styles.css">        #1
</head>
 
<body>                                                             #2
    <div class="header">
        <h1>Welcome to EzyTutor</h1>
        <p>Start your own online tutor business in a few minutes</p>
    </div>
    <div class="center">
        <h2>
            Tutor registration
        </h2>
        <form action=/register method=POST>                        #3
            <label for="userid">Enter username</label><br>
            <input type="text" name="username" value="{{current_username}}"
            autocomplete="username" minlength="6"
                maxlength="12" required><br>                       #4
            <label for="password">Enter password</label><br>
            <input type="password" name="password"
            value="{{current_password}}" autocomplete="new-password"
                minlength="8" maxlength="12" required><br>         #5
            <label for="confirm">Confirm password</label><br>
            <input type="password" name="confirmation"
            value="{{current_confirmation}}" autocomplete="new-password"
                minlength="8" maxlength="12" required><br>         #6
            <label for="userid">Enter tutor name</label><br>
            <input type="text" name="name" value="{{current_name}}"
            maxlength="12" required><br>
            <label for="imageurl">Enter tutor image url</label><br>
            <input type="text" name="imageurl" value="{{current_imageurl}}"
            maxlength="30"><br>
            <label for="profile">Brief tutor profile</label><br>
            <input type="text" name="profile" value="{{current_profile}}"
            maxlength="40"><br>
            <label for="error">                                    #7
                <p style="color:red">{{error}}</p>
            </label>
            <br>
            <button type=submit id="button1">Register</button>
        </form>
        <form action=/signinhome method=GET>
            <button type=submit id="button2">Sign in</button>
        </form>
    </div>
    <p>
    <div id="footer">
        (c)Photo by Author
    </div>
    </p>
 
</html>

The registration template shows how a POST action, named fields, Tera values, and HTML length and required attributes fit in one form. section 8.2

Sign-in failure

A sign-in form posts a username and password to `/signin`. The handler checks credentials against the local database. If authentication fails, it renders the form again with an error; on success, the user proceeds to the application’s home page. section 9.1 section 9.3

How Rust Servers, Services, and Apps covers it

How deep it goes
Builds from a minimal POST form to registration and sign-in workflows, including extraction, browser validation, server checks, and error redisplay.
Examples
Uses a tutor-name submission, a six-field registration form, and a username-and-password sign-in form.
What it assumes
Assumes Rust and Actix Web application context while introducing form submission and extraction through a minimal example.
What you can do after
Build it from scratch: implement server-rendered forms that accept submissions, validate input, and return success or error feedback.
How it connects
Links HTML forms to HTTP POST, Actix Web routes and extractors, Tera templates, validation, authentication, and database storage.

Also explained in

Compare how 12 books explain web form →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust Servers, Services, and Apps. The section links point to the parts of the book it draws on.