表单、上传与加载反馈
带有 on-submit 的表单可以在一次操作中发送多个绑定。将此代码块粘贴到 index.html 中,输入名称,然后点击 Greet 查看结果。提交表单时会发送该绑定。
<live id="greeting-form">
<state name="name" value="''" />
<state name="message" value="''" />
<form on-submit="$message = 'Hello, ' + $name + '!'">
<label>Name <input @bind($name, on: action) required></label>
<button live-disable>Greet</button>
</form>
<p role="status">{$message}</p>
</live>使用 HTML 控件(例如 required)获取即时反馈,并使用 KScript 类型或规则进行服务器端验证。要保存到数据库,请使用模型表单。
| 属性 | 浏览器行为 |
|---|---|
live-loading |
在操作待处理时显示该元素。 |
live-disable |
在请求完成之前禁用该控件。 |
live-dirty |
当绑定的值有未提交的编辑时显示该元素。请为其指定一个绑定名称,以监视某个字段。 |
live-offline |
在浏览器报告离线时显示该元素。 |
live-error |
操作失败时显示反馈信息。 |
live-cloak |
在实时运行时启动前隐藏标记。 |
在带名称的文件输入框上使用 live-upload。上传内容会随其 on-change 操作或所在表单的 on-submit 操作一起提交。对于名为 attachment 的输入框,$event.files.attachment 包含文件条目,每个条目带有 name、type、size 以及 base64 格式的 bytes。请求最多接受 8 MiB 的文件。保存前请先校验内容和权限。为文件输入框添加 live-replace,可在下次重绘时清空其选中的文件。
使用 @live_download('report.txt', bytes: $report->to_base64()) 将操作生成的字节以文件形式提供。