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());Sourcepub fn can_delete(&self) -> bool
pub fn can_delete(&self) -> bool
Returns whether forms in this set can be deleted.
Sourcepub fn with_extra(self, extra: usize) -> Self
pub fn with_extra(self, extra: usize) -> Self
Sets the number of extra empty forms to include.
Sourcepub fn with_can_delete(self, can_delete: bool) -> Self
pub fn with_can_delete(self, can_delete: bool) -> Self
Enables or disables form deletion within this set.
Sourcepub fn with_can_order(self, can_order: bool) -> Self
pub fn with_can_order(self, can_order: bool) -> Self
Enables or disables ordering of forms within this set.
Sourcepub fn with_max_num(self, max_num: Option<usize>) -> Self
pub fn with_max_num(self, max_num: Option<usize>) -> Self
Sets the maximum number of forms allowed.
Sourcepub fn with_min_num(self, min_num: usize) -> Self
pub fn with_min_num(self, min_num: usize) -> Self
Sets the minimum number of forms required for validation.
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());Sourcepub fn form_count(&self) -> usize
pub fn form_count(&self) -> usize
Returns the number of forms currently in this set.
Sourcepub fn total_form_count(&self) -> usize
pub fn total_form_count(&self) -> usize
Returns the total form count including extra empty forms.
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();Sourcepub fn cleaned_data(&self) -> Vec<&HashMap<String, Value>>
pub fn cleaned_data(&self) -> Vec<&HashMap<String, Value>>
Returns cleaned data from all forms in the set.
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);