Skip to main content

FormWizard

Struct FormWizard 

Source
pub struct FormWizard { /* private fields */ }
Expand description

FormWizard manages multi-step forms

Implementations§

Source§

impl FormWizard

Source

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());
Source

pub fn steps(&self) -> &Vec<WizardStep>

Returns a reference to all wizard steps.

Source

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);
Source

pub fn current_step(&self) -> usize

Returns the zero-based index of the current step.

Source

pub fn current_step_name(&self) -> Option<&str>

Returns the name of the current step, or None if there are no steps.

Source

pub fn current_form(&self) -> Option<&Form>

Returns a reference to the current step’s form, or None if there are no steps.

Source

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.

Source

pub fn total_steps(&self) -> usize

Returns the total number of steps in the wizard.

Source

pub fn is_first_step(&self) -> bool

Returns true if the wizard is on the first step.

Source

pub fn is_last_step(&self) -> bool

Returns true if the wizard is on the last step.

Source

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);
Source

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);
Source

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());
Source

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());
Source

pub fn get_all_data(&self) -> &HashMap<String, HashMap<String, Value>>

Returns all session data collected across all completed steps.

Source

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.

Source

pub fn clear_data(&mut self)

Clears all session data and resets the wizard to the first step.

Source

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"));
Source

pub fn progress_percentage(&self) -> f32

Returns the wizard completion progress as a percentage (0.0 to 100.0).

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.