Skip to main content

FormSet

Struct FormSet 

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

FormSet manages multiple forms

Implementations§

Source§

impl FormSet

Source

pub fn new(prefix: String) -> Self

Create a new FormSet with the given prefix

§Examples
use reinhardt_forms::FormSet;

let formset = FormSet::new("form".to_string());
assert_eq!(formset.prefix(), "form");
assert!(!formset.can_delete());
Source

pub fn prefix(&self) -> &str

Source

pub fn can_delete(&self) -> bool

Source

pub fn with_extra(self, extra: usize) -> Self

Source

pub fn with_can_delete(self, can_delete: bool) -> Self

Source

pub fn with_can_order(self, can_order: bool) -> Self

Source

pub fn with_max_num(self, max_num: Option<usize>) -> Self

Source

pub fn with_min_num(self, min_num: usize) -> Self

Source

pub fn add_form(&mut self, form: Form) -> Result<(), String>

Add a form to the formset.

Returns an error if adding the form would exceed max_num.

§Examples
use reinhardt_forms::{FormSet, Form};

let mut formset = FormSet::new("form".to_string());
let form = Form::new();
assert!(formset.add_form(form).is_ok());
assert_eq!(formset.forms().len(), 1);
use reinhardt_forms::{FormSet, Form};

let mut formset = FormSet::new("form".to_string()).with_max_num(Some(1));
assert!(formset.add_form(Form::new()).is_ok());
assert!(formset.add_form(Form::new()).is_err());
Source

pub fn forms(&self) -> &[Form]

Source

pub fn forms_mut(&mut self) -> &mut Vec<Form>

Source

pub fn form_count(&self) -> usize

Source

pub fn total_form_count(&self) -> usize

Source

pub fn is_valid(&mut self) -> bool

Validate all forms in the formset

§Examples
use reinhardt_forms::{FormSet, Form};

let mut formset = FormSet::new("form".to_string());
formset.add_form(Form::new()).unwrap();
Source

pub fn errors(&self) -> &[String]

Source

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

Source

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

Get management form data (for tracking forms in HTML)

§Examples
use reinhardt_forms::FormSet;

let formset = FormSet::new("form".to_string());
let data = formset.management_form_data();
assert!(data.contains_key("form-TOTAL_FORMS"));
Source

pub fn process_data(&mut self, data: &HashMap<String, HashMap<String, Value>>)

Process bound data from HTML forms.

Respects max_num and silently stops adding forms once the limit is reached.

§Examples
use reinhardt_forms::FormSet;
use std::collections::HashMap;
use serde_json::json;

let mut formset = FormSet::new("form".to_string());
let mut data = HashMap::new();
let mut form_data = HashMap::new();
form_data.insert("field".to_string(), json!("value"));
data.insert("form-0".to_string(), form_data);

formset.process_data(&data);
assert_eq!(formset.form_count(), 1);

Trait Implementations§

Source§

impl Default for FormSet

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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, 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.