pub struct FormSet { /* private fields */ }Expand description
FormSet manages multiple forms
Implementations§
Source§impl FormSet
impl FormSet
Sourcepub fn new(prefix: String) -> Self
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());pub fn prefix(&self) -> &str
pub fn can_delete(&self) -> bool
pub fn with_extra(self, extra: usize) -> Self
pub fn with_can_delete(self, can_delete: bool) -> Self
pub fn with_can_order(self, can_order: bool) -> Self
pub fn with_max_num(self, max_num: Option<usize>) -> Self
pub fn with_min_num(self, min_num: usize) -> Self
Sourcepub fn add_form(&mut self, form: Form) -> Result<(), String>
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());pub fn forms(&self) -> &[Form]
pub fn forms_mut(&mut self) -> &mut Vec<Form>
pub fn form_count(&self) -> usize
pub fn total_form_count(&self) -> usize
Sourcepub fn is_valid(&mut self) -> bool
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();pub fn errors(&self) -> &[String]
pub fn cleaned_data(&self) -> Vec<&HashMap<String, Value>>
Sourcepub fn management_form_data(&self) -> HashMap<String, String>
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"));Sourcepub fn process_data(&mut self, data: &HashMap<String, HashMap<String, Value>>)
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§
Auto Trait Implementations§
impl Freeze for FormSet
impl !RefUnwindSafe for FormSet
impl Send for FormSet
impl Sync for FormSet
impl Unpin for FormSet
impl UnsafeUnpin for FormSet
impl !UnwindSafe for FormSet
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