Skip to main content

Field

Struct Field 

Source
#[non_exhaustive]
pub struct Field { /* private fields */ }
Expand description

One field of a resource’s form: its name (the form’s and the model’s field), its label, its kind and how it looks. The rules are the form type’s own (Validate); required here only marks the field for people and the browser.

use renox_admin::Field;

let fields = vec![
    Field::text("name", "Name").required(),
    Field::select("status", "Status", [("draft", "Draft"), ("live", "Live")]).required(),
    Field::money("price", "Price").required(),
    Field::belongs_to("category_id", "Category", "categories", "name"),
    Field::textarea("notes", "Notes").span_full(),
    Field::toggle("active", "Active").default_value(true),
];

Implementations§

Source§

impl Field

Source

pub fn text(name: &str, label: &str) -> Self

One line of text.

Source

pub fn email(name: &str, label: &str) -> Self

An email address.

Source

pub fn password(name: &str, label: &str) -> Self

A password. The edit page leaves it empty: make the form’s field an Option<String> and change the password only when one was typed.

Source

pub fn url(name: &str, label: &str) -> Self

A web address.

Source

pub fn tel(name: &str, label: &str) -> Self

A phone number.

Source

pub fn textarea(name: &str, label: &str) -> Self

Several lines of text (rows(n), 4 by default).

Source

pub fn number(name: &str, label: &str) -> Self

A number (step, min and max limit it).

Source

pub fn money(name: &str, label: &str) -> Self

An amount the model keeps in the smallest unit (cents, like the data grid’s money columns), with APP_CURRENCY’s code before it. The form shows and takes whole units: with USD, 1299 shows as 12.99 (a 0.01 step), and 12.99 is read back as 1299 before the form’s rules and fill see it (rounded to the cent). A currency without decimals (IDR, JPY) is as typed. Rules on the form (min, max) are in the smallest unit.

Source

pub fn currency_from(self, key: &str) -> Self

For a money field: the amount is in the currency the record keeps in its key field (an ISO 4217 code such as total_currency, the <field>_currency column of a #[model(money)] field), which the resource also lists as a field (a select of codes, say). The edit page shows the amount in whole units with that currency’s decimals and code, the create page uses the currency field’s default value, and the posted amount is read with the decimals of the currency posted with it. Without it the field uses APP_CURRENCY.

use renox_admin::Field;

let fields = vec![
    Field::money("total", "Total").currency_from("total_currency"),
    Field::select("total_currency", "Currency", [("USD", "USD"), ("JPY", "JPY")])
        .default_value("USD"),
];
Source

pub fn quantity(name: &str, label: &str) -> Self

An exact quantity (a Quantity<S> field of the model, kept as a scaled integer and serialized as the decimal text "2.500"). The form shows and takes that decimal text (<rx-input type="quantity">, stepping by the scale’s last digit); the form’s field is a Quantity<S> (or a String) and no conversion is needed. Set the scale with scale and the record’s unit with unit_from.

use renox_admin::Field;

let stock = Field::quantity("stock", "Stock").scale(3).unit_from("stock_unit");
Source

pub fn scale(self, scale: u32) -> Self

For a quantity field: the decimals of its Quantity<SCALE>, 3 for thousandths (at most 9, 0 by default). It sets the input’s step. Other fields ignore it.

Source

pub fn unit_from(self, key: &str) -> Self

For a quantity field: the quantity is in the unit the record keeps in its key field (a code such as stock_unit), which the resource also lists as a field (a select of codes, say). The edit page shows that unit after the input, the create page uses the unit field’s default value. Without it the field has no unit.

use renox_admin::Field;

let fields = vec![
    Field::quantity("stock", "Stock").scale(3).unit_from("stock_unit"),
    Field::select("stock_unit", "Unit", [("kg", "kg"), ("g", "g")])
        .default_value("kg"),
];
Source

pub fn date(name: &str, label: &str) -> Self

A date with the kit’s calendar, sent as YYYY-MM-DD (a NaiveDate).

Source

pub fn datetime(name: &str, label: &str) -> Self

A date and a time, sent as YYYY-MM-DDTHH:MM (a NaiveDateTime in the form; Renox adds the seconds). The edit page shows the stored moment’s first 16 characters, so store UTC or a naive time.

Source

pub fn select<V: Into<String>, L: Into<String>>( name: &str, label: &str, options: impl IntoIterator<Item = (V, L)>, ) -> Self

One of options, (value, label) pairs. searchable() adds a box that filters them.

Source

pub fn checkbox(name: &str, label: &str) -> Self

A checkbox: a bool in the form (ticked or not).

Source

pub fn toggle(name: &str, label: &str) -> Self

A switch: a bool in the form, for a setting that is on or off.

Source

pub fn belongs_to(name: &str, label: &str, table: &str, title: &str) -> Self

The id of a row of table, chosen by its title column: a searchable select of table’s rows (the first 1,000, by title). Pair it with #[validate(exists("table", "id"))] on the form. table and title may only have letters, digits and _.

Source

pub fn required(self) -> Self

Marks the field required for people and the browser (the form’s rules decide).

Source

pub fn hint(self, hint: &str) -> Self

A line of help under the field.

Source

pub fn placeholder(self, placeholder: &str) -> Self

Text shown in the empty field (for a select: its empty choice).

Source

pub fn prefix(self, prefix: &str) -> Self

Text joined to the start of the field (“https://”, “$”).

Source

pub fn suffix(self, suffix: &str) -> Self

Text joined to the end of the field (“kg”, “%”).

Source

pub fn span_full(self) -> Self

Takes the form’s whole width (it has two columns on wider screens).

Source

pub fn rows(self, rows: u32) -> Self

The lines a textarea shows.

Source

pub fn step(self, step: &str) -> Self

A number’s step ("0.01", "any").

Source

pub fn min(self, min: impl ToString) -> Self

The smallest number, or the earliest date.

Source

pub fn max(self, max: impl ToString) -> Self

The largest number, or the latest date.

Source

pub fn autocomplete(self, autocomplete: &str) -> Self

The browser’s autocomplete hint ("off", "email").

Source

pub fn searchable(self) -> Self

For a select: a box to type in that filters the options.

Source

pub fn default_value(self, value: impl Serialize) -> Self

The value the create page starts with (the edit page shows the record’s).

Source

pub fn only_on_create(self) -> Self

Only on the create page.

Source

pub fn only_on_edit(self) -> Self

Only on the edit page.

Source

pub fn readonly_on_edit(self) -> Self

Shown but not changeable on the edit page (it is still sent, so the form’s rules see it). On a settings page (Admin::settings) the stored value is kept whatever is sent, so seeded values can be shown without being editable.

Source

pub fn name(&self) -> &str

The field’s name.

Source

pub fn kind(&self) -> FieldKind

The field’s kind.

Trait Implementations§

Source§

impl Clone for Field

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Field

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Serialize for Field

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl Freeze for Field

§

impl RefUnwindSafe for Field

§

impl Send for Field

§

impl Sync for Field

§

impl Unpin for Field

§

impl UnsafeUnpin for Field

§

impl UnwindSafe for Field

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Fake for T

Source§

fn fake<U>(&self) -> U
where Self: FakeBase<U>,

Source§

fn fake_with_rng<U, R>(&self, rng: &mut R) -> U
where R: RngExt + ?Sized, Self: FakeBase<U>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more