Skip to main content

ModelFormSet

Struct ModelFormSet 

Source
pub struct ModelFormSet<T: FormModel> { /* private fields */ }
Expand description

A formset for managing multiple model instances

Implementations§

Source§

impl<T: FormModel> ModelFormSet<T>

Source

pub fn new( prefix: String, instances: Vec<T>, config: ModelFormSetConfig, ) -> Self

Create a new ModelFormSet with instances

§Examples
use reinhardt_forms::{ModelFormSet, ModelFormSetConfig};

let config = ModelFormSetConfig::new();
let instances = vec![]; // Empty list of model instances
let formset = ModelFormSet::<MyModel>::new("formset".to_string(), instances, config);
assert_eq!(formset.prefix(), "formset");
Source

pub fn empty(prefix: String, config: ModelFormSetConfig) -> Self

Create an empty ModelFormSet (for creating new instances)

§Examples
use reinhardt_forms::{ModelFormSet, ModelFormSetConfig};

let config = ModelFormSetConfig::new().with_extra(3);
let formset = ModelFormSet::<MyModel>::empty("formset".to_string(), config);
assert_eq!(formset.total_form_count(), 3);
Source

pub fn prefix(&self) -> &str

Source

pub fn instances(&self) -> Vec<&T>

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::{ModelFormSet, ModelFormSetConfig};

let config = ModelFormSetConfig::new();
let mut formset = ModelFormSet::<MyModel>::empty("formset".to_string(), config);
let is_valid = formset.is_valid();
Source

pub fn errors(&self) -> Vec<String>

Source

pub fn save(&mut self) -> Result<Vec<T>, FormError>

Save all valid forms to the database

§Examples
use reinhardt_forms::{ModelFormSet, ModelFormSetConfig};

let config = ModelFormSetConfig::new();
let mut formset = ModelFormSet::<MyModel>::empty("formset".to_string(), config);
let result = formset.save();
Source

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

Get management form data for HTML rendering

§Examples
use reinhardt_forms::{ModelFormSet, ModelFormSetConfig};

let config = ModelFormSetConfig::new().with_extra(2);
let formset = ModelFormSet::<MyModel>::empty("article".to_string(), config);
let mgmt_data = formset.management_form_data();

assert!(mgmt_data.contains_key("article-TOTAL_FORMS"));
assert_eq!(mgmt_data.get("article-TOTAL_FORMS"), Some(&"2".to_string()));

Auto Trait Implementations§

§

impl<T> Freeze for ModelFormSet<T>

§

impl<T> !RefUnwindSafe for ModelFormSet<T>

§

impl<T> Send for ModelFormSet<T>

§

impl<T> Sync for ModelFormSet<T>

§

impl<T> Unpin for ModelFormSet<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for ModelFormSet<T>

§

impl<T> !UnwindSafe for ModelFormSet<T>

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.