pub struct FormWizard { /* private fields */ }Expand description
FormWizard manages multi-step forms
Implementations§
Source§impl FormWizard
impl FormWizard
Sourcepub fn new(_prefix: String) -> Self
pub fn new(_prefix: String) -> Self
Create a new form wizard
§Examples
use reinhardt_forms::FormWizard;
let wizard = FormWizard::new("wizard".to_string());
assert_eq!(wizard.current_step(), 0);
assert!(wizard.steps().is_empty());Sourcepub fn steps(&self) -> &Vec<WizardStep>
pub fn steps(&self) -> &Vec<WizardStep>
Returns a reference to all wizard steps.
Sourcepub fn add_step(&mut self, step: WizardStep)
pub fn add_step(&mut self, step: WizardStep)
Add a step to the wizard
§Examples
use reinhardt_forms::{FormWizard, WizardStep, Form};
let mut wizard = FormWizard::new("wizard".to_string());
let form = Form::new();
let step = WizardStep::new("step1".to_string(), form);
wizard.add_step(step);
assert_eq!(wizard.steps().len(), 1);Sourcepub fn current_step(&self) -> usize
pub fn current_step(&self) -> usize
Returns the zero-based index of the current step.
Sourcepub fn current_step_name(&self) -> Option<&str>
pub fn current_step_name(&self) -> Option<&str>
Returns the name of the current step, or None if there are no steps.
Sourcepub fn current_form(&self) -> Option<&Form>
pub fn current_form(&self) -> Option<&Form>
Returns a reference to the current step’s form, or None if there are no steps.
Sourcepub fn current_form_mut(&mut self) -> Option<&mut Form>
pub fn current_form_mut(&mut self) -> Option<&mut Form>
Returns a mutable reference to the current step’s form, or None if there are no steps.
Sourcepub fn total_steps(&self) -> usize
pub fn total_steps(&self) -> usize
Returns the total number of steps in the wizard.
Sourcepub fn is_first_step(&self) -> bool
pub fn is_first_step(&self) -> bool
Returns true if the wizard is on the first step.
Sourcepub fn is_last_step(&self) -> bool
pub fn is_last_step(&self) -> bool
Returns true if the wizard is on the last step.
Sourcepub fn next_step(&mut self) -> Result<(), String>
pub fn next_step(&mut self) -> Result<(), String>
Move to the next available step
§Examples
use reinhardt_forms::{FormWizard, WizardStep, Form};
let mut wizard = FormWizard::new("wizard".to_string());
let form1 = Form::new();
let form2 = Form::new();
wizard.add_step(WizardStep::new("step1".to_string(), form1));
wizard.add_step(WizardStep::new("step2".to_string(), form2));
let result = wizard.next_step();
assert!(result.is_ok());
assert_eq!(wizard.current_step(), 1);Sourcepub fn previous_step(&mut self) -> Result<(), String>
pub fn previous_step(&mut self) -> Result<(), String>
Move to the previous step
§Examples
use reinhardt_forms::{FormWizard, WizardStep, Form};
let mut wizard = FormWizard::new("wizard".to_string());
let form1 = Form::new();
let form2 = Form::new();
wizard.add_step(WizardStep::new("step1".to_string(), form1));
wizard.add_step(WizardStep::new("step2".to_string(), form2));
wizard.next_step().unwrap(); // Move to step 2
let result = wizard.previous_step();
assert!(result.is_ok());
assert_eq!(wizard.current_step(), 0);Sourcepub fn goto_step(&mut self, name: &str) -> Result<(), String>
pub fn goto_step(&mut self, name: &str) -> Result<(), String>
Go to a specific step by name.
Forward navigation (to a step after the current one) requires that all previous steps have been completed (i.e., their data has been saved to the session). This prevents attackers from skipping required validation steps such as terms acceptance or payment details.
Backward navigation (to a step before the current one) is always allowed, enabling users to review and edit previous answers.
§Examples
use reinhardt_forms::{FormWizard, WizardStep, Form};
use std::collections::HashMap;
use serde_json::json;
let mut wizard = FormWizard::new("wizard".to_string());
let form1 = Form::new();
let form2 = Form::new();
let form3 = Form::new();
wizard.add_step(WizardStep::new("step1".to_string(), form1));
wizard.add_step(WizardStep::new("step2".to_string(), form2));
wizard.add_step(WizardStep::new("step3".to_string(), form3));
// Forward navigation without completing previous steps is rejected
assert!(wizard.goto_step("step3").is_err());
// Complete step1 and step2 first
let mut data = HashMap::new();
data.insert("field".to_string(), json!("value"));
wizard.save_step_data(data.clone()).unwrap();
wizard.next_step().unwrap();
wizard.save_step_data(data).unwrap();
// Now forward navigation to step3 succeeds
assert!(wizard.goto_step("step3").is_ok());Sourcepub fn save_step_data(
&mut self,
data: HashMap<String, Value>,
) -> Result<(), FormError>
pub fn save_step_data( &mut self, data: HashMap<String, Value>, ) -> Result<(), FormError>
Save data for the current step
§Examples
use reinhardt_forms::{FormWizard, WizardStep, Form};
use std::collections::HashMap;
use serde_json::json;
let mut wizard = FormWizard::new("wizard".to_string());
let form = Form::new();
wizard.add_step(WizardStep::new("step1".to_string(), form));
let mut data = HashMap::new();
data.insert("name".to_string(), json!("John"));
let result = wizard.save_step_data(data);
assert!(result.is_ok());Sourcepub fn get_all_data(&self) -> &HashMap<String, HashMap<String, Value>>
pub fn get_all_data(&self) -> &HashMap<String, HashMap<String, Value>>
Returns all session data collected across all completed steps.
Sourcepub fn get_step_data(&self, step_name: &str) -> Option<&HashMap<String, Value>>
pub fn get_step_data(&self, step_name: &str) -> Option<&HashMap<String, Value>>
Returns the saved data for a specific step, or None if that step has no data.
Sourcepub fn clear_data(&mut self)
pub fn clear_data(&mut self)
Clears all session data and resets the wizard to the first step.
Sourcepub fn process_step(
&mut self,
data: HashMap<String, Value>,
) -> Result<bool, FormError>
pub fn process_step( &mut self, data: HashMap<String, Value>, ) -> Result<bool, FormError>
Process current step and move to next if valid
§Examples
use reinhardt_forms::{FormWizard, WizardStep, Form};
use std::collections::HashMap;
use serde_json::json;
let mut wizard = FormWizard::new("wizard".to_string());
let form = Form::new();
wizard.add_step(WizardStep::new("step1".to_string(), form));
let mut data = HashMap::new();
data.insert("field".to_string(), json!("value"));Sourcepub fn progress_percentage(&self) -> f32
pub fn progress_percentage(&self) -> f32
Returns the wizard completion progress as a percentage (0.0 to 100.0).