add validation builder

This commit is contained in:
2024-06-10 12:14:21 -05:00
parent 360ef63b58
commit bf04957370
4 changed files with 155 additions and 13 deletions
+7 -7
View File
@@ -11,11 +11,11 @@ use web_sys::FormData;
///
/// This can be useful to use the same validation logic on the front
/// end and backend without duplicating the logic.
pub struct Validator<FD> {
pub struct FormValidator<FD> {
pub(crate) validations: Vec<Rc<dyn ValidationFn<FD>>>,
}
impl<FD: FormToolData> Validator<FD> {
impl<FD: FormToolData> FormValidator<FD> {
/// Validates the given form data.
///
/// This runs all the validation functions for all the fields
@@ -40,8 +40,8 @@ pub struct Form<FD: FormToolData> {
impl<FD: FormToolData> Form<FD> {
/// Gets the [`Validator`] for this form.
pub fn validator(self) -> Validator<FD> {
Validator {
pub fn validator(self) -> FormValidator<FD> {
FormValidator {
validations: self.validations,
}
}
@@ -57,10 +57,10 @@ impl<FD: FormToolData> Form<FD> {
}
/// Splits this [`Form`] into it's parts.
pub fn to_parts(self) -> (RwSignal<FD>, Validator<FD>, View) {
pub fn to_parts(self) -> (RwSignal<FD>, FormValidator<FD>, View) {
(
self.fd,
Validator {
FormValidator {
validations: self.validations,
},
self.view,
@@ -131,7 +131,7 @@ pub trait FormToolData: Default + 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() -> Validator<Self> {
fn get_validator() -> FormValidator<Self> {
let builder = FormBuilder::new(Self::default(), Self::Style::default());
let builder = Self::build_form(builder);
builder.validator()