Crates.io | yew_form_derive |
lib.rs | yew_form_derive |
version | 0.1.7 |
source | src |
created_at | 2020-03-05 10:40:43.130503 |
updated_at | 2020-09-02 12:06:01.078405 |
description | Bringing MVC to Yew! A set mildly opinionated of Yew component to map and validate a model to a HTML form |
homepage | |
repository | https://github.com/jfbilodeau/yew_form |
max_upload_size | |
id | 215669 |
size | 5,068 |
Bringing MVC to Yew! A set of mildly opinionated Yew component to map and validate a model to a HTML form.
Supports:
String
and bool
fields are supported presently. More to comeCargo.toml:
[dependencies]
validator = "0.10"
validator_derive = "0.10"
yew = "0.12"
yew_form = "0.1"
yew_form_derive = "0.1.4"
main.rs:
#[macro_use]
extern crate validator_derive;
extern crate yew_form;
#[macro_use]
extern crate yew_form_derive;
Consider the following model:
#[derive(Model, Validate, PartialEq, Clone)]
struct Address {
#[validate(length(min = 1, message="Street is required"))]
street: String,
#[validate(length(min = 1, message="City name is required"))]
city: String,
#[validate(regex(path="PROVINCE_RE", message="Enter 2 digit province code"))]
province: String,
postal_code: String,
country: String,
}
#[derive(Model, Validate, PartialEq, Clone)]
struct Registration {
#[validate(length(min = 1, message="First name is required"))]
first_name: String,
#[validate(length(min = 1, message="Last name is required"))]
last_name: String,
quantity: u32,
price: f64,
#[validate]
address: Address,
#[validate(custom = "must_be_true")]
accept_terms: bool,
}
The struct can be bound to a Yew form using the following definition:
struct App {
form: Form<Registration>,
...
}
For now, the Form
needs to be instantiated as follows:
fn create(_props: Self::Properties, link: ComponentLink<Self>) -> Self {
// Create model initial state
let model = Registration {
first_name: String::from("J-F"),
last_name: String::from("Bilodeau"),
address: Address {
street: String::new(),
city: String::from("Ottawa"),
province: String::from("ONT"),
postal_code: String::from("K2P 0A4"),
country: String::new(),
},
};
Self {
form: Form::new(model),
...
}
...
Fields can then be added to the form as follows:
<Field<Registration> form=&self.form field_name="first_name" oninput=self.link.callback(|_: InputData| AppMessage::Update) />
...
<Field<Registration> form=&self.form field_name="address.street" oninput=self.link.callback(|_: InputData| AppMessage::Update) />
...
<CheckBox<Registration> field_name="accept_terms" form=&self.form />
The Field
component takes care of two way binding between struct Registration
and the HTML <input>
Validation is done automatically when the user edits the form or programmatically.
if self.form.validate() {
...
}
Todo/Wish List:
derive
for model to remove need for vec!
of fieldsoninput
optionalField
is updatedField
i32
Vec<T>
#![feature(get_mut_unchecked)]
from code (Thanks L0g4n)i32
, bool
, etc...BREAKING CHANGES
#[derive(Model)]
Form::new()
Make Field::oninput
optional
(Made with ❤️ with Rust)