2. Check input before handing it off
Keep this example on the form side of the boundary. It checks for an empty email, a basic email shape, and an empty password, then exposes a translated message through a semantic region. This is convenience validation only; it does not verify identity or send either field anywhere.
The form retains what the user entered so they can correct a value without starting again. The button uses a reducer for the local validation result; there is no fake request, demo account, or signed-in screen.
{"id":"fission-tabs-1","tabs":[{"index":0,"label":"src/app.rs","value":"src-app-rs"},{"index":1,"label":"preview/lib.rs","value":"preview-lib-rs"}]}
{"id":"fission-tabs-1","index":0,"label":"src/app.rs","value":"src-app-rs"}
use fission::prelude::*;
fn validate_login_form(email: &str, password: &str) -> Result<(), &'static str> {
if email.trim().is_empty() {
Err("login.validation.email_required")
} else if !email.trim().split_once('@').is_some_and(|(name, domain)| {
!name.is_empty()
&& !domain.is_empty()
&& !domain.contains('@')
&& !email.chars().any(char::is_whitespace)
}) {
Err("login.validation.email_invalid")
} else if password.is_empty() {
Err("login.validation.password_required")
} else {
Ok(())
}
}
fission_component! {
pub LoginForm<()> {
state {
email: String = String::new(),
password: String = String::new(),
feedback: Option<String> = None,
}
}
view(login, _state, cx) {
let tokens = &cx.view.env().theme.tokens;
let email_error = login.feedback.as_deref()
.filter(|key| key.starts_with("login.validation.email")).map(|key| cx.tr(key));
let password_error = login.feedback.as_deref()
.filter(|key| *key == "login.validation.password_required").map(|key| cx.tr(key));
} =>
SemanticsRegion(
identifier: Some("harbour-supply-login".to_string()),
label: Some(cx.tr("login.semantics.form")),
child: Center(Container(Column(gap: tokens.spacing.m, children: [
Column(gap: tokens.spacing.xs, children: [
Text(cx.tr("login.brand")).weight(tokens.typography.font_weight_bold),
Text(cx.tr("login.title")).weight(tokens.typography.font_weight_semibold),
Text(cx.tr("login.detail")).color(tokens.colors.text_secondary),
]),
TextInput(
label: Some(cx.tr("login.email.label").into()),
placeholder: Some(cx.tr("login.email.placeholder").into()),
name: Some("email".into()),
keyboard_type: TextInputType::EmailAddress,
autocorrect: false,
spell_check: false,
autofill_hints: vec!["username".into()],
error_text: email_error.clone().map(Into::into),
validation_message: email_error.clone(),
required: true,
bind: login.email,
key: "login-email",
semantics_identifier: "harbour.login.email".to_string(),
),
TextInput(
label: Some(cx.tr("login.password.label").into()),
placeholder: Some(cx.tr("login.password.placeholder").into()),
name: Some("password".into()),
autocorrect: false,
spell_check: false,
autofill_hints: vec!["current-password".into()],
error_text: password_error.clone().map(Into::into),
validation_message: password_error.clone(),
required: true,
obscure_text: true,
bind: login.password,
key: "login-password",
semantics_identifier: "harbour.login.password".to_string(),
),
if let Some(message_key) = &login.feedback {
SemanticsRegion(
identifier: Some("harbour.login.feedback".to_string()),
label: Some(cx.tr(message_key)),
child: Text(cx.tr(message_key)).color(tokens.colors.text_secondary),
)
} else {
Text(cx.tr("login.preview.note")).color(tokens.colors.text_secondary)
},
Button(
child: Text(cx.tr("login.action.validate")),
key: "validate-login-form",
on_press: reduce {
login.feedback = Some(match validate_login_form(&login.email, &login.password) {
Ok(()) => "login.validation.not_connected",
Err(message_key) => message_key,
}.to_owned());
},
).semantics_identifier("harbour.login.validate-form".to_string()),
]))
.padding_all(tokens.spacing.l)
.bg(tokens.colors.surface)
.border(tokens.colors.border, 1.0)
.border_radius(tokens.radii.large)
.width_length(Length::percent(100.0))
.max_width(480.0)),
)
}
#[cfg(test)]
mod tests {
use super::validate_login_form;
#[test]
fn missing_fields_have_specific_messages() {
assert_eq!(validate_login_form("", "secret"), Err("login.validation.email_required"));
assert_eq!(validate_login_form("[email protected]", ""), Err("login.validation.password_required"));
}
#[test]
fn incomplete_addresses_do_not_pass_the_convenience_check() {
for email in ["@", "receiver@", "@example.com", "receiver@@example.com", "receiver @example.com"] {
assert_eq!(validate_login_form(email, "secret"), Err("login.validation.email_invalid"));
}
}
#[test]
fn form_validation_does_not_apply_password_policy() {
assert_eq!(validate_login_form("[email protected]", " secret "), Ok(()));
}
}
{"id":"fission-tabs-1","index":1,"label":"preview/lib.rs","value":"preview-lib-rs"}
mod app;
mod translations;
use fission::prelude::*;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(start)]
pub fn run() -> Result<(), JsValue> {
console_error_panic_hook::set_once();
WebApp::<(), _>::new(app::LoginForm::new())
.with_env(translations::create_env())
.with_title("Harbour Supply login form")
.mount("#fission-web-mount")
.run()
.map_err(|error| JsValue::from_str(&error.to_string()))
}
{"id":"fission-tabs-1"}
Check input before handing it off: Submit empty fields, then try an invalid email and a complete pair. Each result is a local validation message; the complete pair still does not sign in or leave the browser.
Show the app previewHide the app preview
Show the app preview
Hide the app preview