pub struct FileField<F = ()>(/* private fields */);Expand description
A file field: Field::file, or a record form’s file control (F is the form).
Implementations§
Source§impl<F> FileField<F>
impl<F> FileField<F>
Sourcepub fn extensions<I>(self, extensions: I) -> Self
pub fn extensions<I>(self, extensions: I) -> Self
Refuse a file whose name does not end in one of extensions, listed without the dot and
matched without case, and hint them to the browser’s file picker through accept.
Field::file(lens!(Doc.cover))
.extensions(["png", "jpg", "jpeg", "webp"])
.max_bytes(2 * 1024 * 1024);- The extension is the check, not the bytes. A served upload’s type comes from its
extension, under
X-Content-Type-Options: nosniff, so the extension is what decides how a browser treats the file: sniffing the content would pass PNG bytes namedpage.html. Nothing checks that the bytes are what the extension says; parse them in theUploaderwhen that matters, as before resizing an image. - The name checked is the sanitized one the
Uploaderreceives. A custom uploader that names the stored file otherwise should keep its extension. - Only the server’s check refuses:
acceptis a hint the browser’s picker may ignore. A refused file is not stored, and the error renders on the field. - A refused submission carries a stored upload back through
keep_<field>, signed for the field, so a carry names only a file this field’s rules passed; a carried path whose extension the field does not take is dropped too, as one the uploader does not hold is. A value the record already stores is not checked again. - Mounting refuses an empty list, and an extension that is not letters and digits.
Sourcepub fn max_bytes(self, max_bytes: usize) -> Self
pub fn max_bytes(self, max_bytes: usize) -> Self
Refuse a file larger than max_bytes; the error spells the size in KiB or MiB when it
divides evenly. Only the server checks it, before the Uploader stores
the file; a carried upload passed it when it was stored. Mounting refuses a size at or
above the 10 MiB form-body cap, which refuses a larger body first.
Source§impl<F> FileField<F>
impl<F> FileField<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> FileField<F>
impl<F> FileField<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> FileField<F>
impl<F> FileField<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.