class Bezel::FormBuilder

include DslScope

Writes the fields of a form over a rotor changeset (HtmlBuilder#form_for): each field is named <prefix>[<field>] (user[email]), with the id <prefix>_<field>, and shows the value the changeset holds: its change, else the model's; for a permitted field with errors and no change (a value that did not cast, or a blank required one), the input as given, so that it shows as typed. errors writes the changeset's errors on a field.

class FormUser < Rotor::Model
  table "form_users" do
    column :id, Int, primary: true
    column :email, String
  end
end

cs = Rotor::Changeset.cast(FormUser, {"email" => "ann"}, [:email]).validate_format(:email, /@/)
page = Bezel.html do |h|
  h.form_for(cs, action: "/users") do |f|
    f.label(:email, "Email")
    f.text_field(:email)
    f.errors(:email)
  end
end
page.to_s
# => "<form action=\"/users\" method=\"post\"><label for=\"form_user_email\">Email</label><input type=\"text\" name=\"form_user[email]\" value=\"ann\" id=\"form_user_email\" class=\"invalid\" aria-invalid=\"true\"><span id=\"form_user_email_error\" class=\"error\">has invalid format</span></form>"

A field with errors gets the class invalid and aria-invalid.

src/html/form.qz:30

Class methods

def self.humanize(field: Symbol) -> String

email_address as Email address.

src/html/form.qz:163

Methods

def html -> Bezel::HtmlBuilder

The HTML builder, for markup between the fields.

src/html/form.qz:43

def name(field: Symbol) -> String

The name of field's input: user[email].

src/html/form.qz:46

def id(field: Symbol) -> String

The id of field's input: user_email.

src/html/form.qz:49

def value(field: Symbol) -> String?

The value the form shows for field, or nil.

src/html/form.qz:52

def errors_on(field: Symbol) -> Array[String]

The errors on field.

src/html/form.qz:55

def label(field: Symbol, text: String? = …, *, class_name: String | Array[String?] | Nil = …) -> Void

Writes <label for="...">text</label> (the field's name, humanized, without text).

src/html/form.qz:59

def text_field(field: Symbol, *, placeholder: String? = …, required: Bool = …, class_name: String | Array[String?] | Nil = …, attrs: Hash[String, String] = …) -> Void

Writes an <input type="text"> for field.

src/html/form.qz:64

def email_field(field: Symbol, *, placeholder: String? = …, required: Bool = …, class_name: String | Array[String?] | Nil = …, attrs: Hash[String, String] = …) -> Void

Writes an <input type="email"> for field.

src/html/form.qz:70

def number_field(field: Symbol, *, placeholder: String? = …, required: Bool = …, class_name: String | Array[String?] | Nil = …, attrs: Hash[String, String] = …) -> Void

Writes an <input type="number"> for field.

src/html/form.qz:76

def password_field(field: Symbol, *, placeholder: String? = …, required: Bool = …, class_name: String | Array[String?] | Nil = …, attrs: Hash[String, String] = …) -> Void

Writes an <input type="password"> for field, never with a value.

src/html/form.qz:82

def hidden(field: Symbol) -> Void

Writes an <input type="hidden"> for field.

src/html/form.qz:88

def textarea(field: Symbol, *, rows: Int? = …, placeholder: String? = …, required: Bool = …, class_name: String | Array[String?] | Nil = …) -> Void

Writes a <textarea> for field, its value the content.

src/html/form.qz:93

def checkbox(field: Symbol, *, class_name: String | Array[String?] | Nil = …) -> Void

Writes a checkbox for field: a hidden false first, so that an unchecked box still sends the field, then the box (value true), checked when the value is true (true, 1, on).

src/html/form.qz:103

def select(field: Symbol, options: Array[[String, String]], *, prompt: String? = …, class_name: String | Array[String?] | Nil = …) -> Void

Writes a <select> for field of options ([label, value] pairs), the one of the field's value selected; prompt adds a first option with an empty value.

src/html/form.qz:114

def errors(field: Symbol) -> Void

Writes the errors on field (<span class="error">, one per error, joined by , ), or nothing without one.

src/html/form.qz:129

def submit(text: String = …, *, class_name: String | Array[String?] | Nil = …) -> Void

Writes the submit button.

src/html/form.qz:135