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
Fields can be configured to be readonly. In edit view, these fields are rendered with the additional HTML attributes `readonly` and `disabled`, ensuring that the user cannot interact with the field or change its value.
3
+
Fields can be configured to be readonly. In edit view, these fields are rendered with the additional HTML attributes `readonly` and `disabled`, ensuring that users cannot interact with the field or change its value.
4
4
5
-
In index view, if readonly and index editable is set to `true`, forms will be rendered with the `readonly` HTML attribute.
5
+
In index view, if readonly and index editable are both set to true, forms will be rendered with the `readonly` HTML attribute.
6
6
7
7
## Supported fields
8
8
9
-
On index view, read-only is supported for all fields with the index editable option (see [Index Edit](index-edit.md)).
9
+
On index view, readonly is supported for all fields with the index editable option (see [Index Edit](index-edit.md)).
10
10
11
-
On edit view, read-only is supported for:
11
+
On edit view, readonly is supported for:
12
12
-`Backpex.Fields.Date`
13
13
-`Backpex.Fields.DateTime`
14
14
-`Backpex.Fields.Number`
@@ -17,19 +17,19 @@ On edit view, read-only is supported for:
17
17
18
18
## Configuration
19
19
20
-
To enable read-only for a field, you need to set the `readonly` option to `true` in the field configuration. This key must contain either a boolean value or a function that returns a boolean value.
20
+
To enable readonly for a field, you need to set the `readonly` option to true in the field configuration. This key must contain either a boolean value or a function that returns a boolean value.
21
21
22
22
```elixir
23
23
# in your resource configuration file
24
24
deffieldsdo
25
25
[
26
-
rating: %{
27
-
module:Backpex.Fields.Text,
28
-
label:"Rating",
29
-
readonly:fn assigns ->
30
-
assigns.current_user.role in [:employee]
31
-
end
32
-
}
26
+
rating: %{
27
+
module:Backpex.Fields.Text,
28
+
label:"Rating",
29
+
readonly:fn assigns ->
30
+
assigns.current_user.role in [:employee]
31
+
end
32
+
}
33
33
]
34
34
end
35
35
```
@@ -38,41 +38,54 @@ end
38
38
# in your resource configuration file
39
39
deffieldsdo
40
40
[
41
-
rating: %{
42
-
module:Backpex.Fields.Text,
43
-
label:"Rating",
44
-
readonly:true
45
-
}
41
+
rating: %{
42
+
module:Backpex.Fields.Text,
43
+
label:"Rating",
44
+
readonly:true
45
+
}
46
46
]
47
47
end
48
48
```
49
49
50
50
## Readonly for custom fields
51
51
52
-
You can also add readonly functionality to a custom field. To do this, you need to define a [`render_form_readonly/1`](Backpex.Field.html#c:render_form_readonly/1) function. This function must return markup to be used when readonly is enabled.
52
+
You can also add readonly functionality to a custom field. To do this, you need to handle the readonly state in the `c:Backpex.Field.render_form/1` function of your custom field. You can access the readonly value from the assigns, which will be `true` or `false`.
When defining a custom field with index editable support, you need to handle the readonly state in the index editable markup. There is a `readonly` value in the assigns, which will be `true` or `false`.
78
+
If your readonly logic is more complex, you can also use a dedicated function that returns the markup for the readonly state.
79
+
80
+
```elixir
81
+
@implBackpex.Field
82
+
defrender_form(%{readonly:true} = assigns) do
83
+
# Return readonly markup
84
+
end
85
+
86
+
defrender_form(%{readonly:false} = assigns) do
87
+
# Return editable markup
88
+
end
89
+
```
90
+
91
+
When defining a custom field with index editable support, you need to handle the readonly state in `c:Backpex.Field.render_index_form/1`. There is also a readonly value in the assigns, which will be `true` or `false`.
Copy file name to clipboardExpand all lines: guides/upgrading/v0.14.md
+19Lines changed: 19 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -81,3 +81,22 @@ def changeset(change, attrs, _metadata) do
81
81
|>Ecto.Changeset.validate_required([:field1])
82
82
end
83
83
```
84
+
85
+
## `render_form_readonly/1` callback has been removed
86
+
87
+
We removed the `render_form_readonly/1` callback from fields. Instead, `readonly` must be handled directly in the `c:Backpex.Field.render_form/1` callback.
88
+
89
+
Make sure to update your custom fields accordingly. The `readonly` value will be available in the assigns, which will be `true` or `false`.
90
+
91
+
```elixir
92
+
@implBackpex.Field
93
+
defrender_form(%{readonly:true} = assigns) do
94
+
# Render readonly field
95
+
end
96
+
97
+
defrender_form(%{readonly:false} = assigns) do
98
+
# Render editable field
99
+
end
100
+
```
101
+
102
+
See the [Readonly guide](readonly.md) for more details.
0 commit comments