📎 Webclip
Let's Code: Contact Form in Phoenix -- The Schema
The post walks through the schema used to model a contact form message in a Phoenix 1.4 app. It starts from a typespec, then uses an embedded Ecto schema so the message can be validated and shaped without being stored in the database.
Reading notes#
- It models a support message with email, subject, and body fields inside a Support context.
- The module begins with
@moduledoc falsebecause it is meant to stay internal to the application. - The initial
@type tdocuments the structure, but the module still needs a schema to compile. embedded_schemais used because the message is not intended to be persisted anywhere.- The schema defines
email,subject, andbodyas string fields. - The author stubs tests first, with cases for valid fields, missing required fields, and invalid email format.
- The first changeset step uses
castto allow onlyemail,subject, andbodyfrom the input map. validate_requiredmakes all three fields mandatory.validate_formatchecks that the email matches a simple pattern.- The final result is a
Messagemodule with an embedded schema and a changeset function that casts, requires, and validates the fields.
