1
1
defmodule Ecto.Changeset do
2
2
@ moduledoc ~S"""
3
- Changesets allow filtering, casting, validation and
4
- definition of constraints when manipulating structs, usually in
5
- preparation for inserting and updating entries into a database.
6
-
7
- There is an example of
8
- [working with changesets](`Ecto#module-changesets`) in the
9
- introductory documentation in the `Ecto` module. In a nutshell, there
10
- are two main functions for creating a changeset. The `cast/4` function
11
- is used to receive external parameters from a form, API or command
12
- line, and convert them to the types defined in your `Ecto.Schema`.
13
- `change/2` is used to modify data directly from your application,
14
- assuming the data given is valid and matches the existing types.
15
-
16
- The remaining functions in this module, such as validations,
17
- constraints, association handling, are about manipulating
3
+ Changesets allow filtering, type casting, validation, and
4
+ constraints when manipulating structs, usually in preparation
5
+ for inserting and updating entries into a database.
6
+
7
+ Let's break down what those features mean. Imagine the common
8
+ scenario where you want to receive data from a user submitted
9
+ web form to create or update entries in the database. Once you
10
+ receive this data on the server, changesets will help you perform
11
+ the following actions:
12
+
13
+ * **filtering** - because you are receiving external data from
14
+ a third-party, you must explicitly list which data you accept.
15
+ For example, you most likely don't want to allow a user to set
16
+ its own "is_admin" field to true
17
+
18
+ * **type casting** - a web form sends most of its data as strings.
19
+ When the user types the number "100", Ecto will receive it as
20
+ as the string "100", which must then be converted to 100.
21
+ Changesets are responsible for converting these values to the
22
+ types defined in your `Ecto.Schema`, support even complex types
23
+ such as datetimes
24
+
25
+ * **validations** - the data the user submits may not correct.
26
+ For example, the user may type an invalid email address, with
27
+ a trailing dot. Or say the date for a future meeting would
28
+ happen in the last year. You must validate the data and give
29
+ feedback to the user
30
+
31
+ * **constraints** - some validations can only happen with the
32
+ help of the database. For example, in order to know if a user
33
+ email is already taken or not, you must query the database.
34
+ Constraints help you do that in a way that respects data
35
+ integrity
36
+
37
+ Although we have used a web form as an example, changesets can be used
38
+ for APIs and many other scenarios. Changesets may also be used to work
39
+ with data even if it won't be written to a database. We will cover
40
+ these scenarios in the documentation below. There is also an introductory
41
+ example of working with changesets and how it relates to schemas and
42
+ repositories [in the `Ecto` module](`Ecto#module-changesets`).
43
+
44
+ In a nutshell, there are two main functions for creating a changeset.
45
+ The `cast/4` function is used to receive external parameters from a
46
+ form, API or command line, and convert them to the types defined in
47
+ your `Ecto.Schema`. `change/2` is used to modify data directly from
48
+ your application, assuming the data given is valid and matches the
49
+ existing types. The remaining functions in this module, such as
50
+ validations, constraints, association handling, are about manipulating
18
51
changesets.
19
52
20
53
## External vs internal data
@@ -25,7 +58,7 @@ defmodule Ecto.Changeset do
25
58
a form that needs to be type-converted and properly validated. This
26
59
use case is primarily covered by the `cast/4` function.
27
60
28
- * internal to the application - for example programmatically generated,
61
+ * internal to the application - for example programmatically generated,
29
62
or coming from other subsystems. This use case is primarily covered
30
63
by the `change/2` and `put_change/3` functions.
31
64
0 commit comments