All Projects → jfbilodeau → yew_form

jfbilodeau / yew_form

Licence: Apache-2.0, MIT licenses found Licenses found Apache-2.0 LICENSE-APACHE MIT LICENSE-MIT
Components to simplify handling forms with Yew

Programming Languages

rust
11053 projects

Projects that are alternatives of or similar to yew form

facade
Facade Framework - autogenerated embedded live dashboards for Rust apps
Stars: ✭ 95 (+14.46%)
Mutual labels:  yew
yew-router
Router extension to yew
Stars: ✭ 27 (-67.47%)
Mutual labels:  yew
wasi-worker
WASM / WASI interface for browser service workers
Stars: ✭ 31 (-62.65%)
Mutual labels:  yew
patternfly-yew
PatternFly components for Yew
Stars: ✭ 51 (-38.55%)
Mutual labels:  yew
ybc
A Yew component library based on the Bulma CSS framework.
Stars: ✭ 131 (+57.83%)
Mutual labels:  yew
rust-electron-demo
rust electron demo using yew
Stars: ✭ 15 (-81.93%)
Mutual labels:  yew
rusty-connect4
A full-stack application written completely in Rust.
Stars: ✭ 30 (-63.86%)
Mutual labels:  yew
fullstack-rust
Reference implementation of a full-stack Rust application
Stars: ✭ 39 (-53.01%)
Mutual labels:  yew
rocket-yew-starter-pack
Example boilerplate for websites in pure Rust
Stars: ✭ 77 (-7.23%)
Mutual labels:  yew
material-yew
Yew wrapper for Material Web Components
Stars: ✭ 116 (+39.76%)
Mutual labels:  yew
bounce
The uncomplicated Yew State management library
Stars: ✭ 43 (-48.19%)
Mutual labels:  yew
lifeee-rs
An implementation of the Game of Life
Stars: ✭ 53 (-36.14%)
Mutual labels:  yew
stylist-rs
A CSS-in-Rust styling solution for WebAssembly Applications
Stars: ✭ 124 (+49.4%)
Mutual labels:  yew
zzhack
🦀️ WASM webapp template written in Rust
Stars: ✭ 170 (+104.82%)
Mutual labels:  yew
FullstackRustDemo
Novelty website + bucket questions implementation.
Stars: ✭ 40 (-51.81%)
Mutual labels:  yew
surfer
The Blog built on pure Rust stack. Backend for graphql services, and frontend for web application.
Stars: ✭ 35 (-57.83%)
Mutual labels:  yew
paudle
Wordle in Rust
Stars: ✭ 40 (-51.81%)
Mutual labels:  yew
reacty yew
Generate Yew components from React components via Typescript type definitions
Stars: ✭ 46 (-44.58%)
Mutual labels:  yew
portfolio
My Portfolio
Stars: ✭ 17 (-79.52%)
Mutual labels:  yew
Yew-WebRTC-Chat
A simple WebRTC chat made with Yew
Stars: ✭ 112 (+34.94%)
Mutual labels:  yew

License:MIT License:Apache

Yew Form

Bringing MVC to Yew! A set of mildly opinionated Yew component to map and validate a model to a HTML form.

Live demo

Supports:

  • 2-way Binding to struct (with support for nested structs)
  • Validation (using Keats Validator)
  • Only String and bool fields are supported presently. More to come

Usage

Cargo.toml:

[dependencies]
validator = "0.14"
validator_derive = "0.14"
yew = "0.18"
yew_form = "0.1"
yew_form_derive = "0.1"

main.rs:

#[macro_use]
extern crate validator_derive;
extern crate yew_form;
#[macro_use]
extern crate yew_form_derive;

Example

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 
<<<<<<< HEAD
    field_name="first_name"
    autocomplete="given-name"
=======
    field_name="first_name" 
>>>>>>> 0ab668b7f3fb9ba13e1e0d4d97bee619280f3a3d
    oninput=self.link.callback(|_: InputData| AppMessage::Update) />

<!-- here we use custom css classes -->
<Field<Registration> 
    form=&self.form 
    field_name="address.street"
    class="form-control"
    class_invalid="is-invalid red-border"
    class_valid="is-valid green-border"
    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:

  • Add documentation (In progress)
  • Remove clone requirement from model
  • Add derive for model to remove need for vec! of fields
  • Make oninput optional
  • Make Yew update the view when Field is updated
  • Need to add additional HTML attribute to Field
  • Remove hard-coded Bootstrap styles
  • Add support for additional types such as i32
  • Support Vec<T>
  • Support Rust Stable

Change Log

0.1.8

  • Remove hardcoded Bootstrap css classes
  • Fix examples/form
  • Add autocomplete attribute

0.1.7

  • Remove #![feature(get_mut_unchecked)] from code (Thanks L0g4n)

0.1.6

  • Removed unsafe code
  • Updated yew_form version in documentation

0.1.5

  • Updated to Yew 0.17

0.1.4

  • Added blanket implementation for FieldValue to support i32, bool, etc...

0.1.3

BREAKING CHANGES

  • Added #[derive(Model)]
  • No need to manually pass a vector of fields to Form::new()

0.1.2

  • Added CheckBox

0.1.1

  • Make Field::oninput optional

(Made with ❤️ with Rust)

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].