You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: usage-rules/ecto.md
+23Lines changed: 23 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,3 +7,26 @@
7
7
- You **must** use `Ecto.Changeset.get_field(changeset, :field)` to access changeset fields
8
8
- Fields which are set programatically, such as `user_id`, must not be listed in `cast` calls or similar for security purposes. Instead they must be explicitly set when creating the struct
9
9
-**Always** invoke `mix ecto.gen.migration migration_name_using_underscores` when generating migration files, so the correct timestamp and conventions are applied
10
+
11
+
### Creating LiveView forms from changesets
12
+
13
+
When using changesets with LiveView, the underlying data, form params, and errors are retrieved from it. The `:as` option is automatically computed too. E.g. if you have a user schema:
14
+
15
+
defmodule MyApp.Users.User do
16
+
use Ecto.Schema
17
+
...
18
+
end
19
+
20
+
And then you create a changeset that you pass to `to_form`:
21
+
22
+
%MyApp.Users.User{}
23
+
|> Ecto.Changeset.change()
24
+
|> to_form()
25
+
26
+
Once the form is submitted, the params will be available under `%{"user" => user_params}`.
27
+
28
+
In the template, the form assign can be passed to the `<.form>` function component:
Copy file name to clipboardExpand all lines: usage-rules/elixir.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -29,7 +29,7 @@
29
29
end
30
30
31
31
-**Never** nest multiple modules in the same file as it can cause cyclic dependencies and compilation errors
32
-
-**Never** use map access syntax (`changeset[:field]`) on structs as they do not implement the Access behaviour by default. For regular structs, you **must** access the fields directly, such as `my_struct.field` or use higher level APIs that are available on the struct if they exist, `Ecto.Changeset.get_field/2` for changesets
32
+
-**Never** use map access syntax on structs as they do not implement the Access behaviour by default. For structs, you **must** access the fields directly, such as `my_struct.field` or use higher level APIs that are available on the struct if they exist
33
33
- Elixir's standard library has everything necessary for date and time manipulation. Familiarize yourself with the common `Time`, `Date`, `DateTime`, and `Calendar` interfaces by accessing their documentation as necessary. **Never** install additional dependencies unless asked or for date/time parsing (which you can use the `date_time_parser` package)
34
34
- Don't use `String.to_atom/1` on user input (memory leak risk)
35
35
- Predicate function names should not start with `is_` and should end in a question mark. Names like `is_thing` should be reserved for guards
When using changesets, the underlying data, form params, and errors are retrieved from it. The `:as` option is automatically computed too. E.g. if you have a user schema:
192
-
193
-
defmodule MyApp.Users.User do
194
-
use Ecto.Schema
195
-
...
196
-
end
197
-
198
-
And then you create a changeset that you pass to `to_form`:
199
-
200
-
%MyApp.Users.User{}
201
-
|> Ecto.Changeset.change()
202
-
|> to_form()
203
-
204
-
Once the form is submitted, the params will be available under `%{"user" => user_params}`.
205
-
206
-
In the template, the form form assign can be passed to the `<.form>` function component:
Always give the form an explicit, unique DOM ID, like `id="todo-form"`.
213
190
214
191
#### Avoiding form errors
@@ -223,9 +200,8 @@ Always give the form an explicit, unique DOM ID, like `id="todo-form"`.
223
200
And **never** do this:
224
201
225
202
<%!-- NEVER do this (invalid) --%>
226
-
<.form for={@changeset} id="my-form">
227
-
<.input field={@changeset[:field]} type="text" />
203
+
<.form for={@data} id="my-form">
204
+
<.input field={@data[:field]} type="text" />
228
205
</.form>
229
206
230
-
- You are FORBIDDEN from accessing the changeset in the template as it will cause errors
231
-
-**Never** use `<.form let={f} ...>` in the template, instead **always use `<.form for={@form} ...>`**, then drive all form references from the form assign as in `@form[:field]`. The UI should **always** be driven by a `to_form/2` assigned in the LiveView module that is derived from a changeset
207
+
-**Never** use `<.form let={f} ...>` in the template, instead **always use `<.form for={@form} ...>`**, then drive all form references from the form assign as in `@form[:field]`. The UI should **always** be driven by a `to_form/1` or `to_form/2` assigned in the LiveView module
0 commit comments