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());pub fn steps(&self) -> &Vec<WizardStep>
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);pub fn current_step(&self) -> usize
pub fn current_step_name(&self) -> Option<&str>
pub fn current_form(&self) -> Option<&Form>
pub fn current_form_mut(&mut self) -> Option<&mut Form>
pub fn total_steps(&self) -> usize
pub fn is_first_step(&self) -> bool
pub fn is_last_step(&self) -> bool
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());pub fn get_all_data(&self) -> &HashMap<String, HashMap<String, Value>>
pub fn get_step_data(&self, step_name: &str) -> Option<&HashMap<String, Value>>
pub fn clear_data(&mut self)
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"));pub fn progress_percentage(&self) -> f32
Auto Trait Implementations§
impl Freeze for FormWizard
impl !RefUnwindSafe for FormWizard
impl Send for FormWizard
impl Sync for FormWizard
impl Unpin for FormWizard
impl UnsafeUnpin for FormWizard
impl !UnwindSafe for FormWizard
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more