Archived
generated from mitchell/rust_template
add context
This commit is contained in:
+23
-13
@@ -40,15 +40,16 @@ pub struct Form<FD: FormToolData> {
|
||||
|
||||
impl<FD: FormToolData> Form<FD> {
|
||||
/// Gets the [`Validator`] for this form.
|
||||
pub fn validator(self) -> FormValidator<FD> {
|
||||
pub fn validator(&self) -> FormValidator<FD> {
|
||||
FormValidator {
|
||||
validations: self.validations,
|
||||
validations: self.validations.clone(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Validates the [`ToolFormData`], returning the result
|
||||
pub fn validate(&self) -> Result<(), String> {
|
||||
self.fd.get_untracked().validate()
|
||||
let validator = self.validator();
|
||||
validator.validate(&self.fd.get_untracked())
|
||||
}
|
||||
|
||||
/// Gets the view associated with this [`Form`].
|
||||
@@ -80,6 +81,7 @@ impl<FD: FormToolData> IntoView for Form<FD> {
|
||||
/// to physically lay out a form, and how that data should be parsed and validated.
|
||||
pub trait FormToolData: Clone + 'static {
|
||||
type Style: FormStyle;
|
||||
type Context: 'static;
|
||||
|
||||
/// Defines how the form should be layed out and how the data should be parsed and validated.
|
||||
///
|
||||
@@ -88,7 +90,9 @@ pub trait FormToolData: Clone + 'static {
|
||||
/// Uses the given form builder to specify what fields should be present
|
||||
/// in the form, what properties those fields should have, and how that
|
||||
/// data should be parsed and checked.
|
||||
fn build_form(fb: FormBuilder<Self, Self::Style>) -> FormBuilder<Self, Self::Style>;
|
||||
fn build_form(
|
||||
fb: FormBuilder<Self, Self::Style, Self::Context>,
|
||||
) -> FormBuilder<Self, Self::Style, Self::Context>;
|
||||
|
||||
/// Constructs a [`Form`] for this [`FormToolData`] type.
|
||||
///
|
||||
@@ -96,8 +100,13 @@ pub trait FormToolData: Clone + 'static {
|
||||
/// [`Form`](leptos_router::Form)
|
||||
/// component. Call [`get_action_form`]\() to get the
|
||||
/// [`ActionForm`](leptos_router::ActionForm) version.
|
||||
fn get_form(self, action: impl ToString, style: Self::Style) -> Form<Self> {
|
||||
let builder = FormBuilder::new();
|
||||
fn get_form(
|
||||
self,
|
||||
action: impl ToString,
|
||||
style: Self::Style,
|
||||
context: Self::Context,
|
||||
) -> Form<Self> {
|
||||
let builder = FormBuilder::new(context);
|
||||
let builder = Self::build_form(builder);
|
||||
builder.build_plain_form(action.to_string(), self, style)
|
||||
}
|
||||
@@ -112,13 +121,14 @@ pub trait FormToolData: Clone + 'static {
|
||||
self,
|
||||
action: Action<ServFn, Result<ServFn::Output, ServerFnError<ServFn::Error>>>,
|
||||
style: Self::Style,
|
||||
context: Self::Context,
|
||||
) -> Form<Self>
|
||||
where
|
||||
ServFn: DeserializeOwned + ServerFn<InputEncoding = PostUrl> + 'static,
|
||||
<<ServFn::Client as Client<ServFn::Error>>::Request as ClientReq<ServFn::Error>>::FormData:
|
||||
From<FormData>,
|
||||
{
|
||||
let builder = FormBuilder::new();
|
||||
let builder = FormBuilder::new(context);
|
||||
let builder = Self::build_form(builder);
|
||||
builder.build_action_form(action, self, style)
|
||||
}
|
||||
@@ -131,18 +141,18 @@ pub trait FormToolData: Clone + 'static {
|
||||
///
|
||||
/// However, the code to render the views are not configured out, it
|
||||
/// simply doesn't run, so the view needs to compile even on the server.
|
||||
fn get_validator() -> FormValidator<Self> {
|
||||
let builder = FormBuilder::new();
|
||||
fn get_validator(context: Self::Context) -> FormValidator<Self> {
|
||||
let builder = FormBuilder::new(context);
|
||||
let builder = Self::build_form(builder);
|
||||
builder.validator()
|
||||
}
|
||||
|
||||
/// Validates this [`FormToolData`] struct.
|
||||
///
|
||||
/// This is shorthand for creating a validator with [`get_validator`]\()
|
||||
/// and then calling `validator.validate(&self)`.
|
||||
fn validate(&self) -> Result<(), String> {
|
||||
let validator = Self::get_validator();
|
||||
/// This is shorthand for creating a validator with [`get_validator`]
|
||||
/// and then calling `validator.validate(&self, context)`.
|
||||
fn validate(&self, context: Self::Context) -> Result<(), String> {
|
||||
let validator = Self::get_validator(context);
|
||||
validator.validate(self)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user