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.
Class methods
def self.humanize(field: Symbol) -> String
email_address as Email address.
Methods
def html -> Bezel::HtmlBuilder
The HTML builder, for markup between the fields.
def name(field: Symbol) -> String
The name of field's input: user[email].
def id(field: Symbol) -> String
The id of field's input: user_email.
def value(field: Symbol) -> String?
The value the form shows for field, or nil.
def errors_on(field: Symbol) -> Array[String]
The errors on field.
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).
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.
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.
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.
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.
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.
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).
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.
def errors(field: Symbol) -> Void
Writes the errors on field (<span class="error">, one per error,
joined by , ), or nothing without one.
def submit(text: String = …, *, class_name: String | Array[String?] | Nil = …) -> Void
Writes the submit button.