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
- Out of the box, `core_components.ex` imports an `<.icon name="hero-x-mark" class="w-5 h-5"/>` component for hero icons. **Always** use the `<.icon>` component for icons, **never** use `Heroicons` modules or similar
4
+
-**Always** use the imported `<.input>` component for form inputs from `core_components.ex` when available. `<.input>` is imported and using it will save steps and prevent errors
5
+
- If you override the default input classes (`<.input class="myclass px-2 py-1 rounded-lg">)`) class with your own values, no default classes are inherited, so your
-**Always** begin your LiveView templates with `<Layouts.app flash={@flash} ...>` which wraps all inner content
4
3
- The `MyAppWeb.Layouts` module is aliased in the `my_app_web.ex` file, so you can use it without needing to alias it again
5
-
- Anytime you run into errors with no `current_scope` assign:
6
-
- You failed to follow the Authenticated Routes guidelines, or you failed to pass `current_scope` to `<Layouts.app>`
7
-
-**Always** fix the `current_scope` error by moving your routes to the proper `live_session` and ensure you pass `current_scope` as needed
8
4
- Phoenix v1.8 moved the `<.flash_group>` component to the `Layouts` module. You are **forbidden** from calling `<.flash_group>` outside of the `layouts.ex` module
9
-
- Out of the box, `core_components.ex` imports an `<.icon name="hero-x-mark" class="w-5 h-5"/>` component for hero icons. **Always** use the `<.icon>` component for icons, **never** use `Heroicons` modules or similar
10
-
-**Always** use the imported `<.input>` component for form inputs from `core_components.ex` when available. `<.input>` is imported and using it will save steps and prevent errors
11
-
- If you override the default input classes (`<.input class="myclass px-2 py-1 rounded-lg">)`) class with your own values, no default classes are inherited, so your
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:
6
+
7
+
defmodule MyApp.Users.User do
8
+
use Ecto.Schema
9
+
...
10
+
end
11
+
12
+
And then you create a changeset that you pass to `to_form`:
13
+
14
+
%MyApp.Users.User{}
15
+
|> Ecto.Changeset.change()
16
+
|> to_form()
17
+
18
+
Once the form is submitted, the params will be available under `%{"user" => user_params}`.
19
+
20
+
In the template, the form assign can be passed to the `<.form>` function component:
Always give the form an explicit, unique DOM ID, like `id="todo-form"`.
27
+
28
+
### Avoiding form errors with changesets
29
+
30
+
**Always** use a form assigned via `to_form/1` or `to_form/2`, and the `<.input>` component in the template. In the template **always access forms this way**:
31
+
32
+
<%!-- ALWAYS do this (valid) --%>
33
+
<.form for={@form} id="my-form">
34
+
<.input field={@form[:field]} type="text" />
35
+
</.form>
36
+
37
+
And **never** do this:
38
+
39
+
<%!-- NEVER do this (invalid) --%>
40
+
<.form for={@changeset} id="my-form">
41
+
<.input field={@changeset[:field]} type="text" />
42
+
</.form>
43
+
44
+
- You are FORBIDDEN from accessing the changeset in the template as it will cause errors
45
+
-**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` that is derived from a changeset
Copy file name to clipboardExpand all lines: usage-rules/ecto.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
@@ -4,7 +4,7 @@
4
4
- Remember `import Ecto.Query` and other supporting modules when you write `seeds.exs`
5
5
-`Ecto.Schema` fields always use the `:string` type, even for `:text`, columns, ie: `field :name, :string`
6
6
-`Ecto.Changeset.validate_number/2`**DOES NOT SUPPORT the `:allow_nil` option**. By default, Ecto validations only run if a change for the given field exists and the change value is not nil, so such as option is never needed
7
-
- You **must** use `Ecto.Changeset.get_field(changeset, :field)` to access changeset fields
7
+
-**Never** use map access syntax (`changeset[:field]`) on changesets. 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
Copy file name to clipboardExpand all lines: usage-rules/elixir.md
+6-6Lines changed: 6 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,18 +18,18 @@
18
18
you *must* bind the result of the expression to a variable if you want to use it and you CANNOT rebind the result inside the expression, ie:
19
19
20
20
# INVALID: we are rebinding inside the `if` and the result never gets assigned
21
-
if connected?(socket) do
22
-
socket = assign(socket, :val, val)
21
+
if some_condition?(data) do
22
+
data = transform(data)
23
23
end
24
24
25
25
# VALID: we rebind the result of the `if` to a new variable
26
-
socket =
27
-
if connected?(socket) do
28
-
assign(socket, :val, val)
26
+
data =
27
+
if some_condition?(data) do
28
+
transform(data)
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 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
32
+
-**Never** use map access syntax (`my_struct[:field]`) 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
Copy file name to clipboardExpand all lines: usage-rules/html.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,9 +2,9 @@
2
2
3
3
- Phoenix templates **always** use `~H` or .html.heex files (known as HEEx), **never** use `~E`
4
4
-**Always** use the imported `Phoenix.Component.form/1` and `Phoenix.Component.inputs_for/1` function to build forms. **Never** use `Phoenix.HTML.form_for` or `Phoenix.HTML.inputs_for` as they are outdated
5
-
- When building forms **always** use the already imported `Phoenix.Component.to_form/2` (`assign(socket, form: to_form(...))` and `<.form for={@form} id="msg-form">`), then access those forms in the template via `@form[:field]`
5
+
- When building forms **always** use the already imported `Phoenix.Component.to_form/2` (`assign(:form, to_form(...))` and `<.form for={@form} id="msg-form">`), then access those forms in the template via `@form[:field]`
6
6
-**Always** add unique DOM IDs to key elements (like forms, buttons, etc) when writing templates, these IDs can later be used in tests (`<.form for={@form} id="product-form">`)
7
-
- For "app wide" template imports, you can import/alias into the `my_app_web.ex`'s `html_helpers` block, so they will be available to all LiveViews, LiveComponent's, and all modules that do `use MyAppWeb, :html` (replace "my_app" by the actual app name)
7
+
- For "app wide" template imports, you can import/alias into the `my_app_web.ex`'s `html_helpers` block, so they will be available to all templates and all modules that do `use MyAppWeb, :html` (replace "my_app" by the actual app name)
8
8
9
9
- Elixir supports `if/else` but **does NOT support `if/else if` or `if/elsif`**. **Never use `else if` or `elseif` in Elixir**, **always** use `cond` or `case` for multiple conditionals.
Always give the form an explicit, unique DOM ID, like `id="todo-form"`.
190
-
191
-
#### Avoiding form errors
189
+
In the template, the form assign can be passed to the `<.form>` function component:
192
190
193
-
**Always** use a form assigned via `to_form/2` in the LiveView, and the `<.input>` component in the template. In the template **always access forms this**:
-**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
195
+
Always give the form an explicit, unique DOM ID, like `id="todo-form"`.
0 commit comments