pub struct CustomField<F = ()>(/* private fields */);Expand description
A field an app’s Control renders: Field::custom and Field::toggle, or a record
form’s toggle (F is the form).
Implementations§
Source§impl<F: EditsRecords> CustomField<F>
impl<F: EditsRecords> CustomField<F>
Renders nothing for the field on the create form. The create stores the field’s
default, else its blank answer: a required field with neither cannot mount. A
field hidden on both forms only ever stores that: set it in the record instead.
Renders nothing for the field on the edit form, and keeps its stored value whatever
an edit posts: the create form sets the value, and no edit changes it, as
disabled_on_edit does without showing it.
Source§impl<F> CustomField<F>
impl<F> CustomField<F>
Sourcepub fn disabled_when(
self,
watched: &impl Watched<F>,
values: impl IntoIterator<Item = impl Into<String>>,
) -> Self
pub fn disabled_when( self, watched: &impl Watched<F>, values: impl IntoIterator<Item = impl Into<String>>, ) -> Self
Renders the control disabled while watched posts one of values: an option’s
value, or true or false for a toggle. Disabled, the control posts nothing, so
an edit keeps the stored value and a create takes the field’s default, else its
blank answer.
let status = Field::choice(Order::fields().status()).options(["draft", "sent"]);
let total = Field::text(Order::fields().total()).disabled_when(&status, ["sent"]);
Schema::new((status, total));- The browser enables and disables the control as the watched field changes, with no
request, through a disabled fieldset around it. An app’s own
Controlfollows it only through the form controls it renders. Without JavaScript, the control starts as the form’s initial values set it, the stored ones on an edit, and stays that way. - The panel reads a submission the same way and drops the field’s posted key, and a
file field’s carried upload, while the condition holds. The condition reads what
the user may post, so it is no access check:
ResourceDef::field_accessis. - Mounting refuses what it refuses for
visible_when: an unplaced watched field, a value it never posts, a multiple choice, and a watched field another condition hides while this one still shows. It also refuses a required field with no non-blankdefault, which a create could not fill while the condition holds, and a condition or a dependent choice watching this field: a disabled control posts nothing, so the browser and the server would read different values. - A later call replaces an earlier one.
Source§impl<F> CustomField<F>
impl<F> CustomField<F>
Sourcepub fn default(self, value: impl IntoFormValue) -> Self
pub fn default(self, value: impl IntoFormValue) -> Self
Fills the create form’s control with value: any
FormScalar or a &str in the form spelling, or, for a
multiple choice, an array or Vec of them. On a
disabled field it is the value a create stores. A schema
refuses a default the control never posts: one a typed field does not parse, one
not among a choice’s options, a list on a control posting one value, and one value
on a multiple choice.
Field::choice(Post::fields().status())
.options(["draft", "published"])
.default("draft");
Field::text(Post::fields().views()).default(0_i64).disabled();
Field::choice_input("channels")
.options(["email", "sms"])
.multiple()
.default(["email"]);- The control names its help text in
aria-describedby, beside its error. - A disabled control renders
disabled, so the browser posts nothing for it, and the panel drops a key posted anyway, with a file field’s carried upload. An edit keeps the stored value. A create stores the field’s default, else its blank answer:.disabled()on a field with neither refuses to mount, since no create could fill it. .disabled_on_edit()leaves the create form’s control enabled: the create sets the value, and no edit changes it.- A condition watching a disabled field reads what the browser shows: its default on a create, its stored value on an edit, never what the submission posts.
- A default seeds the create form and an action’s input form; an edit form shows the stored value. The blank answer is different: it is what an empty submission stores. Mounting refuses a default the control never posts: one a typed field does not parse, or not one of a choice’s options.
- A repeater’s row controls take neither
disablednordefault: mounting refuses both.
Source§impl<F> CustomField<F>
impl<F> CustomField<F>
Sourcepub fn switch(self) -> Self
pub fn switch(self) -> Self
Renders a bool field as a switch rather than a checkbox: the same toggle, posting true
or false, drawn as an on/off track. A condition follows it as it follows a toggle.
Field::toggle(lens!(Post.featured)).switch();Sourcepub fn accepted(self) -> Self
pub fn accepted(self) -> Self
Refuse a submission that leaves the checkbox unchecked, and render it required: the box
accepting the terms an action’s input asks for. Only the built-in checkbox of
Field::toggle and Field::toggle_input takes it, drawn as a box or a
switch; another control fails the mount, as
does a disabled one with no true default. A hidden field is not
checked, nor a value the record already stores. A request a
FieldAccess view rule hides the field from may not create unless
it has a true default.
Field::toggle_input("terms")
.label("I accept the terms")
.accepted();Source§impl<F> CustomField<F>
impl<F> CustomField<F>
pub fn label(self, label: impl Into<Text>) -> Self
pub fn name(&self) -> &str
Sourcepub fn visible_when(
self,
watched: &impl Watched<F>,
values: impl IntoIterator<Item = impl Into<String>>,
) -> Self
pub fn visible_when( self, watched: &impl Watched<F>, values: impl IntoIterator<Item = impl Into<String>>, ) -> Self
Shows the field only while watched posts one of values: an option’s value, or
true for a checked toggle. A hidden field posts nothing, so an edit keeps its
stored value and a create takes its blank answer; a field with no blank answer
cannot be conditional. A field shows under one condition: a later call replaces an
earlier one, so a field depending on two watched fields sits in a
Section guarded by the other.
let kind = Field::choice(Customer::fields().kind()).options(["person", "company"]);
let vat = Field::text(Customer::fields().vat_number()).visible_when(&kind, ["company"]);
Schema::new((kind, vat));- The browser shows and hides the field as the watched one changes, with no request. A hidden field’s control is disabled, so the browser neither validates nor posts it. Without JavaScript, the fields start as the stored values show them and stay that way.
- The panel reads a submission the same way and drops a hidden field’s posted key, and a file field’s carried upload: an edit keeps the stored value, and a create stores the blank answer. A conditional field therefore needs a blank answer, and mounting refuses one without.
- Mounting also refuses a value the watched field never posts (a choice’s option or
a toggle’s
trueandfalseare checked), and a condition watching a field that another condition hides while the guarded field still shows: place the guarded field inside the block that hides the watched one. A schema also refuses a condition watching a field it does not place, which only a page’s or an action’s schema can do: a resource’s form places every record-form field. - A toggle is followed through its checkbox. An app’s own checkbox
Controlposts the samevaluechecked or not, so a condition cannot follow it. - A multiple choice’s key holds a list, which no condition value spells: mounting refuses a condition watching one.
Source§impl<F> CustomField<F>
impl<F> CustomField<F>
Sourcepub fn help(self, text: impl Into<Text>) -> Self
pub fn help(self, text: impl Into<Text>) -> Self
Renders text under the control, which the control names as its description.
Sourcepub fn disabled(self) -> Self
pub fn disabled(self) -> Self
Renders the control disabled on the create and edit forms. The server drops a
value posted for it anyway, so an edit keeps the stored value and a create takes
the field’s default, else its blank answer; a field with neither cannot be
created, and its panel refuses to mount.
Sourcepub fn disabled_on_edit(self) -> Self
pub fn disabled_on_edit(self) -> Self
Renders the control disabled on the edit form only: the create form sets the value, and no edit changes it.
Sourcepub fn custom(self, control: impl Control + 'static) -> CustomField<F>
pub fn custom(self, control: impl Control + 'static) -> CustomField<F>
Renders the field with an app’s Control instead, as Field::custom does:
how a record form’s control takes a custom input. The field keeps its key and
label; the replaced control’s own modifiers, such as email, unique or
options, are dropped with it.
Source§impl<F> CustomField<F>
impl<F> CustomField<F>
Sourcepub fn choice(self) -> ChoiceField<F>
pub fn choice(self) -> ChoiceField<F>
Renders the field as a choice instead, as Field::choice does, ready for
options or relationship.
The replaced control’s own modifiers are dropped with it.