brazier/form
Types
A form field with a value and optional validation errors.
pub type Field {
Field(name: String, value: String, errors: List(String))
}
Constructors
-
Field(name: String, value: String, errors: List(String))Arguments
- name
-
The field name (used as the HTML name attribute).
- value
-
The current value.
- errors
-
Validation errors for this field (empty if valid).
A form with multiple fields and form-level errors.
pub type Form {
Form(
fields: List(Field),
form_errors: List(String),
csrf_token: String,
)
}
Constructors
-
Form( fields: List(Field), form_errors: List(String), csrf_token: String, )Arguments
- fields
-
All fields in the form.
- form_errors
-
Form-level errors (not tied to a specific field).
- csrf_token
-
CSRF token for the form.
Values
pub fn add_error(
form: Form,
field_name: String,
error: String,
) -> Form
Add a validation error to a field.
pub fn create_csrf_store(name: String) -> CsrfStore
Create a session-bound CSRF token store backed by ETS. Tokens are one-time-use: consumed on verification.
pub fn csrf_field(form: Form) -> element.Element(msg)
Render a hidden CSRF input field.
pub fn field_has_errors(form: Form, name: String) -> Bool
Check if a specific field has errors.
pub fn generate_session_csrf(
store: CsrfStore,
session_id: String,
secret: String,
) -> String
Generate a session-bound CSRF token (256 random bits) and store it in
ETS, replacing any earlier token for the session. Returns the token.
secret is not needed for a random token; the parameter is kept so
existing callers compile.
pub fn get_value(form: Form, name: String) -> String
Get a field’s value by name, returning empty string if not found.
pub fn new(secret_key: String) -> Form
Create a new empty form with a CSRF token (256 random bits).
secret_key is not needed for a random token; the parameter is kept so
existing callers compile.
pub fn password_input(
form: Form,
name: String,
attrs: List(attribute.Attribute(msg)),
) -> element.Element(msg)
Render a password input field. Pass the listener in attrs, for example
event.on_input(PasswordChanged).
pub fn select(
form: Form,
name: String,
options: List(#(String, String)),
attrs: List(attribute.Attribute(msg)),
) -> element.Element(msg)
Render a select dropdown of #(value, label) options. Pass the listener
in attrs, for example event.on_change(Picked).
pub fn text_input(
form: Form,
name: String,
attrs: List(attribute.Attribute(msg)),
) -> element.Element(msg)
Render a text input field with error display. Pass the listener in
attrs, for example event.on_input(NameChanged).
pub fn textarea(
form: Form,
name: String,
attrs: List(attribute.Attribute(msg)),
) -> element.Element(msg)
Render a textarea field. Pass the listener in attrs.
pub fn validate(
form: Form,
validators: List(fn(Form) -> Form),
) -> Form
Validate a form by running multiple validation functions.
pub fn validate_email(form: Form, field_name: String) -> Form
Validate that a field matches a regex-like pattern (email format).
pub fn validate_matches(
form: Form,
field_name: String,
other_field: String,
error_msg: String,
) -> Form
Validate that two fields match (e.g., password confirmation).
pub fn validate_max_length(
form: Form,
field_name: String,
max: Int,
) -> Form
Validate that a field’s value has a maximum length.
pub fn validate_min_length(
form: Form,
field_name: String,
min: Int,
) -> Form
Validate that a field’s value has a minimum length.
pub fn validate_required(form: Form, field_name: String) -> Form
Validate that a field is not empty.
pub fn verify_session_csrf(
store: CsrfStore,
session_id: String,
token: String,
) -> Bool
Verify a session-bound CSRF token against the store. The comparison is constant-time, and the token is consumed atomically on success (one-time: of two concurrent requests with the same token, only one succeeds). A wrong token does not consume the stored one. Returns True if valid, False if invalid or already used.