Skip to main content

ModelForm

Struct ModelForm 

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

A form that is automatically generated from a Model

Implementations§

Source§

impl<T: FormModel> ModelForm<T>

Source

pub fn new(instance: Option<T>, config: ModelFormConfig) -> Self

Create a new ModelForm from a model instance

§Examples
use reinhardt_forms::{ModelForm, ModelFormConfig};

let config = ModelFormConfig::new();
let form = ModelForm::new(Some(instance), config);
Source

pub fn empty(config: ModelFormConfig) -> Self

Create a new ModelForm without an instance (for creation)

§Examples
use reinhardt_forms::{ModelForm, ModelFormConfig};

let config = ModelFormConfig::new();
let form = ModelForm::<MyModel>::empty(config);
Source

pub fn bind(&mut self, data: HashMap<String, Value>) -> &mut Self

Bind data to the form

§Examples
use reinhardt_forms::{ModelForm, ModelFormConfig};
use std::collections::HashMap;
use serde_json::json;

let config = ModelFormConfig::new();
let mut form = ModelForm::<MyModel>::empty(config);
let mut data = HashMap::new();
data.insert("field".to_string(), json!("value"));
form.bind(data);
Source

pub fn is_valid(&mut self) -> bool

Check if the form is valid

§Examples
use reinhardt_forms::{ModelForm, ModelFormConfig};

let config = ModelFormConfig::new();
let mut form = ModelForm::<MyModel>::empty(config);
let is_valid = form.is_valid();
Source

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

Save the form data to the model instance

Returns FormError::NoInstance if no model instance is available.

§Examples
use reinhardt_forms::{ModelForm, ModelFormConfig};

let config = ModelFormConfig::new();
let mut form = ModelForm::<MyModel>::empty(config);
// Returns Err(FormError::NoInstance) without an instance
assert!(form.save().is_err());
Source

pub fn set_field_value(&mut self, field_name: &str, value: Value)

Set a field value directly on the model instance.

This is used by InlineFormSet to set foreign key values on child instances before saving.

If no instance exists, this method is a no-op.

Source

pub fn form(&self) -> &Form

Source

pub fn form_mut(&mut self) -> &mut Form

Source

pub fn instance(&self) -> Option<&T>

Auto Trait Implementations§

§

impl<T> Freeze for ModelForm<T>
where T: Freeze,

§

impl<T> !RefUnwindSafe for ModelForm<T>

§

impl<T> Send for ModelForm<T>

§

impl<T> Sync for ModelForm<T>

§

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

§

impl<T> UnsafeUnpin for ModelForm<T>
where T: UnsafeUnpin,

§

impl<T> !UnwindSafe for ModelForm<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.